Browse Source

fix file get API for onedrive

Gerald 9 years ago
parent
commit
04475a92fb
2 changed files with 36 additions and 7 deletions
  1. 5 3
      src/background/sync/index.js
  2. 31 4
      src/background/sync/onedrive.js

+ 5 - 3
src/background/sync/index.js

@@ -248,7 +248,7 @@ var sync = function () {
             _this.authState.set('error');
           }
           _this.syncState.set('idle');
-          _this.config.setOption('enabled', false);
+          // _this.config.setOption('enabled', false);
         } else {
           _this.authState.set('unauthorized');
         }
@@ -300,8 +300,10 @@ var sync = function () {
           }
           xhr.timeout = 10 * 1000;
           xhr.onload = function () {
-            if (this.status > 300) reject(this);
-            else resolve(this.responseText);
+            if (!this.status || this.status > 300 || !this.responseText)
+              reject(this);
+            else
+              resolve(this.responseText);
           };
           xhr.onerror = function () {
             if (this.status === 503) {

+ 31 - 4
src/background/sync/onedrive.js

@@ -99,12 +99,21 @@ setTimeout(function () {
       var _this = this;
       return getMeta()
       .catch(function (res) {
-        if (res.status === 401) {
-          return _this.refreshToken().then(getMeta);
+        if (res.status === 404) {
+          var header = res.getResponseHeader('WWW-Authenticate') || '';
+          if (/"invalid_token"/.test(header)) {
+            return _this.refreshToken().then(getMeta);
+          } else {
+            return {};
+          }
         }
+        throw res;
       });
       function getMeta() {
-        return sync.BaseService.prototype.getMeta.call(_this);
+        return _this.get(_this.metaFile)
+        .then(function (data) {
+          return JSON.parse(data);
+        });
       }
     },
     list: function () {
@@ -121,7 +130,25 @@ setTimeout(function () {
     },
     get: function (path) {
       return this.request({
-        url: '/drive/special/approot:/' + encodeURIComponent(path) + ':/content',
+        url: '/drive/special/approot:/' + encodeURIComponent(path),
+      }).then(function (text) {
+        return JSON.parse(text);
+      }).then(function (data) {
+        var url = data['@content.downloadUrl'];
+        return new Promise(function (resolve, reject) {
+          var xhr = new XMLHttpRequest;
+          xhr.open('GET', url, true);
+          xhr.onload = function () {
+            resolve(xhr.responseText);
+          };
+          xhr.onerror = function () {
+            reject();
+          };
+          xhr.ontimeout = function () {
+            reject();
+          };
+          xhr.send();
+        });
       });
     },
     put: function (path, data) {