@@ -32,6 +32,7 @@ const isFolder = util.isFolder;
3232const cleanPath = util . cleanPath ;
3333const shouldBeTreatedAsBinary = util . shouldBeTreatedAsBinary ;
3434const readBinaryData = util . readBinaryData ;
35+ const getJSONFromLocalStorage = util . getJSONFromLocalStorage ;
3536
3637let hasLocalStorage ;
3738
@@ -92,6 +93,18 @@ function googleDrivePath (path) {
9293 return cleanPath ( `${ PATH_PREFIX } /${ path } ` ) ;
9394}
9495
96+ /**
97+ * Remove surrounding quotes from a string.
98+ *
99+ * @param {string } string - string with surrounding quotes
100+ * @returns {string } string without surrounding quotes
101+ *
102+ * @private
103+ */
104+ function removeQuotes ( string ) {
105+ return string . replace ( / ^ [ " ' ] ( .* ) [ " ' ] $ / , "$1" ) ;
106+ }
107+
95108/**
96109 * Internal cache object for storing Google file IDs.
97110 *
@@ -129,12 +142,7 @@ const GoogleDrive = function (remoteStorage, clientId) {
129142 hasLocalStorage = util . localStorageAvailable ( ) ;
130143
131144 if ( hasLocalStorage ) {
132- let settings ;
133- try {
134- settings = JSON . parse ( localStorage . getItem ( SETTINGS_KEY ) ) ;
135- } catch ( e ) {
136- // no settings stored
137- }
145+ const settings = getJSONFromLocalStorage ( SETTINGS_KEY ) ;
138146 if ( settings ) {
139147 this . configure ( settings ) ;
140148 }
@@ -258,7 +266,7 @@ GoogleDrive.prototype = {
258266 function putDone ( response ) {
259267 if ( response . status >= 200 && response . status < 300 ) {
260268 const meta = JSON . parse ( response . responseText ) ;
261- const etagWithoutQuotes = meta . etag . substring ( 1 , meta . etag . length - 1 ) ;
269+ const etagWithoutQuotes = removeQuotes ( meta . etag ) ;
262270 return Promise . resolve ( { statusCode : 200 , contentType : meta . mimeType , revision : etagWithoutQuotes } ) ;
263271 } else if ( response . status === 412 ) {
264272 return Promise . resolve ( { statusCode : 412 , revision : 'conflict' } ) ;
@@ -302,7 +310,7 @@ GoogleDrive.prototype = {
302310 return this . _getMeta ( id ) . then ( ( meta ) => {
303311 let etagWithoutQuotes ;
304312 if ( ( typeof meta === 'object' ) && ( typeof meta . etag === 'string' ) ) {
305- etagWithoutQuotes = meta . etag . substring ( 1 , meta . etag . length - 1 ) ;
313+ etagWithoutQuotes = removeQuotes ( meta . etag ) ;
306314 }
307315 if ( options && options . ifMatch && ( options . ifMatch !== etagWithoutQuotes ) ) {
308316 return { statusCode : 412 , revision : etagWithoutQuotes } ;
@@ -430,14 +438,13 @@ GoogleDrive.prototype = {
430438 return this . _getMeta ( id ) . then ( ( meta ) => {
431439 let etagWithoutQuotes ;
432440 if ( typeof ( meta ) === 'object' && typeof ( meta . etag ) === 'string' ) {
433- etagWithoutQuotes = meta . etag . substring ( 1 , meta . etag . length - 1 ) ;
441+ etagWithoutQuotes = removeQuotes ( meta . etag ) ;
434442 }
435443
436444 if ( options && options . ifNoneMatch && ( etagWithoutQuotes === options . ifNoneMatch ) ) {
437445 return Promise . resolve ( { statusCode : 304 } ) ;
438446 }
439447
440- const options2 = { } ;
441448 if ( ! meta . downloadUrl ) {
442449 if ( meta . exportLinks && meta . exportLinks [ 'text/html' ] ) {
443450 // Documents that were generated inside GoogleDocs have no
@@ -450,7 +457,7 @@ GoogleDrive.prototype = {
450457 }
451458 }
452459
453- return this . _request ( 'GET' , meta . downloadUrl , options2 ) . then ( ( response ) => {
460+ return this . _request ( 'GET' , meta . downloadUrl , { } ) . then ( ( response ) => {
454461 let body = response . response ;
455462 if ( meta . mimeType . match ( / ^ a p p l i c a t i o n \/ j s o n / ) ) {
456463 try {
@@ -514,7 +521,7 @@ GoogleDrive.prototype = {
514521
515522 itemsMap = { } ;
516523 for ( const item of data . items ) {
517- etagWithoutQuotes = item . etag . substring ( 1 , item . etag . length - 1 ) ;
524+ etagWithoutQuotes = removeQuotes ( item . etag ) ;
518525 if ( item . mimeType === GD_DIR_MIME_TYPE ) {
519526 this . _fileIdCache . set ( path + item . title + '/' , item . id ) ;
520527 itemsMap [ item . title + '/' ] = {
0 commit comments