6
6
const _ = require ( 'underscore' ) ;
7
7
const cos = require ( './uploader/cos' ) ;
8
8
const qiniu = require ( './uploader/qiniu' ) ;
9
+ const s3 = require ( './uploader/s3' ) ;
9
10
const AVError = require ( './error' ) ;
10
11
const AVRequest = require ( './request' ) . request ;
11
12
@@ -618,20 +619,19 @@ module.exports = function(AV) {
618
619
* @return {AV.Promise } Resolved with the response
619
620
* @private
620
621
*/
621
- _fileToken : function ( type , route = 'fileTokens' ) {
622
+ _fileToken ( type , route = 'fileTokens' ) {
622
623
const name = this . attributes . name ;
623
- //Create 16-bits uuid as qiniu key.
624
+
625
+ // Create 16-bits uuid as qiniu key.
624
626
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 ) ;
628
628
const key = hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + hexOctet ( ) + extName ;
629
629
const data = {
630
- key : key ,
630
+ key,
631
+ name,
631
632
ACL : this . _acl ,
632
- name : name ,
633
633
mime_type : type ,
634
- metaData : this . attributes . metaData
634
+ metaData : this . attributes . metaData ,
635
635
} ;
636
636
if ( type && ! this . attributes . metaData . mime_type ) {
637
637
this . attributes . metaData . mime_type = type ;
@@ -651,7 +651,7 @@ module.exports = function(AV) {
651
651
* @param {Object } options A Backbone-style options object.
652
652
* @return {AV.Promise } Promise that is resolved when the save finishes.
653
653
*/
654
- save : function ( ...args ) {
654
+ save ( ...args ) {
655
655
if ( this . id ) {
656
656
throw new Error ( 'File already saved. If you want to manipulate a file, use AV.Query to get it.' ) ;
657
657
}
@@ -660,41 +660,45 @@ module.exports = function(AV) {
660
660
switch ( args . length ) {
661
661
case 1 :
662
662
options = args [ 0 ] ;
663
- break ;
663
+ break ;
664
664
case 2 :
665
665
saveOptions = args [ 0 ] ;
666
666
options = args [ 1 ] ;
667
- break ;
667
+ break ;
668
668
}
669
669
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 )
676
673
. then ( uploadInfo => {
677
674
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 ;
682
686
}
683
687
return uploadPromise . catch ( err => {
684
- //destroy this file object when upload fails.
688
+ // destroy this file object when upload fails.
685
689
this . destroy ( ) ;
686
690
throw err ;
687
691
} ) ;
688
- } ) ;
689
- } ) ;
692
+ } )
693
+ ) ;
690
694
} else if ( this . attributes . url && this . attributes . metaData . __source === 'external' ) {
691
- //external link file.
692
- var data = {
695
+ // external link file.
696
+ const data = {
693
697
name : this . attributes . name ,
694
698
ACL : this . _acl ,
695
699
metaData : this . attributes . metaData ,
696
700
mime_type : this . _guessedType ,
697
- url : this . attributes . url
701
+ url : this . attributes . url ,
698
702
} ;
699
703
this . _previousSave = AVRequest ( 'files' , this . attributes . name , null , 'post' , data ) . then ( ( response ) => {
700
704
this . attributes . name = response . name ;
@@ -705,38 +709,6 @@ module.exports = function(AV) {
705
709
}
706
710
return this ;
707
711
} ) ;
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
- } ) ;
740
712
}
741
713
}
742
714
return this . _previousSave . _thenRunCallbacks ( options ) ;
0 commit comments