Skip to content

Commit d09e161

Browse files
authored
fix(File): parse file data after fileToken (#460)
1 parent 5dd99fc commit d09e161

File tree

1 file changed

+73
-67
lines changed

1 file changed

+73
-67
lines changed

src/file.js

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const AVRequest = require('./request').request;
77
const Promise = require('./promise');
88
const { tap } = require('./utils');
99
const debug = require('debug')('leancloud:file');
10+
const parseBase64 = require('./utils/parse-base64');
1011

1112
module.exports = function(AV) {
1213

@@ -101,7 +102,16 @@ module.exports = function(AV) {
101102
base64: '',
102103
};
103104

105+
if (_.isString(data)) {
106+
throw new TypeError("Creating an AV.File from a String is not yet supported.");
107+
}
108+
if (_.isArray(data)) {
109+
this.attributes.metaData.size = data.length;
110+
data = { base64: encodeBase64(data) };
111+
}
112+
104113
this._extName = '';
114+
this._data = data;
105115

106116
let owner;
107117
if (data && data.owner) {
@@ -117,46 +127,10 @@ module.exports = function(AV) {
117127
}
118128
}
119129
}
120-
121-
this.attributes.metaData = {
122-
owner: (owner ? owner.id : 'unknown')
123-
};
130+
131+
this.attributes.metaData.owner = owner ? owner.id : 'unknown';
124132

125133
this.set('mime_type', mimeType);
126-
127-
if (_.isArray(data)) {
128-
this.attributes.metaData.size = data.length;
129-
data = { base64: encodeBase64(data) };
130-
}
131-
if (data && data.base64) {
132-
var parseBase64 = require('./utils/parse-base64');
133-
var dataBase64 = parseBase64(data.base64, mimeType);
134-
this._source = Promise.resolve({ data: dataBase64, type: mimeType });
135-
} else if (data && data.blob) {
136-
if (!data.blob.type && mimeType) {
137-
data.blob.type = mimeType;
138-
}
139-
if (!data.blob.name) {
140-
data.blob.name = name;
141-
}
142-
if (process.env.CLIENT_PLATFORM === 'ReactNative' || process.env.CLIENT_PLATFORM === 'Weapp') {
143-
this._extName = extname(data.blob.uri);
144-
}
145-
this._source = Promise.resolve({ data: data.blob, type: mimeType });
146-
} else if (typeof File !== "undefined" && data instanceof File) {
147-
if (data.size) {
148-
this.attributes.metaData.size = data.size;
149-
}
150-
if (data.name) {
151-
this._extName = extname(data.name);
152-
}
153-
this._source = Promise.resolve({ data, type: mimeType });
154-
} else if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
155-
this.attributes.metaData.size = data.length;
156-
this._source = Promise.resolve({ data, type: mimeType });
157-
} else if (_.isString(data)) {
158-
throw new Error("Creating a AV.File from a String is not yet supported.");
159-
}
160134
};
161135

162136
/**
@@ -450,37 +424,68 @@ module.exports = function(AV) {
450424
throw new Error('File already saved. If you want to manipulate a file, use AV.Query to get it.');
451425
}
452426
if (!this._previousSave) {
453-
if (this._source) {
454-
this._previousSave = this._source.then(({ data, type }) =>
455-
this._fileToken(type)
456-
.then(uploadInfo => {
457-
if (uploadInfo.mime_type) {
458-
this.set('mime_type', uploadInfo.mime_type);
427+
if (this._data) {
428+
let mimeType = this.get('mime_type');
429+
this._previousSave = this._fileToken(mimeType).then(uploadInfo => {
430+
if (uploadInfo.mime_type) {
431+
mimeType = uploadInfo.mime_type;
432+
this.set('mime_type', mimeType);
433+
}
434+
this._token = uploadInfo.token;
435+
return Promise.resolve().then(() => {
436+
const data = this._data;
437+
if (data && data.base64) {
438+
return parseBase64(data.base64, mimeType);
439+
}
440+
if (data && data.blob) {
441+
if (!data.blob.type && mimeType) {
442+
data.blob.type = mimeType;
443+
}
444+
if (!data.blob.name) {
445+
data.blob.name = this.get('name');
446+
}
447+
if (process.env.CLIENT_PLATFORM === 'ReactNative' || process.env.CLIENT_PLATFORM === 'Weapp') {
448+
this._extName = extname(data.blob.uri);
459449
}
460-
this._token = uploadInfo.token;
461-
462-
let uploadPromise;
463-
switch (uploadInfo.provider) {
464-
case 's3':
465-
uploadPromise = s3(uploadInfo, data, this, options);
466-
break;
467-
case 'qcloud':
468-
uploadPromise = cos(uploadInfo, data, this, options);
469-
break;
470-
case 'qiniu':
471-
default:
472-
uploadPromise = qiniu(uploadInfo, data, this, options);
473-
break;
450+
return data.blob;
451+
}
452+
if (typeof File !== "undefined" && data instanceof File) {
453+
if (data.size) {
454+
this.attributes.metaData.size = data.size;
474455
}
475-
return uploadPromise.then(
476-
tap(() => this._callback(true)),
477-
(error) => {
478-
this._callback(false);
479-
throw error;
480-
}
481-
);
482-
})
483-
);
456+
if (data.name) {
457+
this._extName = extname(data.name);
458+
}
459+
return data;
460+
}
461+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
462+
this.attributes.metaData.size = data.length;
463+
return data;
464+
}
465+
throw new TypeError('malformed file data');
466+
}).then(data => {
467+
let uploadPromise;
468+
switch (uploadInfo.provider) {
469+
case 's3':
470+
uploadPromise = s3(uploadInfo, data, this, options);
471+
break;
472+
case 'qcloud':
473+
uploadPromise = cos(uploadInfo, data, this, options);
474+
break;
475+
case 'qiniu':
476+
default:
477+
uploadPromise = qiniu(uploadInfo, data, this, options);
478+
break;
479+
}
480+
return uploadPromise.then(
481+
tap(() => this._callback(true)),
482+
(error) => {
483+
this._callback(false);
484+
throw error;
485+
}
486+
);
487+
});
488+
});
484489
} else if (this.attributes.url && this.attributes.metaData.__source === 'external') {
485490
// external link file.
486491
const data = {
@@ -510,6 +515,7 @@ module.exports = function(AV) {
510515
result: success,
511516
}).catch(debug);
512517
delete this._token;
518+
delete this._data;
513519
},
514520

515521
/**

0 commit comments

Comments
 (0)