Skip to content

Commit 2a2f730

Browse files
committed
refactor(File): Extract qiniu upload token request method
1 parent c6579d0 commit 2a2f730

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

lib/file.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,42 @@
502502
return request._thenRunCallbacks(options);
503503
},
504504

505+
/**
506+
* Request Qiniu upload token
507+
* @param {string} type
508+
* @return {AV.Promise} Resolved with the response
509+
* @private
510+
*/
511+
_qiniuToken: function(type) {
512+
var self = this;
513+
//Create 16-bits uuid as qiniu key.
514+
var extName;
515+
if (AV._isNode) {
516+
var path = require('path');
517+
extName = path.extname(self._name);
518+
} else {
519+
var nameParts = self._name.split('.');
520+
extName = '.' + nameParts[nameParts.length - 1];
521+
}
522+
var hexOctet = function() {
523+
return Math.floor((1+Math.random())*0x10000).toString(16).substring(1);
524+
};
525+
var key = hexOctet() + hexOctet() + hexOctet() + hexOctet() + hexOctet()
526+
+ extName;
527+
528+
var data = {
529+
key: key,
530+
ACL: self._acl,
531+
name:self._name,
532+
mime_type: type,
533+
metaData: self._metaData
534+
};
535+
if(type && self._metaData.mime_type == null)
536+
self._metaData.mime_type = type;
537+
self._qiniu_key = key;
538+
return AV._request("qiniu", null, null, 'POST', data);
539+
},
540+
505541
/**
506542
* @callback UploadProgressCallback
507543
* @param {XMLHttpRequestProgressEvent} event - The progress event with 'loaded' and 'total' attributes
@@ -526,29 +562,12 @@
526562
if (!self._previousSave) {
527563
if(self._source){
528564
if(AV._isNode){
529-
//Use qiniu sdk to upload files to qiniu.
530-
var qiniu = require('qiniu');
531-
var path = require('path');
532565
self._previousSave = self._source.then(function(base64, type) {
533-
//Create 16-bits uuid as qiniu key.
534-
var hexOctet = function() {
535-
return Math.floor((1+Math.random())*0x10000).toString(16).substring(1);
536-
};
537-
var key = hexOctet() + hexOctet() + hexOctet() + hexOctet()
538-
+ path.extname(self._name);
539-
var data = {
540-
key: key,
541-
ACL: self._acl,
542-
name:self._name,
543-
mime_type: type,
544-
metaData: self._metaData
545-
};
546-
if(type && self._metaData.mime_type == null)
547-
self._metaData.mime_type = type;
548-
self._qiniu_key = key;
549566
self._base64 = base64;
550-
return AV._request("qiniu", null, null, 'POST', data);
567+
return self._qiniuToken(type);
551568
}).then(function(response) {
569+
//Use qiniu sdk to upload files to qiniu.
570+
var qiniu = require('qiniu');
552571
self._url = response.url;
553572
self._bucket = response.bucket;
554573
self.id = response.objectId;
@@ -573,27 +592,9 @@
573592
return promise;
574593
});
575594
} else if (this._isBlob) {
576-
577595
self._previousSave = self._source.then(function(blob, type) {
578-
//Create 16-bits uuid as qiniu key.
579-
var hexOctet = function() {
580-
return Math.floor((1+Math.random())*0x10000).toString(16).substring(1);
581-
};
582-
var nameParts = self._name.split('.');
583-
var key = hexOctet() + hexOctet() + hexOctet() + hexOctet() + hexOctet()
584-
+ '.' + nameParts[nameParts.length - 1];
585-
var data = {
586-
key: key,
587-
ACL: self._acl,
588-
name:self._name,
589-
mime_type: type,
590-
metaData: self._metaData
591-
};
592-
if(type && self._metaData.mime_type == null)
593-
self._metaData.mime_type = type;
594-
self._qiniu_key = key;
595596
self._blob = blob;
596-
return AV._request("qiniu", null, null, 'POST', data);
597+
return self._qiniuToken(type);
597598
}).then(function(response) {
598599
self._url = response.url;
599600
self._bucket = response.bucket;

0 commit comments

Comments
 (0)