cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [1/3] git commit: Add createFileWriter and createTmpFileUrl to ResourcesLoader
Date Thu, 15 May 2014 17:30:54 GMT
Repository: cordova-app-harness
Updated Branches:
  refs/heads/master 5319e5dea -> b826b671c


Add createFileWriter and createTmpFileUrl to ResourcesLoader


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/47e5c721
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/47e5c721
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/47e5c721

Branch: refs/heads/master
Commit: 47e5c721541a69723d8944b316d1f365ef2aa313
Parents: 5319e5d
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Thu May 15 11:32:06 2014 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu May 15 11:32:06 2014 -0400

----------------------------------------------------------------------
 www/cdvah/js/ResourcesLoader.js | 53 ++++++++++++++++++++++--------------
 www/cdvah/js/app.js             |  8 ++++--
 2 files changed, 38 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/47e5c721/www/cdvah/js/ResourcesLoader.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/ResourcesLoader.js b/www/cdvah/js/ResourcesLoader.js
index 6151672..9424fed 100644
--- a/www/cdvah/js/ResourcesLoader.js
+++ b/www/cdvah/js/ResourcesLoader.js
@@ -2,7 +2,7 @@
 (function() {
     'use strict';
 
-    myApp.factory('ResourcesLoader', ['$q', '$window', '$http', function($q, $window, $http)
{
+    myApp.factory('ResourcesLoader', ['$q', '$window', '$http', 'TEMP_DIR', function($q,
$window, $http, TEMP_DIR) {
         function resolveURL(url) {
             var d = $q.defer();
             $window.resolveLocalFileSystemURL(url, d.resolve, d.reject);
@@ -64,41 +64,50 @@
             return helper(targetUrl);
         }
 
-        function createFileEntry(url) {
+        function createFileWriter(url, append) {
             var rootUrl = dirName(url);
             return ensureDirectoryExists(rootUrl)
             .then(function(dirEntry) {
                 var path = decodeURI(baseName(url));
                 return getFilePromisified(dirEntry, path, {create: true});
+            }).then(function(fileEntry) {
+                var deferred = $q.defer();
+                function gotWriter(writer) {
+                    if (!append && writer.length > 0) {
+                        writer.onwrite = function() {
+                            if (writer.length === 0) {
+                                writer.onwriteend = null;
+                                writer.onerror = null;
+                                deferred.resolve(writer);
+                            }
+                        };
+                        writer.onerror = deferred.reject;
+                        writer.truncate(0);
+                    } else {
+                        deferred.resolve(writer);
+                    }
+                }
+                fileEntry.createWriter(gotWriter, deferred.reject);
+                return deferred.promise;
             });
         }
 
         function writeToFile(url, contents, append) {
-            return createFileEntry(url)
-            .then(function(fileEntry){
+            return createFileWriter(url, append)
+            .then(function(writer) {
                 var deferred = $q.defer();
-
-                var errorGettingFileWriter = function(error) {
-                    var str = 'There was an error writing the file.' + JSON.stringify(error);
-                    deferred.reject(new Error(str));
-                };
-
-                var gotFileWriter = function(writer) {
-                    writer.onwrite = deferred.resolve;
-                    writer.onerror = function(evt) {
-                        deferred.reject(new Error(evt));
-                    };
-                    if(append){
-                        writer.seek(writer.length);
-                    }
-                    writer.write(contents);
-                };
-                fileEntry.createWriter(gotFileWriter, errorGettingFileWriter);
+                writer.onwrite = deferred.resolve;
+                writer.onerror = deferred.reject;
+                writer.write(contents);
                 return deferred.promise;
             });
         }
 
         return {
+            createTmpFileUrl: function(extension) {
+                return TEMP_DIR + Math.floor(Math.random()* 100000000) + (extension || '');
+            },
+
             doesFileExist: function(url){
                 return resolveURL(url).then(function() { return true; }, function() { return
false; });
             },
@@ -136,6 +145,8 @@
                 });
             },
 
+            createFileWriter: createFileWriter,
+
             writeFileContents: function(url, contents) {
                 return writeToFile(url, contents, false /* append */);
             },

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/47e5c721/www/cdvah/js/app.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/app.js b/www/cdvah/js/app.js
index 51467f5..1f4b52b 100644
--- a/www/cdvah/js/app.js
+++ b/www/cdvah/js/app.js
@@ -35,7 +35,11 @@ document.addEventListener('deviceready', function() {
     cordova.filesystem.getDataDirectory(false, function(dirEntry) {
         myApp.value('INSTALL_DIRECTORY', dirEntry.toURL() + 'apps');
         myApp.value('APPS_JSON', dirEntry.toURL() + 'apps.json');
-
-        angular.bootstrap(document, ['CordovaAppHarness']);
+        window.requestFileSystem(window.TEMPORARY, 1 * 1024 * 1024, function(fs) {
+            myApp.value('TEMP_DIR', fs.root.toURL());
+            angular.bootstrap(document, ['CordovaAppHarness']);
+        }, function() {
+            console.error('Failed to get temporary FS');
+        });
     });
 }, false);


Mime
View raw message