aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2013-07-16 11:07:54 +0100
committerStruan Donald <struan@exo.org.uk>2013-07-16 11:07:54 +0100
commit4b8f24a725eb40cd35e6af5420ad4e8f85ece8d5 (patch)
treea561c422db3d9c82893cb92f43e89ea069ae42d8
parentf04fd59a370153a7ab9f1c4b60bf04c58403890a (diff)
On android we access photos directly on the file system rather than in
their own app sandbox as on iOS so we need to copy files instead of moving them as otherwise we end up deleting people's photos which would be bad. As part of this we should avoid trying to copy or move a file on to itself as that fails so check that the source and destination files aren't the same. This should sort out the problems in #43.
-rw-r--r--src/js/files.js38
-rw-r--r--src/js/views/photo.js14
2 files changed, 51 insertions, 1 deletions
diff --git a/src/js/files.js b/src/js/files.js
index a3a6458..583011a 100644
--- a/src/js/files.js
+++ b/src/js/files.js
@@ -26,6 +26,25 @@
});
},
+ copyURI: 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 copyFile( fileEntry, directory );
+ });
+ },
+
deleteURI: function (uri) {
console.log('Deleting URI: ' + uri);
@@ -122,6 +141,25 @@
return move.promise();
}
+ function copyFile (src, dest, options) {
+
+ console.log( 'copying file ' + src.fullPath + ' to ' + dest.fullPath );
+
+ var copy = $.Deferred();
+
+ var destPath = dest.fullPath + '/' + src.name;
+ var srcPath = src.fullPath + '';
+ if ( srcPath === destPath ) {
+ console.log('not copying because files are the same');
+ copy.resolve( src );
+ } else {
+ console.log('paths differ so copying');
+ src.copyTo( dest, null, copy.resolve, copy.reject);
+ }
+
+ return copy.promise();
+ }
+
function getFileFromURI(uri) {
console.log( 'getting file from uri ' + uri );
diff --git a/src/js/views/photo.js b/src/js/views/photo.js
index 74f3933..0c308a8 100644
--- a/src/js/views/photo.js
+++ b/src/js/views/photo.js
@@ -40,7 +40,19 @@
},
addPhotoSuccess: function(imgURI) {
- var move = FMS.files.moveURI( imgURI );
+ console.log(imgURI);
+ var move;
+ // on iOS the photos go into a temp folder in the apps own filespace so we
+ // can move them, and indeed have to as the tmp space is cleaned out by the OS
+ // so draft reports might have their images removed. on android you access the
+ // images where they are stored on the filesystem so if you move, and then delete
+ // them, you are moving and deleting the only copy of them which is likely to be
+ // surprising and unwelcome so we copy them instead.
+ if ( FMS.isAndroid ) {
+ move = FMS.files.copyURI( imgURI );
+ } else {
+ move = FMS.files.moveURI( imgURI );
+ }
var that = this;
move.done( function( file ) {