-
Notifications
You must be signed in to change notification settings - Fork 114
/
file.js
686 lines (643 loc) · 20.6 KB
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
const _ = require('underscore');
const cos = require('./uploader/cos');
const qiniu = require('./uploader/qiniu');
const s3 = require('./uploader/s3');
const AVError = require('./error');
const { request, _request: AVRequest } = require('./request');
const { tap, transformFetchOptions } = require('./utils');
const debug = require('debug')('leancloud:file');
const parseBase64 = require('./utils/parse-base64');
module.exports = function(AV) {
// port from browserify path module
// since react-native packager won't shim node modules.
const extname = path => {
if (!_.isString(path)) return '';
return path.match(
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/
)[4];
};
const b64Digit = number => {
if (number < 26) {
return String.fromCharCode(65 + number);
}
if (number < 52) {
return String.fromCharCode(97 + (number - 26));
}
if (number < 62) {
return String.fromCharCode(48 + (number - 52));
}
if (number === 62) {
return '+';
}
if (number === 63) {
return '/';
}
throw new Error('Tried to encode large digit ' + number + ' in base64.');
};
var encodeBase64 = function(array) {
var chunks = [];
chunks.length = Math.ceil(array.length / 3);
_.times(chunks.length, function(i) {
var b1 = array[i * 3];
var b2 = array[i * 3 + 1] || 0;
var b3 = array[i * 3 + 2] || 0;
var has2 = i * 3 + 1 < array.length;
var has3 = i * 3 + 2 < array.length;
chunks[i] = [
b64Digit((b1 >> 2) & 0x3f),
b64Digit(((b1 << 4) & 0x30) | ((b2 >> 4) & 0x0f)),
has2 ? b64Digit(((b2 << 2) & 0x3c) | ((b3 >> 6) & 0x03)) : '=',
has3 ? b64Digit(b3 & 0x3f) : '=',
].join('');
});
return chunks.join('');
};
/**
* An AV.File is a local representation of a file that is saved to the AV
* cloud.
* @param name {String} The file's name. This will change to a unique value
* once the file has finished saving.
* @param data {Array} The data for the file, as either:
* 1. an Array of byte value Numbers, or
* 2. an Object like { base64: "..." } with a base64-encoded String.
* 3. a Blob(File) selected with a file upload control in a browser.
* 4. an Object like { blob: {uri: "..."} } that mimics Blob
* in some non-browser environments such as React Native.
* 5. a Buffer in Node.js runtime.
* 6. a Stream in Node.js runtime.
*
* For example:<pre>
* var fileUploadControl = $("#profilePhotoFileUpload")[0];
* if (fileUploadControl.files.length > 0) {
* var file = fileUploadControl.files[0];
* var name = "photo.jpg";
* var file = new AV.File(name, file);
* file.save().then(function() {
* // The file has been saved to AV.
* }, function(error) {
* // The file either could not be read, or could not be saved to AV.
* });
* }</pre>
*
* @class
* @param [mimeType] {String} Content-Type header to use for the file. If
* this is omitted, the content type will be inferred from the name's
* extension.
*/
AV.File = function(name, data, mimeType) {
this.attributes = {
name,
url: '',
metaData: {},
// 用来存储转换后要上传的 base64 String
base64: '',
};
if (_.isString(data)) {
throw new TypeError(
'Creating an AV.File from a String is not yet supported.'
);
}
if (_.isArray(data)) {
this.attributes.metaData.size = data.length;
data = { base64: encodeBase64(data) };
}
this._extName = '';
this._data = data;
this._uploadHeaders = {};
if (data && data.blob && typeof data.blob.uri === 'string') {
this._extName = extname(data.blob.uri);
}
if (typeof Blob !== 'undefined' && data instanceof Blob) {
if (data.size) {
this.attributes.metaData.size = data.size;
}
if (data.name) {
this._extName = extname(data.name);
}
}
/* NODE-ONLY:start */
if (data instanceof require('stream') && data.path) {
this._extName = extname(data.path);
}
if (Buffer.isBuffer(data)) {
this.attributes.metaData.size = data.length;
}
/* NODE-ONLY:end */
let owner;
if (data && data.owner) {
owner = data.owner;
} else if (!AV._config.disableCurrentUser) {
try {
owner = AV.User.current();
} catch (error) {
if ('SYNC_API_NOT_AVAILABLE' !== error.code) {
throw error;
}
}
}
this.attributes.metaData.owner = owner ? owner.id : 'unknown';
this.set('mime_type', mimeType);
};
/**
* Creates a fresh AV.File object with exists url for saving to AVOS Cloud.
* @param {String} name the file name
* @param {String} url the file url.
* @param {Object} [metaData] the file metadata object.
* @param {String} [type] Content-Type header to use for the file. If
* this is omitted, the content type will be inferred from the name's
* extension.
* @return {AV.File} the file object
*/
AV.File.withURL = function(name, url, metaData, type) {
if (!name || !url) {
throw new Error('Please provide file name and url');
}
var file = new AV.File(name, null, type);
//copy metaData properties to file.
if (metaData) {
for (var prop in metaData) {
if (!file.attributes.metaData[prop])
file.attributes.metaData[prop] = metaData[prop];
}
}
file.attributes.url = url;
//Mark the file is from external source.
file.attributes.metaData.__source = 'external';
file.attributes.metaData.size = 0;
return file;
};
/**
* Creates a file object with exists objectId.
* @param {String} objectId The objectId string
* @return {AV.File} the file object
*/
AV.File.createWithoutData = function(objectId) {
if (!objectId) {
throw new TypeError('The objectId must be provided');
}
var file = new AV.File();
file.id = objectId;
return file;
};
/**
* Request file censor.
* @since 4.13.0
* @param {String} objectId
* @return {Promise.<string>}
*/
AV.File.censor = function(objectId) {
if (!AV._config.masterKey) {
throw new Error('Cannot censor a file without masterKey');
}
return request({
method: 'POST',
path: `/files/${objectId}/censor`,
authOptions: { useMasterKey: true },
}).then(res => res.censorResult);
};
_.extend(
AV.File.prototype,
/** @lends AV.File.prototype */ {
className: '_File',
_toFullJSON(seenObjects, full = true) {
var json = _.clone(this.attributes);
AV._objectEach(json, function(val, key) {
json[key] = AV._encode(val, seenObjects, undefined, full);
});
AV._objectEach(this._operations, function(val, key) {
json[key] = val;
});
if (_.has(this, 'id')) {
json.objectId = this.id;
}
['createdAt', 'updatedAt'].forEach(key => {
if (_.has(this, key)) {
const val = this[key];
json[key] = _.isDate(val) ? val.toJSON() : val;
}
});
if (full) {
json.__type = 'File';
}
return json;
},
/**
* Returns a JSON version of the file with meta data.
* Inverse to {@link AV.parseJSON}
* @since 3.0.0
* @return {Object}
*/
toFullJSON(seenObjects = []) {
return this._toFullJSON(seenObjects);
},
/**
* Returns a JSON version of the object.
* @return {Object}
*/
toJSON(key, holder, seenObjects = [this]) {
return this._toFullJSON(seenObjects, false);
},
/**
* Gets a Pointer referencing this file.
* @private
*/
_toPointer() {
return {
__type: 'Pointer',
className: this.className,
objectId: this.id,
};
},
/**
* Returns the ACL for this file.
* @returns {AV.ACL} An instance of AV.ACL.
*/
getACL() {
return this._acl;
},
/**
* Sets the ACL to be used for this file.
* @param {AV.ACL} acl An instance of AV.ACL.
*/
setACL(acl) {
if (!(acl instanceof AV.ACL)) {
return new AVError(AVError.OTHER_CAUSE, 'ACL must be a AV.ACL.');
}
this._acl = acl;
return this;
},
/**
* Gets the name of the file. Before save is called, this is the filename
* given by the user. After save is called, that name gets prefixed with a
* unique identifier.
*/
name() {
return this.get('name');
},
/**
* Gets the url of the file. It is only available after you save the file or
* after you get the file from a AV.Object.
* @return {String}
*/
url() {
return this.get('url');
},
/**
* Gets the attributs of the file object.
* @param {String} The attribute name which want to get.
* @returns {Any}
*/
get(attrName) {
switch (attrName) {
case 'objectId':
return this.id;
case 'url':
case 'name':
case 'mime_type':
case 'metaData':
case 'createdAt':
case 'updatedAt':
return this.attributes[attrName];
default:
return this.attributes.metaData[attrName];
}
},
/**
* Set the metaData of the file object.
* @param {Object} Object is an key value Object for setting metaData.
* @param {String} attr is an optional metadata key.
* @param {Object} value is an optional metadata value.
* @returns {String|Number|Array|Object}
*/
set(...args) {
const set = (attrName, value) => {
switch (attrName) {
case 'name':
case 'url':
case 'mime_type':
case 'base64':
case 'metaData':
this.attributes[attrName] = value;
break;
default:
// File 并非一个 AVObject,不能完全自定义其他属性,所以只能都放在 metaData 上面
this.attributes.metaData[attrName] = value;
break;
}
};
switch (args.length) {
case 1:
// 传入一个 Object
for (var k in args[0]) {
set(k, args[0][k]);
}
break;
case 2:
set(args[0], args[1]);
break;
}
return this;
},
/**
* Set a header for the upload request.
* For more infomation, go to https://url.leanapp.cn/avfile-upload-headers
*
* @param {String} key header key
* @param {String} value header value
* @return {AV.File} this
*/
setUploadHeader(key, value) {
this._uploadHeaders[key] = value;
return this;
},
/**
* <p>Returns the file's metadata JSON object if no arguments is given.Returns the
* metadata value if a key is given.Set metadata value if key and value are both given.</p>
* <p><pre>
* var metadata = file.metaData(); //Get metadata JSON object.
* var size = file.metaData('size'); // Get the size metadata value.
* file.metaData('format', 'jpeg'); //set metadata attribute and value.
*</pre></p>
* @return {Object} The file's metadata JSON object.
* @param {String} attr an optional metadata key.
* @param {Object} value an optional metadata value.
**/
metaData(attr, value) {
if (attr && value) {
this.attributes.metaData[attr] = value;
return this;
} else if (attr && !value) {
return this.attributes.metaData[attr];
} else {
return this.attributes.metaData;
}
},
/**
* 如果文件是图片,获取图片的缩略图URL。可以传入宽度、高度、质量、格式等参数。
* @return {String} 缩略图URL
* @param {Number} width 宽度,单位:像素
* @param {Number} heigth 高度,单位:像素
* @param {Number} quality 质量,1-100的数字,默认100
* @param {Number} scaleToFit 是否将图片自适应大小。默认为true。
* @param {String} fmt 格式,默认为png,也可以为jpeg,gif等格式。
*/
thumbnailURL(
width,
height,
quality = 100,
scaleToFit = true,
fmt = 'png'
) {
const url = this.attributes.url;
if (!url) {
throw new Error('Invalid url.');
}
if (!width || !height || width <= 0 || height <= 0) {
throw new Error('Invalid width or height value.');
}
if (quality <= 0 || quality > 100) {
throw new Error('Invalid quality value.');
}
const mode = scaleToFit ? 2 : 1;
return (
url +
'?imageView/' +
mode +
'/w/' +
width +
'/h/' +
height +
'/q/' +
quality +
'/format/' +
fmt
);
},
/**
* Returns the file's size.
* @return {Number} The file's size in bytes.
**/
size() {
return this.metaData().size;
},
/**
* Returns the file's owner.
* @return {String} The file's owner id.
*/
ownerId() {
return this.metaData().owner;
},
/**
* Destroy the file.
* @param {AuthOptions} options
* @return {Promise} A promise that is fulfilled when the destroy
* completes.
*/
destroy(options) {
if (!this.id) {
return Promise.reject(new Error('The file id does not eixst.'));
}
var request = AVRequest(
'files',
null,
this.id,
'DELETE',
null,
options
);
return request;
},
/**
* Request Qiniu upload token
* @param {string} type
* @return {Promise} Resolved with the response
* @private
*/
_fileToken(type, authOptions) {
let name = this.attributes.name;
let extName = extname(name);
if (!extName && this._extName) {
name += this._extName;
extName = this._extName;
}
const data = {
name,
keep_file_name: authOptions.keepFileName,
key: authOptions.key,
ACL: this._acl,
mime_type: type,
metaData: this.attributes.metaData,
};
return AVRequest('fileTokens', null, null, 'POST', data, authOptions);
},
/**
* @callback UploadProgressCallback
* @param {XMLHttpRequestProgressEvent} event - The progress event with 'loaded' and 'total' attributes
*/
/**
* Saves the file to the AV cloud.
* @param {AuthOptions} [options] AuthOptions plus:
* @param {UploadProgressCallback} [options.onprogress] 文件上传进度,在 Node.js 中无效,回调参数说明详见 {@link UploadProgressCallback}。
* @param {boolean} [options.keepFileName = false] 保留下载文件的文件名。
* @param {string} [options.key] 指定文件的 key。设置该选项需要使用 masterKey
* @return {Promise} Promise that is resolved when the save finishes.
*/
save(options = {}) {
if (this.id) {
throw new Error('File is already saved.');
}
if (!this._previousSave) {
if (this._data) {
let mimeType = this.get('mime_type');
this._previousSave = this._fileToken(mimeType, options).then(
uploadInfo => {
if (uploadInfo.mime_type) {
mimeType = uploadInfo.mime_type;
this.set('mime_type', mimeType);
}
this._token = uploadInfo.token;
return Promise.resolve()
.then(() => {
const data = this._data;
if (data && data.base64) {
return parseBase64(data.base64, mimeType);
}
if (data && data.blob) {
if (!data.blob.type && mimeType) {
data.blob.type = mimeType;
}
if (!data.blob.name) {
data.blob.name = this.get('name');
}
return data.blob;
}
if (typeof Blob !== 'undefined' && data instanceof Blob) {
return data;
}
/* NODE-ONLY:start */
if (data instanceof require('stream')) {
return data;
}
if (Buffer.isBuffer(data)) {
return data;
}
/* NODE-ONLY:end */
throw new TypeError('malformed file data');
})
.then(data => {
const _options = _.extend({}, options);
// filter out download progress events
if (options.onprogress) {
_options.onprogress = event => {
if (event.direction === 'download') return;
return options.onprogress(event);
};
}
switch (uploadInfo.provider) {
case 's3':
return s3(uploadInfo, data, this, _options);
case 'qcloud':
return cos(uploadInfo, data, this, _options);
case 'qiniu':
default:
return qiniu(uploadInfo, data, this, _options);
}
})
.then(tap(() => this._callback(true)), error => {
this._callback(false);
throw error;
});
}
);
} else if (
this.attributes.url &&
this.attributes.metaData.__source === 'external'
) {
// external link file.
const data = {
name: this.attributes.name,
ACL: this._acl,
metaData: this.attributes.metaData,
mime_type: this.mimeType,
url: this.attributes.url,
};
this._previousSave = AVRequest(
'files',
null,
null,
'post',
data,
options
).then(response => {
this.id = response.objectId;
return this;
});
}
}
return this._previousSave;
},
_callback(success) {
AVRequest('fileCallback', null, null, 'post', {
token: this._token,
result: success,
}).catch(debug);
delete this._token;
delete this._data;
},
/**
* fetch the file from server. If the server's representation of the
* model differs from its current attributes, they will be overriden,
* @param {Object} fetchOptions Optional options to set 'keys',
* 'include' and 'includeACL' option.
* @param {AuthOptions} options
* @return {Promise} A promise that is fulfilled when the fetch
* completes.
*/
fetch(fetchOptions, options) {
if (!this.id) {
throw new Error('Cannot fetch unsaved file');
}
var request = AVRequest(
'files',
null,
this.id,
'GET',
transformFetchOptions(fetchOptions),
options
);
return request.then(this._finishFetch.bind(this));
},
_finishFetch(response) {
var value = AV.Object.prototype.parse(response);
value.attributes = {
name: value.name,
url: value.url,
mime_type: value.mime_type,
bucket: value.bucket,
};
value.attributes.metaData = value.metaData || {};
value.id = value.objectId;
// clean
delete value.objectId;
delete value.metaData;
delete value.url;
delete value.name;
delete value.mime_type;
delete value.bucket;
_.extend(this, value);
return this;
},
/**
* Request file censor
* @since 4.13.0
* @return {Promise.<string>}
*/
censor() {
if (!this.id) {
throw new Error('Cannot censor an unsaved file');
}
return AV.File.censor(this.id);
},
}
);
};