@@ -45,8 +45,8 @@ const PATH_PREFIX = '/remotestorage';
4545const isFolder = util . isFolder ;
4646const cleanPath = util . cleanPath ;
4747const shouldBeTreatedAsBinary = util . shouldBeTreatedAsBinary ;
48- const readBinaryData = util . readBinaryData ;
4948const getJSONFromLocalStorage = util . getJSONFromLocalStorage ;
49+ const getTextFromArrayBuffer = util . getTextFromArrayBuffer ;
5050
5151/**
5252 * Map a local path to a path in Dropbox.
@@ -389,8 +389,9 @@ Dropbox.prototype = {
389389
390390 var params = {
391391 headers : {
392- 'Dropbox-API-Arg' : JSON . stringify ( { path : getDropboxPath ( path ) } )
393- }
392+ 'Dropbox-API-Arg' : JSON . stringify ( { path : getDropboxPath ( path ) } ) ,
393+ } ,
394+ responseType : 'arraybuffer'
394395 } ;
395396 if ( options && options . ifNoneMatch ) {
396397 params . headers [ 'If-None-Match' ] = options . ifNoneMatch ;
@@ -403,51 +404,52 @@ Dropbox.prototype = {
403404 return Promise . resolve ( { statusCode : status } ) ;
404405 }
405406 meta = resp . getResponseHeader ( 'Dropbox-API-Result' ) ;
406- body = resp . responseText ;
407-
408- if ( status === 409 ) {
409- meta = body ;
410- }
411-
412- try {
413- meta = JSON . parse ( meta ) ;
414- } catch ( e ) {
415- return Promise . reject ( e ) ;
416- }
407+ //first encode the response as text, and later check if
408+ //text appears to actually be binary data
409+ return getTextFromArrayBuffer ( resp . response , 'UTF-8' ) . then ( function ( responseText ) {
410+ body = responseText ;
411+ if ( status === 409 ) {
412+ meta = body ;
413+ }
417414
418- if ( status === 409 ) {
419- if ( compareApiError ( meta , [ 'path' , 'not_found' ] ) ) {
420- return Promise . resolve ( { statusCode : 404 } ) ;
415+ try {
416+ meta = JSON . parse ( meta ) ;
417+ } catch ( e ) {
418+ return Promise . reject ( e ) ;
421419 }
422- return Promise . reject ( new Error ( 'API error while downloading file ("' + path + '"): ' + meta . error_summary ) ) ;
423- }
424420
425- mime = resp . getResponseHeader ( 'Content-Type' ) ;
426- rev = meta . rev ;
427- self . _revCache . set ( path , rev ) ;
428- self . _shareIfNeeded ( path ) ;
421+ if ( status === 409 ) {
422+ if ( compareApiError ( meta , [ 'path' , 'not_found' ] ) ) {
423+ return { statusCode : 404 } ;
424+ }
425+ return Promise . reject ( new Error ( 'API error while downloading file ("' + path + '"): ' + meta . error_summary ) ) ;
426+ }
429427
430- // handling binary
431- if ( shouldBeTreatedAsBinary ( resp . response , mime ) ) {
432- return readBinaryData ( resp . response , mime ) . then ( ( result ) => {
433- return {
434- statusCode : status ,
435- body : result ,
436- contentType : mime ,
437- revision : rev
438- } ;
439- } ) ;
440- }
428+ mime = resp . getResponseHeader ( 'Content-Type' ) ;
429+ rev = meta . rev ;
430+ self . _revCache . set ( path , rev ) ;
431+ self . _shareIfNeeded ( path ) ;
441432
442- // handling json (always try)
443- try {
444- body = JSON . parse ( body ) ;
445- mime = 'application/json; charset=UTF-8' ;
446- } catch ( e ) {
447- //Failed parsing Json, assume it is something else then
448- }
433+ if ( shouldBeTreatedAsBinary ( responseText , mime ) ) {
434+ //return unprocessed response
435+ body = resp . response ;
436+ } else {
437+ // handling json (always try)
438+ try {
439+ body = JSON . parse ( body ) ;
440+ mime = 'application/json; charset=UTF-8' ;
441+ } catch ( e ) {
442+ //Failed parsing Json, assume it is something else then
443+ }
444+ }
449445
450- return Promise . resolve ( { statusCode : status , body : body , contentType : mime , revision : rev } ) ;
446+ return {
447+ statusCode : status ,
448+ body : body ,
449+ contentType : mime ,
450+ revision : rev
451+ } ;
452+ } ) ;
451453 } ) ;
452454 } ,
453455
0 commit comments