66const _ = require ( 'underscore' ) ;
77const cos = require ( './uploader/cos' ) ;
88const qiniu = require ( './uploader/qiniu' ) ;
9+ const s3 = require ( './uploader/s3' ) ;
910const AVError = require ( './error' ) ;
1011const AVRequest = require ( './request' ) . request ;
1112
@@ -618,20 +619,19 @@ module.exports = function(AV) {
618619 * @return {AV.Promise } Resolved with the response
619620 * @private
620621 */
621- _fileToken : function ( type , route = 'fileTokens' ) {
622+ _fileToken ( type , route = 'fileTokens' ) {
622623 const name = this . attributes . name ;
623- //Create 16-bits uuid as qiniu key.
624+
625+ // Create 16-bits uuid as qiniu key.
624626 const extName = extname ( name ) ;
625- const hexOctet = function ( ) {
626- return Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) . substring ( 1 ) ;
627- } ;
627+ const hexOctet = ( ) => Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) . substring ( 1 ) ;
628628 const key = hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + extName ;
629629 const data = {
630- key : key ,
630+ key,
631+ name,
631632 ACL : this . _acl ,
632- name : name ,
633633 mime_type : type ,
634- metaData : this . attributes . metaData
634+ metaData : this . attributes . metaData ,
635635 } ;
636636 if ( type && ! this . attributes . metaData . mime_type ) {
637637 this . attributes . metaData . mime_type = type ;
@@ -651,7 +651,7 @@ module.exports = function(AV) {
651651 * @param {Object } options A Backbone-style options object.
652652 * @return {AV.Promise } Promise that is resolved when the save finishes.
653653 */
654- save : function ( ...args ) {
654+ save ( ...args ) {
655655 if ( this . id ) {
656656 throw new Error ( 'File already saved. If you want to manipulate a file, use AV.Query to get it.' ) ;
657657 }
@@ -660,41 +660,45 @@ module.exports = function(AV) {
660660 switch ( args . length ) {
661661 case 1 :
662662 options = args [ 0 ] ;
663- break ;
663+ break ;
664664 case 2 :
665665 saveOptions = args [ 0 ] ;
666666 options = args [ 1 ] ;
667- break ;
667+ break ;
668668 }
669669 if ( ! this . _previousSave ) {
670- // 如果是国内节点
671- var isCnNodeFlag = isCnNode ( ) ;
672- if ( this . _source && isCnNodeFlag ) {
673- // 通过国内 CDN 服务商上传
674- this . _previousSave = this . _source . then ( ( data , type ) => {
675- return this . _fileToken ( type ) . catch ( ( ) => this . _fileToken ( type , 'qiniu' ) )
670+ if ( this . _source ) {
671+ this . _previousSave = this . _source . then ( ( data , type ) =>
672+ this . _fileToken ( type )
676673 . then ( uploadInfo => {
677674 let uploadPromise ;
678- if ( uploadInfo . provider === 'qcloud' ) {
679- uploadPromise = cos ( uploadInfo , data , this , saveOptions ) ;
680- } else {
681- uploadPromise = qiniu ( uploadInfo , data , this , saveOptions ) ;
675+ switch ( uploadInfo . provider ) {
676+ case 's3' :
677+ uploadPromise = s3 ( uploadInfo , data , this , saveOptions ) ;
678+ break ;
679+ case 'qcloud' :
680+ uploadPromise = cos ( uploadInfo , data , this , saveOptions ) ;
681+ break ;
682+ case 'qiniu' :
683+ default :
684+ uploadPromise = qiniu ( uploadInfo , data , this , saveOptions ) ;
685+ break ;
682686 }
683687 return uploadPromise . catch ( err => {
684- //destroy this file object when upload fails.
688+ // destroy this file object when upload fails.
685689 this . destroy ( ) ;
686690 throw err ;
687691 } ) ;
688- } ) ;
689- } ) ;
692+ } )
693+ ) ;
690694 } else if ( this . attributes . url && this . attributes . metaData . __source === 'external' ) {
691- //external link file.
692- var data = {
695+ // external link file.
696+ const data = {
693697 name : this . attributes . name ,
694698 ACL : this . _acl ,
695699 metaData : this . attributes . metaData ,
696700 mime_type : this . _guessedType ,
697- url : this . attributes . url
701+ url : this . attributes . url ,
698702 } ;
699703 this . _previousSave = AVRequest ( 'files' , this . attributes . name , null , 'post' , data ) . then ( ( response ) => {
700704 this . attributes . name = response . name ;
@@ -705,38 +709,6 @@ module.exports = function(AV) {
705709 }
706710 return this ;
707711 } ) ;
708- } else if ( ! isCnNodeFlag ) {
709- // 海外节点,通过 LeanCloud 服务器中转
710- this . _previousSave = this . _source . then ( ( file , type ) => {
711- var data = {
712- base64 : '' ,
713- _ContentType : type ,
714- ACL : this . _acl ,
715- mime_type : type ,
716- metaData : this . attributes . metaData ,
717- } ;
718- // 判断是否数据已经是 base64
719- if ( this . attributes . base64 ) {
720- data . base64 = this . attributes . base64 ;
721- return AVRequest ( 'files' , this . attributes . name , null , 'POST' , data ) ;
722- } else if ( typeof global . Buffer !== "undefined" && global . Buffer . isBuffer ( file ) ) {
723- data . base64 = file . toString ( 'base64' ) ;
724- return AVRequest ( 'files' , this . attributes . name , null , 'POST' , data ) ;
725- } else {
726- return readAsync ( file ) . then ( function ( base64 ) {
727- data . base64 = base64 ;
728- return AVRequest ( 'files' , this . attributes . name , null , 'POST' , data ) ;
729- } ) ;
730- }
731- } ) . then ( ( response ) => {
732- this . attributes . name = response . name ;
733- this . attributes . url = response . url ;
734- this . id = response . objectId ;
735- if ( response . size ) {
736- this . attributes . metaData . size = response . size ;
737- }
738- return this ;
739- } ) ;
740712 }
741713 }
742714 return this . _previousSave . _thenRunCallbacks ( options ) ;
0 commit comments