Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding FS caching

  • Loading branch information...
commit d24cba4d97aaf6a87173844840e43c93db679b32 1 parent a823c04
@ebidel ebidel authored
Showing with 113 additions and 20 deletions.
  1. +110 −17 gdocs/js/app.js
  2. +2 −2 gdocs/js/gdocs.js
  3. +1 −1  gdocs/js/resumableupload.js
View
127 gdocs/js/app.js
@@ -18,25 +18,98 @@ Author: Eric Bidelman (ericbidelman@chromium.org)
var gdocs = new GDocs();
+// FILESYSTEM SUPPORT ----------------------------------------------------------
+var fs = null;
+var FOLDERNAME = 'test';
+
+function writeFile(blob) {
+ if (!fs) {
+ return;
+ }
+
+ fs.root.getDirectory(FOLDERNAME, {create: true}, function(dirEntry) {
+ dirEntry.getFile(blob.name, {create: true, exclusive: false}, function(fileEntry) {
+ // Create a FileWriter object for our FileEntry, and write out blob.
+ fileEntry.createWriter(function(fileWriter) {
+ fileWriter.onerror = onError;
+ fileWriter.onwriteend = function(e) {
+ console.log('Write completed.');
+ };
+ fileWriter.write(blob);
+ }, onError);
+ }, onError);
+ }, onError);
+}
+// -----------------------------------------------------------------------------
+
function upload(blob) {
gdocs.upload(blob, function() {
- //...
+ console.log('Upload complete');
});
}
function onError(e) {
- console.error(e);
+ console.log(e);
}
+// Main Angular controller for app.
function DocsController($scope, $http) {
$scope.docs = [];
$scope.fetchDocs = function() {
$scope.docs = []; // Clear out old results.
- var successCallback = function(resp, status, headers, config) {
+ var informAngular = function(doc, i, totalEntries) {
+ $scope.docs.push(doc);
+
+ // Only want to sort and call $apply() when we have all entries.
+ if (totalEntries - 1 == i) {
+ $scope.docs.sort(Util.sortByDate);
+ $scope.$apply(); // Inform angular that we made changes.
+ }
+ };
+
+ // Response handler that doesn't cache file icons.
+ // var successCallback = function(resp, status, headers, config) {
+ // var docs = [];
+
+ // var totalEntries = resp.feed.entry.length;
+
+ // resp.feed.entry.forEach(function(entry, i) {
+ // var doc = {
+ // title: entry.title.$t,
+ // updatedDate: Util.formatDate(entry.updated.$t),
+ // updatedDateFull: entry.updated.$t,
+ // icon: gdocs.getLink(entry.link,
+ // 'http://schemas.google.com/docs/2007#icon').href,
+ // alternateLink: gdocs.getLink(entry.link, 'alternate').href,
+ // size: entry.docs$size ? '( ' + entry.docs$size.$t + ' bytes)' : null
+ // };
+
+ // var xhr = new XMLHttpRequest();
+ // xhr.open('GET', doc.icon, true);
+ // xhr.responseType = 'blob';
+ // xhr.onerror = onError;
+ // xhr.onload = function(e) {
+ // console.log('Fetched icon via XHR');
+
+ // doc.icon = window.URL.createObjectURL(this.response);
+
+ // informAngular(doc, i, totalEntries);
+ // };
+
+ // xhr.send();
+
+ // });
+ // };
+
+
+ // Response handler that caches file icons int he filesystem API.
+ var successCallbackWithFsCaching = function(resp, status, headers, config) {
var docs = [];
+ var totalEntries = resp.feed.entry.length;
+
resp.feed.entry.forEach(function(entry, i) {
var doc = {
title: entry.title.$t,
@@ -48,22 +121,36 @@ function DocsController($scope, $http) {
size: entry.docs$size ? '( ' + entry.docs$size.$t + ' bytes)' : null
};
- var xhr = new XMLHttpRequest();
- xhr.open('GET', doc.icon, true);
- xhr.responseType = 'blob';
- xhr.onerror = onError;
- xhr.onload = function(e) {
- doc.icon = window.URL.createObjectURL(this.response);
+ // 'http://gstatic.google.com/doc_icon_128.png' -> 'doc_icon_128.png'
+ doc.iconFilename = doc.icon.substring(doc.icon.lastIndexOf('/') + 1);
- $scope.docs.push(doc);
+ var fsURL = fs.root.toURL() + FOLDERNAME + '/' + doc.iconFilename;
+ window.webkitResolveLocalFileSystemURL(fsURL, function(entry) {
+ doc.icon = entry.toURL(); // should be === to fsURL, but whatevs.
- if (resp.feed.entry.length - 1 == i) {
- $scope.docs.sort(Util.sortByDate);
- $scope.$apply(); // Inform angular that we made changes.
- }
- };
+ informAngular(doc, i, totalEntries);
+ }, function(e) {
+ // Error: file doesn't exist yet. XHR it in and write it to the FS.
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', doc.icon, true);
+ xhr.responseType = 'blob';
+ xhr.onerror = onError;
+ xhr.onload = function(e) {
+ console.log('Fetched icon via XHR');
- xhr.send();
+ var blob = e.target.response;
+ blob.name = doc.iconFilename; // Add icon filename to blob.
+
+ writeFile(blob); // Write is async, but that's ok.
+
+ doc.icon = window.URL.createObjectURL(blob);
+
+ informAngular(doc, i, totalEntries);
+ };
+
+ xhr.send();
+
+ });
});
};
@@ -75,7 +162,7 @@ function DocsController($scope, $http) {
}
};
- $http.get(gdocs.DOCLIST_FEED, config).success(successCallback);
+ $http.get(gdocs.DOCLIST_FEED, config).success(successCallbackWithFsCaching);
};
gdocs.auth(function() {
@@ -104,4 +191,10 @@ document.addEventListener('DOMContentLoaded', function(e) {
closeButton.addEventListener('click', function(e) {
window.close();
});
+
+ // FILESYSTEM SUPPORT --------------------------------------------------------
+ window.webkitRequestFileSystem(TEMPORARY, 1024 * 1024, function(localFs) {
+ fs = localFs;
+ }, onError);
+ // ---------------------------------------------------------------------------
});
View
4 gdocs/js/gdocs.js
@@ -156,8 +156,8 @@ GDocs.prototype.upload = function(blob, callback) {
resumableMediaLink: this.CREATE_SESSION_URI/*,entry: entry*/
}, function(response) {
var entry = JSON.parse(response).entry;
- console.log(entry, entry.docs$filename.$t, entry.docs$size.$t);
- self.getDocumentList();
+console.log(entry, entry.docs$filename.$t, entry.docs$size.$t);
+ self.getDocumentList(null, callback);
});
};
View
2  gdocs/js/resumableupload.js
@@ -140,7 +140,7 @@ console.log(headers['Content-Range']);
console.log(startByte, startByte + chunkSize_);
- var chunk = this.file.webkitSlice(
+ var chunk = this.file.slice(
startByte, startByte + chunkSize_, this.file.type);
console.log(chunk, chunk.size);
Please sign in to comment.
Something went wrong with that request. Please try again.