aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/files.js
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2014-10-03 15:07:08 +0100
committerDave Arter <davea@mysociety.org>2015-08-28 14:27:15 +0100
commita69d425c0e5c004145ac1ab70e2f7f9fc329b54c (patch)
tree8eb7ff0ff7aaa0fce8bc7214ec6e15b694fddbdc /www/js/files.js
parent8fd15b58733c51d7f001f9eac66b7d03830ec0b4 (diff)
update Android to Cordova 3.6
Required due to security issue Remove Android directory as no longer required, move src -> www to match standard layout, update .gitignore to avoid including the standard platform files, update README based on Steve's zurich one
Diffstat (limited to 'www/js/files.js')
-rw-r--r--www/js/files.js173
1 files changed, 173 insertions, 0 deletions
diff --git a/www/js/files.js b/www/js/files.js
new file mode 100644
index 0000000..eea38c3
--- /dev/null
+++ b/www/js/files.js
@@ -0,0 +1,173 @@
+/**
+ * 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, newName ) {
+
+ var fileEntry;
+ return getFileFromURI(srcURI)
+ .pipe( function (file) {
+ fileEntry = file;
+
+ return getFileSystem();
+ })
+ .pipe( function (filesystem) {
+ FMS.printDebug('Filesystem returned: ' + filesystem);
+
+ return getDirectory(filesystem.root, CONFIG.FILES_DIR, {create: true});
+ })
+ .pipe( function(directory) {
+ return moveFile( fileEntry, directory, newName );
+ });
+ },
+
+ copyURI: function (srcURI, newName ) {
+
+ var fileEntry;
+ return getFileFromURI(srcURI)
+ .pipe( function (file) {
+ fileEntry = file;
+
+ return getFileSystem();
+ })
+ .pipe( function (filesystem) {
+ FMS.printDebug('Filesystem returned: ' + filesystem);
+
+ return getDirectory(filesystem.root, CONFIG.FILES_DIR, {create: true});
+ })
+ .pipe( function(directory) {
+ return copyFile( fileEntry, directory, newName );
+ });
+ },
+
+ deleteURI: function (uri) {
+ FMS.printDebug('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) {
+
+ FMS.printDebug('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() {
+
+ FMS.printDebug('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) {
+
+ FMS.printDebug('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) {
+
+ FMS.printDebug('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, newName) {
+
+ FMS.printDebug( 'moveing file ' + src.fullPath + ' to ' + dest.fullPath );
+
+ var move = $.Deferred();
+
+ var destPath = dest.fullPath + '/' + src.name;
+ var srcPath = src.fullPath + '';
+ if ( srcPath === destPath ) {
+ FMS.printDebug('not moving because files are the same');
+ move.resolve( src );
+ } else {
+ FMS.printDebug('paths differ so moving');
+ src.moveTo( dest, newName, move.resolve, move.reject);
+ }
+
+ return move.promise();
+ }
+
+ function copyFile (src, dest, newName) {
+
+ FMS.printDebug( 'copying file ' + src.fullPath + ' to ' + dest.fullPath );
+
+ var copy = $.Deferred();
+
+ var destPath = dest.fullPath + '/' + src.name;
+ var srcPath = src.fullPath + '';
+ if ( srcPath === destPath ) {
+ FMS.printDebug('not copying because files are the same');
+ copy.resolve( src );
+ } else {
+ FMS.printDebug('paths differ so copying');
+ src.copyTo( dest, newName, copy.resolve, copy.reject);
+ }
+
+ return copy.promise();
+ }
+
+ function getFileFromURI(uri) {
+
+ FMS.printDebug( 'getting file from uri ' + uri );
+
+ var file = $.Deferred();
+
+ window.resolveLocalFileSystemURI( uri, file.resolve, file.reject);
+
+ return file.promise();
+ }
+})(FMS, Backbone, _, $);