alchemy-media 0.9.0-alpha.2 → 0.9.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/helper_field/file_field_type.js +19 -18
- package/model/media_file_model.js +9 -2
- package/model/media_raw_model.js +15 -10
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## 0.9.0-alpha.4 (2024-10-10)
|
|
2
|
+
|
|
3
|
+
* Make `MediaRaw#addFile(url)` support data uris too
|
|
4
|
+
|
|
5
|
+
## 0.9.0-alpha.3 (2024-08-16)
|
|
6
|
+
|
|
7
|
+
* Implement Alchemy v1.4 `Field` changes
|
|
8
|
+
|
|
1
9
|
## 0.9.0-alpha.2 (2024-02-19)
|
|
2
10
|
|
|
3
11
|
* Add `getFile()` method to `MediaFile` and `MediaRaw` documents
|
|
@@ -68,30 +68,31 @@ if (Blast.isBrowser) {
|
|
|
68
68
|
*
|
|
69
69
|
* @author Jelle De Loecker <jelle@develry.be>
|
|
70
70
|
* @since 0.2.0
|
|
71
|
-
* @version 0.
|
|
71
|
+
* @version 0.9.0
|
|
72
72
|
*
|
|
73
|
-
* @param {
|
|
74
|
-
* @param {
|
|
75
|
-
* @param {Datasource} datasource
|
|
76
|
-
* @param {Function} callback
|
|
73
|
+
* @param {Alchemy.OperationalContext.SaveFieldToDatasource} context
|
|
74
|
+
* @param {*} value
|
|
77
75
|
*
|
|
78
|
-
* @return {
|
|
76
|
+
* @return {Pledge<ObjectId>|ObjectId}
|
|
79
77
|
*/
|
|
80
|
-
FileField.setMethod(function _toDatasource(
|
|
78
|
+
FileField.setMethod(function _toDatasource(context, value) {
|
|
81
79
|
|
|
82
|
-
|
|
80
|
+
let options = Object.assign({}, this.options);
|
|
83
81
|
|
|
84
|
-
|
|
82
|
+
let pass_through_media_file = typeof value == 'string' && (value.startsWith('http') || value.startsWith('/'));
|
|
85
83
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
84
|
+
if (!pass_through_media_file) {
|
|
85
|
+
if (value && typeof value == 'object' && !alchemy.isObjectId(value)) {
|
|
86
|
+
pass_through_media_file = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
91
89
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
} else {
|
|
95
|
-
callback(null, this.cast(value));
|
|
90
|
+
if (!pass_through_media_file) {
|
|
91
|
+
return this.cast(value);
|
|
96
92
|
}
|
|
93
|
+
|
|
94
|
+
return Swift.waterfall(
|
|
95
|
+
() => this.getModel('MediaFile').addFile(value, options),
|
|
96
|
+
result => result._id,
|
|
97
|
+
);
|
|
97
98
|
});
|
|
@@ -151,6 +151,8 @@ MediaFile.Document.setFieldGetter(function path() {
|
|
|
151
151
|
*
|
|
152
152
|
* @param {String|ObjectID} id
|
|
153
153
|
* @param {Function} callback
|
|
154
|
+
*
|
|
155
|
+
* @return {Pledge<Alchemy.Document.MediaFile>}
|
|
154
156
|
*/
|
|
155
157
|
MediaFile.setMethod(function getFile(id, callback) {
|
|
156
158
|
|
|
@@ -165,6 +167,9 @@ MediaFile.setMethod(function getFile(id, callback) {
|
|
|
165
167
|
}
|
|
166
168
|
};
|
|
167
169
|
|
|
170
|
+
let pledge = new Swift();
|
|
171
|
+
pledge.done(callback);
|
|
172
|
+
|
|
168
173
|
Function.series(function findInExtra(next) {
|
|
169
174
|
if (!alchemy.plugins.media.extra_media_model) {
|
|
170
175
|
return next();
|
|
@@ -209,15 +214,17 @@ MediaFile.setMethod(function getFile(id, callback) {
|
|
|
209
214
|
}, async function done(err) {
|
|
210
215
|
|
|
211
216
|
if (err) {
|
|
212
|
-
return
|
|
217
|
+
return pledge.reject(err);
|
|
213
218
|
}
|
|
214
219
|
|
|
215
220
|
if (!result.MediaRaw) {
|
|
216
221
|
await result.populate('MediaRaw');
|
|
217
222
|
}
|
|
218
223
|
|
|
219
|
-
|
|
224
|
+
pledge.resolve(result);
|
|
220
225
|
});
|
|
226
|
+
|
|
227
|
+
return pledge;
|
|
221
228
|
});
|
|
222
229
|
|
|
223
230
|
/**
|
package/model/media_raw_model.js
CHANGED
|
@@ -192,7 +192,7 @@ MediaRaw.Document.setMethod(function extraImportFromStream(input) {
|
|
|
192
192
|
*
|
|
193
193
|
* @author Jelle De Loecker <jelle@develry.be>
|
|
194
194
|
* @since 0.0.1
|
|
195
|
-
* @version 0.
|
|
195
|
+
* @version 0.9.0
|
|
196
196
|
*
|
|
197
197
|
* @param {String} file The path to the file, can be a URL
|
|
198
198
|
* @param {Object} options
|
|
@@ -217,8 +217,11 @@ MediaRaw.setMethod(function addFile(file, options, callback) {
|
|
|
217
217
|
file = file.path;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
+
let is_url = file.startsWith('http://') || file.startsWith('https://'),
|
|
221
|
+
is_data_uri = !is_url && file.startsWith('data:');
|
|
222
|
+
|
|
220
223
|
// If the given file is actually a url, we'll need to download it first
|
|
221
|
-
if (
|
|
224
|
+
if (is_url || is_data_uri) {
|
|
222
225
|
|
|
223
226
|
// Set the url as the origin
|
|
224
227
|
options.origin = file;
|
|
@@ -232,16 +235,18 @@ MediaRaw.setMethod(function addFile(file, options, callback) {
|
|
|
232
235
|
return callback(err);
|
|
233
236
|
}
|
|
234
237
|
|
|
235
|
-
if (!
|
|
236
|
-
options.filename
|
|
237
|
-
|
|
238
|
+
if (!is_data_uri) {
|
|
239
|
+
if (!options.filename) {
|
|
240
|
+
options.filename = filename;
|
|
241
|
+
}
|
|
238
242
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
243
|
+
if (!options.filename) {
|
|
244
|
+
options.filename = Url.parse(file).pathname.split('/').last();
|
|
245
|
+
}
|
|
242
246
|
|
|
243
|
-
|
|
244
|
-
|
|
247
|
+
if (!options.name) {
|
|
248
|
+
options.name = options.filename.beforeLast('.') || options.filename;
|
|
249
|
+
}
|
|
245
250
|
}
|
|
246
251
|
|
|
247
252
|
that.addFile(tempfile, options, callback);
|