aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/files.js
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2013-07-12 09:47:56 +0100
committerStruan Donald <struan@exo.org.uk>2013-07-12 11:00:11 +0100
commit25e3a0c94551b0faac3f16e0598e6f4bdffcf7d8 (patch)
treef78f68ba8ed39e5b5004e41435aac3e9bbe9c044 /src/js/files.js
parent4c2206c97250b7f72eb04d13ae886a7fb4f4086a (diff)
instead of using js to inject the correct cordova js file in to
index.html restructure things so that the common files are a level down and the platofrm specific ones are directly placed in the relevant project. This both makes for less fuss and also avoids the error with Android < v3 instantiating cordova twice. Note that the iOS common assets are included by a build script rather than a symlink as symlinking doesn't seem to agree with Xcode
Diffstat (limited to 'src/js/files.js')
-rw-r--r--src/js/files.js127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/js/files.js b/src/js/files.js
new file mode 100644
index 0000000..b8aed05
--- /dev/null
+++ b/src/js/files.js
@@ -0,0 +1,127 @@
+/**
+ * Things to do with Phonegap files
+ * Mainly wrappers around their async functions to return Promises
+ */
+
+ (function (FMS, Backbone, _, $) {
+ _.extend(FMS, {
+ files: {
+ // move a file at from a uri to a desination directory. maintains file name
+ moveURI: function (srcURI, dest ) {
+
+ var fileEntry;
+ return getFileFromURI(srcURI)
+ .pipe( function (file) {
+ fileEntry = file;
+
+ return getFileSystem();
+ })
+ .pipe( function (filesystem) {
+ console.log('Filesystem returned: ' + filesystem);
+
+ return getDirectory(filesystem.root, CONFIG.FILES_DIR, {create: true});
+ })
+ .pipe( function(directory) {
+ return moveFile( fileEntry, directory );
+ });
+ },
+
+ deleteURI: function (uri) {
+ console.log('Deleting URI: ' + uri);
+
+ return getFileFromURI(uri)
+ .pipe( function (file) {
+ var deletingFile = $.Deferred();
+ file.remove(deletingFile.resolve, deletingFile.reject);
+ return deletingFile;
+ });
+
+ },
+
+ // Delete a file from the filesystem
+ deleteFile: function (path) {
+
+ console.log('Deleting file: ' + path);
+
+ // Turn path into a filename
+ var filename = path.split('/').pop();
+
+ return getFileSystem()
+ .pipe(function (filesystem) {
+ return getDirectory(filesystem.root, CONFIG.FILES_DIR);
+ })
+ .pipe(function (directory) {
+ return getFile(directory, filename, {});
+ })
+ .pipe(function (file) {
+ var deletingFile = $.Deferred();
+ file.remove(deletingFile.resolve, deletingFile.reject);
+ return deletingFile;
+ });
+
+ }
+ }
+ });
+
+ // Wrap the async Phonegap way of getting a filesystem in a promise
+ function getFileSystem() {
+
+ console.log('Getting the file system');
+
+ var filesystem = $.Deferred();
+
+ window.requestFileSystem(
+ LocalFileSystem.PERSISTENT,
+ 0,
+ filesystem.resolve,
+ filesystem.reject );
+
+ return filesystem.promise();
+ }
+
+ // Wrap the async Phonegap way of getting a directory in a promise
+ function getDirectory (rootDirectory, path, options) {
+
+ console.log('Getting a directory: ' + path);
+
+ var directory = $.Deferred();
+
+ rootDirectory.getDirectory(path, options, directory.resolve, directory.reject);
+
+ return directory.promise();
+ }
+
+ // Wrap the async Phonegap way of getting a file in a promise
+ function getFile (directory, path, options) {
+
+ console.log('Getting a file with path: ' + path + ' in directory: ' + directory.fullPath);
+
+ var file = $.Deferred();
+
+ directory.getFile(path, options, file.resolve, file.reject);
+
+ return file.promise();
+ }
+
+ function moveFile (src, dest, options) {
+
+ console.log( 'moveing file ' + src.fullPath + ' to ' + dest.fullPath );
+
+ var move = $.Deferred();
+
+ src.moveTo( dest, null, move.resolve, move.reject);
+
+ return move.promise();
+ }
+
+ function getFileFromURI(uri) {
+
+ console.log( 'getting file from uri ' + uri );
+
+ var file = $.Deferred();
+
+ window.resolveLocalFileSystemURI( uri, file.resolve, file.reject);
+
+ return file.promise();
+ }
+})(FMS, Backbone, _, $);