Skip to content

Commit 844ee70

Browse files
authored
feat(File): add #setUploadHeader (#512)
1 parent c46ef9e commit 844ee70

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/file.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ module.exports = function(AV) {
6767
* @param data {Array} The data for the file, as either:
6868
* 1. an Array of byte value Numbers, or
6969
* 2. an Object like { base64: "..." } with a base64-encoded String.
70-
* 3. a File object selected with a file upload control. (3) only works
71-
* in Firefox 3.6+, Safari 6.0.2+, Chrome 7+, and IE 10+.
72-
* 4.a Buffer object in Node.js runtime.
70+
* 3. a Blob(File) selected with a file upload control.
71+
* 4. a Buffer in Node.js runtime.
72+
* 5. a Stream in Node.js runtime.
7373
*
7474
* For example:<pre>
7575
* var fileUploadControl = $("#profilePhotoFileUpload")[0];
@@ -109,6 +109,7 @@ module.exports = function(AV) {
109109

110110
this._extName = '';
111111
this._data = data;
112+
this._uploadHeaders = {};
112113

113114
if (process.env.CLIENT_PLATFORM === 'ReactNative' || process.env.CLIENT_PLATFORM === 'Weapp') {
114115
if (data && data.blob) {
@@ -333,6 +334,19 @@ module.exports = function(AV) {
333334
}
334335
},
335336

337+
/**
338+
* Set a header for the upload request.
339+
* For more infomation, go to https://url.leanapp.cn/avfile-upload-headers
340+
*
341+
* @param {String} key header key
342+
* @param {String} value header value
343+
* @return {AV.File} this
344+
*/
345+
setUploadHeader(key, value) {
346+
this._uploadHeaders[key] = value;
347+
return this;
348+
},
349+
336350
/**
337351
* <p>Returns the file's metadata JSON object if no arguments is given.Returns the
338352
* metadata value if a key is given.Set metadata value if key and value are both given.</p>

src/uploader/cos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = function upload(uploadInfo, data, file, saveOptions = {}) {
1010

1111
return new Promise((resolve, reject) => {
1212
const req = request('POST', uploadUrl)
13+
.set(file._uploadHeaders)
1314
.field('fileContent', data)
1415
.field('op', 'upload');
1516
if (saveOptions.onprogress) {

src/uploader/qiniu.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = function upload(uploadInfo, data, file, saveOptions = {}) {
1010
const uptoken = uploadInfo.token;
1111
return new Promise((resolve, reject) => {
1212
const req = request('POST', 'https://up.qbox.me')
13+
.set(file._uploadHeaders)
1314
.field('file', data)
1415
.field('name', file.attributes.name)
1516
.field('key', file._qiniu_key)

src/uploader/s3.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = function upload(uploadInfo, data, file, saveOptions = {}) {
1717
return new Promise((resolve, reject) => {
1818
// 海外节点,针对 S3 才会返回 upload_url
1919
const req = request('PUT', uploadInfo.upload_url)
20-
.set('Content-Type', file.get('mime_type'))
20+
.set(Object.assign({
21+
'Content-Type': file.get('mime_type'),
22+
'Cache-Control': 'public, max-age=31536000',
23+
}, file._uploadHeaders));
2124
if (saveOptions.onprogress) {
2225
req.on('progress', saveOptions.onprogress);
2326
}

storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ declare namespace AV {
168168
url(): string;
169169
save(options?: FileSaveOptions): Promise<File>;
170170
setACL(acl?: ACL): any;
171+
setUploadHeader(key: string, value: string): File;
171172
size(): any;
172173
thumbnailURL(width: number, height: number): string;
173174
toFullJSON(): any;

0 commit comments

Comments
 (0)