@@ -50,6 +50,13 @@ module.exports = function(AV) {
5050 return chunks . join ( "" ) ;
5151 } ;
5252
53+ var dataURLToBase64 = function ( base64 ) {
54+ if ( base64 . split ( ',' ) [ 0 ] && base64 . split ( ',' ) [ 0 ] . indexOf ( 'base64' ) >= 0 ) {
55+ base64 = base64 . split ( ',' ) [ 1 ] ;
56+ }
57+ return base64 ;
58+ } ;
59+
5360 // A list of file extensions to mime types as found here:
5461 // http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-
5562 // mime-type-of-a-file-based-on-the-file-signature
@@ -317,6 +324,10 @@ module.exports = function(AV) {
317324 */
318325 AV . File = function ( name , data , type ) {
319326 this . _name = name ;
327+
328+ // 用来存储转换后要上传的 base64 String
329+ this . _base64 = '' ;
330+
320331 var currentUser ;
321332 try {
322333 currentUser = AV . User . current ( ) ;
@@ -337,19 +348,22 @@ module.exports = function(AV) {
337348 this . _guessedType = guessedType ;
338349
339350 if ( _ . isArray ( data ) ) {
340- this . _source = AV . Promise . as ( encodeBase64 ( data ) , guessedType ) ;
351+ this . _base64 = encodeBase64 ( data ) ;
352+ this . _source = AV . Promise . as ( this . _base64 , guessedType ) ;
341353 this . _metaData . size = data . length ;
342354 } else if ( data && data . base64 ) {
343355 var parseBase64 = require ( './browserify-wrapper/parse-base64' ) ;
344356 var dataBase64 = parseBase64 ( data . base64 , guessedType ) ;
357+ this . _base64 = dataURLToBase64 ( data . base64 ) ;
345358 this . _source = AV . Promise . as ( dataBase64 , guessedType ) ;
346359 } else if ( data && data . blob ) {
347360 this . _source = AV . Promise . as ( data . blob , guessedType ) ;
348361 } else if ( typeof ( File ) !== "undefined" && data instanceof File ) {
349362 this . _source = AV . Promise . as ( data , guessedType ) ;
350363 } else if ( AV . _isNode && global . Buffer . isBuffer ( data ) ) {
351364 // use global.Buffer to prevent browserify pack Buffer module
352- this . _source = AV . Promise . as ( data . toString ( 'base64' ) , guessedType ) ;
365+ this . _base64 = data . toString ( 'base64' ) ;
366+ this . _source = AV . Promise . as ( this . _base64 , guessedType ) ;
353367 this . _metaData . size = data . length ;
354368 } else if ( _ . isString ( data ) ) {
355369 throw "Creating a AV.File from a String is not yet supported." ;
@@ -567,10 +581,12 @@ module.exports = function(AV) {
567581 }
568582 var self = this ;
569583 if ( ! self . _previousSave ) {
570- if ( self . _source ) {
584+ // 如果是国内节点
585+ if ( self . _source && AV . serverURL === AV . _config . cnApiUrl ) {
586+ // 通过国内 CDN 服务商上传
571587 var upload = require ( './browserify-wrapper/upload' ) ;
572588 upload ( self , AV , saveOptions ) ;
573- } else if ( self . _url && self . _metaData [ '__source' ] == 'external' ) {
589+ } else if ( self . _url && self . _metaData [ '__source' ] == 'external' ) {
574590 //external link file.
575591 var data = {
576592 name : self . _name ,
@@ -579,7 +595,7 @@ module.exports = function(AV) {
579595 mime_type : self . _guessedType ,
580596 url : self . _url
581597 } ;
582- self . _previousSave = AV . _request ( " files" , self . _name , null , 'POST' , data ) . then ( function ( response ) {
598+ self . _previousSave = AV . _request ( ' files' , self . _name , null , 'POST' , data ) . then ( function ( response ) {
583599 self . _name = response . name ;
584600 self . _url = response . url ;
585601 self . id = response . objectId ;
@@ -588,6 +604,34 @@ module.exports = function(AV) {
588604 }
589605 return self ;
590606 } ) ;
607+ } else if ( AV . serverURL !== AV . _config . cnApiUrl ) {
608+ // 海外节点,通过 LeanCloud 服务器中转
609+ self . _previousSave = self . _source . then ( function ( file , type ) {
610+ var data = {
611+ base64 : '' ,
612+ _ContentType : type ,
613+ ACL : self . _acl ,
614+ mime_type : type ,
615+ metaData : self . _metaData ,
616+ } ;
617+ // 判断是否数据已经是 base64
618+ if ( self . _base64 ) {
619+ data . base64 = self . _base64 ;
620+ return AV . _request ( 'files' , self . _name , null , 'POST' , data ) ;
621+ } else {
622+ return readAsync ( file ) . then ( function ( base64 ) {
623+ data . base64 = base64 ;
624+ return AV . _request ( 'files' , self . _name , null , 'POST' , data ) ;
625+ } ) ;
626+ }
627+ } ) . then ( function ( response ) {
628+ self . _name = response . name ;
629+ self . _url = response . url ;
630+ self . id = response . objectId ;
631+ if ( response . size )
632+ self . _metaData . size = response . size ;
633+ return self ;
634+ } ) ;
591635 }
592636 }
593637 return self . _previousSave . _thenRunCallbacks ( options ) ;
0 commit comments