Skip to content

Commit ff7244e

Browse files
committed
fix(file): make File#save to accept AuthOptions
1 parent d7b8ed9 commit ff7244e

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

src/file.js

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ module.exports = function(AV) {
474474
* @return {Promise} Resolved with the response
475475
* @private
476476
*/
477-
_fileToken(type, route = 'fileTokens') {
477+
_fileToken(type, authOptions) {
478478
let name = this.attributes.name;
479479

480480
let extName = extname(name);
@@ -498,7 +498,7 @@ module.exports = function(AV) {
498498
metaData: this.attributes.metaData,
499499
};
500500
this._qiniu_key = key;
501-
return AVRequest(route, null, null, 'POST', data);
501+
return AVRequest('fileTokens', null, null, 'POST', data, authOptions);
502502
},
503503

504504
/**
@@ -507,7 +507,7 @@ module.exports = function(AV) {
507507
*/
508508
/**
509509
* Saves the file to the AV cloud.
510-
* @param {Object} [options]
510+
* @param {AuthOptions} [options] AuthOptions plus:
511511
* @param {UploadProgressCallback} [options.onprogress] 文件上传进度,在 Node.js 中无效,回调参数说明详见 {@link UploadProgressCallback}。
512512
* @return {Promise} Promise that is resolved when the save finishes.
513513
*/
@@ -520,64 +520,66 @@ module.exports = function(AV) {
520520
if (!this._previousSave) {
521521
if (this._data) {
522522
let mimeType = this.get('mime_type');
523-
this._previousSave = this._fileToken(mimeType).then(uploadInfo => {
524-
if (uploadInfo.mime_type) {
525-
mimeType = uploadInfo.mime_type;
526-
this.set('mime_type', mimeType);
527-
}
528-
this._token = uploadInfo.token;
529-
return Promise.resolve()
530-
.then(() => {
531-
const data = this._data;
532-
if (data && data.base64) {
533-
return parseBase64(data.base64, mimeType);
534-
}
535-
if (data && data.blob) {
536-
if (!data.blob.type && mimeType) {
537-
data.blob.type = mimeType;
523+
this._previousSave = this._fileToken(mimeType, options).then(
524+
uploadInfo => {
525+
if (uploadInfo.mime_type) {
526+
mimeType = uploadInfo.mime_type;
527+
this.set('mime_type', mimeType);
528+
}
529+
this._token = uploadInfo.token;
530+
return Promise.resolve()
531+
.then(() => {
532+
const data = this._data;
533+
if (data && data.base64) {
534+
return parseBase64(data.base64, mimeType);
538535
}
539-
if (!data.blob.name) {
540-
data.blob.name = this.get('name');
536+
if (data && data.blob) {
537+
if (!data.blob.type && mimeType) {
538+
data.blob.type = mimeType;
539+
}
540+
if (!data.blob.name) {
541+
data.blob.name = this.get('name');
542+
}
543+
return data.blob;
541544
}
542-
return data.blob;
543-
}
544-
if (typeof Blob !== 'undefined' && data instanceof Blob) {
545-
return data;
546-
}
547-
/* NODE-ONLY:start */
548-
if (data instanceof require('stream')) {
549-
return data;
550-
}
551-
if (Buffer.isBuffer(data)) {
552-
return data;
553-
}
554-
/* NODE-ONLY:end */
555-
throw new TypeError('malformed file data');
556-
})
557-
.then(data => {
558-
const _options = _.extend({}, options);
559-
// filter out download progress events
560-
if (options.onprogress) {
561-
_options.onprogress = event => {
562-
if (event.direction === 'download') return;
563-
return options.onprogress(event);
564-
};
565-
}
566-
switch (uploadInfo.provider) {
567-
case 's3':
568-
return s3(uploadInfo, data, this, _options);
569-
case 'qcloud':
570-
return cos(uploadInfo, data, this, _options);
571-
case 'qiniu':
572-
default:
573-
return qiniu(uploadInfo, data, this, _options);
574-
}
575-
})
576-
.then(tap(() => this._callback(true)), error => {
577-
this._callback(false);
578-
throw error;
579-
});
580-
});
545+
if (typeof Blob !== 'undefined' && data instanceof Blob) {
546+
return data;
547+
}
548+
/* NODE-ONLY:start */
549+
if (data instanceof require('stream')) {
550+
return data;
551+
}
552+
if (Buffer.isBuffer(data)) {
553+
return data;
554+
}
555+
/* NODE-ONLY:end */
556+
throw new TypeError('malformed file data');
557+
})
558+
.then(data => {
559+
const _options = _.extend({}, options);
560+
// filter out download progress events
561+
if (options.onprogress) {
562+
_options.onprogress = event => {
563+
if (event.direction === 'download') return;
564+
return options.onprogress(event);
565+
};
566+
}
567+
switch (uploadInfo.provider) {
568+
case 's3':
569+
return s3(uploadInfo, data, this, _options);
570+
case 'qcloud':
571+
return cos(uploadInfo, data, this, _options);
572+
case 'qiniu':
573+
default:
574+
return qiniu(uploadInfo, data, this, _options);
575+
}
576+
})
577+
.then(tap(() => this._callback(true)), error => {
578+
this._callback(false);
579+
throw error;
580+
});
581+
}
582+
);
581583
} else if (
582584
this.attributes.url &&
583585
this.attributes.metaData.__source === 'external'
@@ -595,7 +597,8 @@ module.exports = function(AV) {
595597
this.attributes.name,
596598
null,
597599
'post',
598-
data
600+
data,
601+
options
599602
).then(response => {
600603
this.attributes.name = response.name;
601604
this.attributes.url = response.url;

storage.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ interface FileSaveOptions extends AuthOptions {
5454
total: number;
5555
percent: number;
5656
}
57-
) => void;
57+
) => any;
5858
}
5959

6060
export interface WaitOption {

0 commit comments

Comments
 (0)