aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2013-04-10 16:58:50 +0100
committerStruan Donald <struan@exo.org.uk>2013-04-10 19:28:10 +0100
commitda4f061cc3b364b828be0aa43b8e7702b7db4eb3 (patch)
treea752a1cd3fde6ee4782b52ef17031e920c5b34d5
parentae76aa0be43c4939942318deaa3772537386bb30 (diff)
when we take a photo copy it from the tmp location to somewhere we
specify so that it doesn't get deleted automatically. likewise when we delete a photo delete it from out storage
-rw-r--r--www/index.html1
-rw-r--r--www/js/files.js127
-rw-r--r--www/js/views/photo.js41
3 files changed, 154 insertions, 15 deletions
diff --git a/www/index.html b/www/index.html
index 4f47a89..a6a6717 100644
--- a/www/index.html
+++ b/www/index.html
@@ -34,6 +34,7 @@
<script type="text/javascript" src="js/locate.js"></script>
<script type="text/javascript" src="js/strings.js"></script>
+ <script type="text/javascript" src="js/files.js"></script>
<script type="text/javascript" src="js/views/fms.js"></script>
<script type="text/javascript" src="js/views/home.js"></script>
diff --git a/www/js/files.js b/www/js/files.js
new file mode 100644
index 0000000..b8aed05
--- /dev/null
+++ b/www/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, _, $);
diff --git a/www/js/views/photo.js b/www/js/views/photo.js
index 4171e6a..8d4b20e 100644
--- a/www/js/views/photo.js
+++ b/www/js/views/photo.js
@@ -27,15 +27,20 @@
},
addPhotoSuccess: function(imgURI) {
- //TODO move this file somewhere permanent rather than temp
- // storage so it doesn't get randomly cleaned up by the OS
- $('#photo').attr('src', imgURI );
- this.model.set('file', imgURI);
- FMS.saveCurrentDraft();
-
- $('#photo-next-btn .ui-btn-text').text('Next');
- $('#display_photo').show();
- $('#add_photo').hide();
+ var move = FMS.files.moveURI( imgURI );
+
+ var that = this;
+ move.done( function( file ) {
+ $('#photo').attr('src', file.toURL());
+ that.model.set('file', file.toURL());
+ FMS.saveCurrentDraft();
+
+ $('#photo-next-btn .ui-btn-text').text('Next');
+ $('#display_photo').show();
+ $('#add_photo').hide();
+ });
+
+ move.fail( function() { that.addPhotoFail(); } );
},
addPhotoFail: function() {
@@ -47,13 +52,19 @@
},
deletePhoto: function() {
- this.model.set('file', '');
- FMS.saveCurrentDraft();
- $('#photo').attr('src', '');
+ var that = this;
+ var del = FMS.files.deleteURI( this.model.get('file') );
+
+ del.done( function() {
+ that.model.set('file', '');
+ FMS.saveCurrentDraft();
+ $('#photo').attr('src', '');
+
+ $('#photo-next-btn .ui-btn-text').text('Skip');
+ $('#display_photo').hide();
+ $('#add_photo').show();
+ });
- $('#photo-next-btn .ui-btn-text').text('Skip');
- $('#display_photo').hide();
- $('#add_photo').show();
}
})
});