@@ -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,53 @@ 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- }
417-
418- if ( status === 409 ) {
419- if ( compareApiError ( meta , [ 'path' , 'not_found' ] ) ) {
420- return Promise . resolve ( { statusCode : 404 } ) ;
421- }
422- return Promise . reject ( new Error ( 'API error while downloading file ("' + path + '"): ' + meta . error_summary ) ) ;
423- }
424-
425- mime = resp . getResponseHeader ( 'Content-Type' ) ;
426- rev = meta . rev ;
427- self . _revCache . set ( path , rev ) ;
428- self . _shareIfNeeded ( path ) ;
429-
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- }
441-
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- }
449-
450- return Promise . resolve ( { statusCode : status , body : body , contentType : mime , revision : rev } ) ;
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' )
410+ . then ( function ( responseText ) {
411+ body = responseText ;
412+ if ( status === 409 ) {
413+ meta = body ;
414+ }
415+
416+ try {
417+ meta = JSON . parse ( meta ) ;
418+ } catch ( e ) {
419+ return Promise . reject ( e ) ;
420+ }
421+
422+ if ( status === 409 ) {
423+ if ( compareApiError ( meta , [ 'path' , 'not_found' ] ) ) {
424+ return { statusCode : 404 } ;
425+ }
426+ return Promise . reject ( new Error ( 'API error while downloading file ("' + path + '"): ' + meta . error_summary ) ) ;
427+ }
428+
429+ mime = resp . getResponseHeader ( 'Content-Type' ) ;
430+ rev = meta . rev ;
431+ self . _revCache . set ( path , rev ) ;
432+ self . _shareIfNeeded ( path ) ;
433+
434+ if ( shouldBeTreatedAsBinary ( responseText , mime ) ) {
435+ //return unprocessed response
436+ body = resp . response ;
437+ } else {
438+ // handling json (always try)
439+ try {
440+ body = JSON . parse ( body ) ;
441+ mime = 'application/json; charset=UTF-8' ;
442+ } catch ( e ) {
443+ //Failed parsing Json, assume it is something else then
444+ }
445+ }
446+
447+ return {
448+ statusCode : status ,
449+ body : body ,
450+ contentType : mime ,
451+ revision : rev
452+ } ;
453+ } ) ;
451454 } ) ;
452455 } ,
453456
0 commit comments