Skip to content

Commit 9788b86

Browse files
committed
feat(File): add keepFileName save option
1 parent 37c53c3 commit 9788b86

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/file.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ module.exports = function(AV) {
496496
const data = {
497497
key,
498498
name,
499+
keep_file_name: true,
499500
ACL: this._acl,
500501
mime_type: type,
501502
metaData: this.attributes.metaData,
@@ -512,13 +513,12 @@ module.exports = function(AV) {
512513
* Saves the file to the AV cloud.
513514
* @param {AuthOptions} [options] AuthOptions plus:
514515
* @param {UploadProgressCallback} [options.onprogress] 文件上传进度,在 Node.js 中无效,回调参数说明详见 {@link UploadProgressCallback}。
516+
* @param {boolean} [options.keepFileName = false] 保留下载文件的文件名。
515517
* @return {Promise} Promise that is resolved when the save finishes.
516518
*/
517519
save(options = {}) {
518520
if (this.id) {
519-
throw new Error(
520-
'File already saved. If you want to manipulate a file, use AV.Query to get it.'
521-
);
521+
throw new Error('File is already saved.');
522522
}
523523
if (!this._previousSave) {
524524
if (this._data) {

src/uploader/qiniu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function upload(uploadInfo, data, file, saveOptions = {}) {
1414
.set(file._uploadHeaders)
1515
.attach('file', data, file.attributes.name)
1616
.field('name', file.attributes.name)
17-
.field('key', file._qiniu_key)
17+
.field('key', uploadInfo.key || file._qiniu_key)
1818
.field('token', uptoken);
1919
if (saveOptions.onprogress) {
2020
req.on('progress', saveOptions.onprogress);

test/file.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,27 @@ try {
99

1010
describe('File', function() {
1111
describe('Saving base64', function() {
12+
var base64 = 'd29ya2luZyBhdCBhdm9zY2xvdWQgaXMgZ3JlYXQh';
13+
var fileName = 'base64.txt';
14+
1215
it('should be saved', function() {
13-
var base64 = 'd29ya2luZyBhdCBhdm9zY2xvdWQgaXMgZ3JlYXQh';
14-
var file = new AV.File('base64.txt', { base64: base64 });
16+
var file = new AV.File(fileName, { base64: base64 });
17+
file.metaData('format', 'txt file');
18+
file.setACL(new AV.ACL());
19+
return file.save().then(function() {
20+
expect(file.id).to.be.ok();
21+
expect(file.metaData('format')).to.be('txt file');
22+
expect(file.get('mime_type')).to.be('text/plain');
23+
return file.destroy({ useMasterKey: true });
24+
});
25+
});
26+
27+
it('with keepFileName', function() {
28+
var file = new AV.File(fileName, { base64: base64 });
1529
file.metaData('format', 'txt file');
1630
file.setACL(new AV.ACL());
1731
return file.save().then(function() {
32+
expect(file.url()).to.match(new RegExp(fileName + '$'));
1833
expect(file.ownerId()).to.be.ok();
1934
expect(file.id).to.be.ok();
2035
expect(file.metaData('format')).to.be('txt file');
@@ -163,7 +178,7 @@ describe('File', function() {
163178
var file = AV.File.createWithoutData(fileId);
164179
expect(function saveFetchedFile() {
165180
file.save();
166-
}).to.throwError(/File already saved\./);
181+
}).to.throwError(/File is already saved\./);
167182
});
168183
describe('fetch', () => {
169184
before(function() {

0 commit comments

Comments
 (0)