66'use strict' ;
77
88const _ = require ( 'underscore' ) ;
9+ const cos = require ( './uploader/cos' ) ;
10+ const qiniu = require ( './uploader/qiniu' ) ;
911
1012module . exports = function ( AV ) {
1113
@@ -617,7 +619,7 @@ module.exports = function(AV) {
617619 * @return {AV.Promise } Resolved with the response
618620 * @private
619621 */
620- _qiniuToken : function ( type ) {
622+ _fileToken : function ( type , route = 'fileTokens' ) {
621623 const name = this . attributes . name ;
622624 //Create 16-bits uuid as qiniu key.
623625 const extName = extname ( name ) ;
@@ -636,7 +638,7 @@ module.exports = function(AV) {
636638 this . attributes . metaData . mime_type = type ;
637639 }
638640 this . _qiniu_key = key ;
639- return AV . _request ( "qiniu" , null , null , 'POST' , data ) ;
641+ return AV . _request ( route , null , null , 'POST' , data ) ;
640642 } ,
641643
642644 /**
@@ -670,8 +672,22 @@ module.exports = function(AV) {
670672 var isCnNodeFlag = isCnNode ( ) ;
671673 if ( this . _source && isCnNodeFlag ) {
672674 // 通过国内 CDN 服务商上传
673- var upload = require ( './browserify-wrapper/upload' ) ;
674- upload ( this , AV , saveOptions ) ;
675+ this . _previousSave = this . _source . then ( ( data , type ) => {
676+ return this . _fileToken ( type ) . catch ( ( ) => this . _fileToken ( type , 'qiniu' ) )
677+ . then ( uploadInfo => {
678+ let uploadPromise ;
679+ if ( uploadInfo . provider === 'qcloud' ) {
680+ uploadPromise = cos ( uploadInfo , data , this , saveOptions ) ;
681+ } else {
682+ uploadPromise = qiniu ( uploadInfo , data , this , saveOptions ) ;
683+ }
684+ return uploadPromise . catch ( err => {
685+ //destroy this file object when upload fails.
686+ this . destroy ( ) ;
687+ throw err ;
688+ } ) ;
689+ } ) ;
690+ } ) ;
675691 } else if ( this . attributes . url && this . attributes . metaData . __source === 'external' ) {
676692 //external link file.
677693 var data = {
0 commit comments