[Filesystem] File.moveTo JS part
authorPawel Kaczmarek <p.kaczmarek3@samsung.com>
Fri, 27 Feb 2015 09:31:58 +0000 (10:31 +0100)
committerRafal Galka <r.galka@samsung.com>
Tue, 3 Mar 2015 13:32:57 +0000 (22:32 +0900)
[Verification]
var documentsDir;
 function onsuccess(files) {
   for (var i = 0; i < files.length; i++) {
     if (files[i].isDirectory == false) {
       documentsDir.moveTo(files[i].fullPath,
                           "images/newDir/" + files[i].name,
                           false,
                           function() {console.log("file moved");});
     }
   }
 }

 function onerror(error) {
   console.log("The error " + error.message +
               " occurred during listing the files in the selected folder");
 }

 tizen.filesystem.resolve(
     'documents',
     function(dir) {
       documentsDir = dir;
       dir.listFiles(onsuccess, onerror);
     }, function(e) {
       console.log("Error" + e.message);
     }, "rw"
 );

//should move all files from documents dir to images/newDir

Change-Id: If02186dd2e01bfa92d947f1ad43bbb1544e54ad6
Signed-off-by: Pawel Kaczmarek <p.kaczmarek3@samsung.com>
src/filesystem/js/file.js

index 6e2299ad167eb10562a50fd98aef776db23ce60d..747450bafd679c530ca3ac1b50b1f60bc0a383b8 100644 (file)
@@ -330,10 +330,60 @@ File.prototype.moveTo = function(originFilePath, destinationFilePath, overwrite,
     {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true}
   ]);
 
+  if (this.isFile) {
+    setTimeout(function() {
+      native_.callIfPossible(args.onerror,
+          new tizen.WebAPIException(tizen.WebAPIException.IO_ERR,
+          'File object which call this method is not directory'));
+    }, 0);
+    return;
+  }
+
+  var _realOriginalPath = commonFS_.toRealPath(args.originFilePath);
+  var _realDestinationPath = commonFS_.toRealPath(args.destinationFilePath);
+
+  var resultOldPath = native_.callSync('File_statSync', {location: _realOriginalPath});
+  if (native_.isFailure(resultOldPath)) {
+    setTimeout(function() {
+      native_.callIfPossible(args.onerror,
+          new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR,
+          'Source file is not avalaible'));
+    }, 0);
+    return;
+  }
+
+  if (!args.overwrite) {
+    var resultNewPath = native_.callSync('File_statSync', {location: _realDestinationPath});
+    if (native_.isSuccess(resultNewPath)) {
+      setTimeout(function() {
+        native_.callIfPossible(args.onerror,
+            new tizen.WebAPIException(tizen.WebAPIException.IO_ERR, 'Overwrite is not allowed'));
+      }, 0);
+      return;
+    }
+  }
+
+  if (!commonFS_.f_isSubDir(_realOriginalPath, this.fullPath)) {
+    var m1 = 'Source file should be subdirectory of: ' + this.fullPath;
+    setTimeout(function() {
+      native_.callIfPossible(args.onerror,
+          new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, m1));
+    }, 0);
+    return;
+  }
+
+  if (this.mode === 'r' || !commonFS_.isLocationAllowed(_realDestinationPath)) {
+    var m2 = 'Source/Destination is read only folder: ' + this.fullPath;
+    setTimeout(function() {
+      native_.callIfPossible(args.onerror,
+          new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, m2));
+    }, 0);
+    return;
+  }
+
   var data = {
-    originFilePath: args.originFilePath,
-    destinationFilePath: args.destinationFilePath,
-    overwrite: args.overwrite
+    oldPath: _realOriginalPath,
+    newPath: _realDestinationPath
   };
 
   var callback = function(result) {
@@ -344,7 +394,7 @@ File.prototype.moveTo = function(originFilePath, destinationFilePath, overwrite,
     native_.callIfPossible(args.onsuccess);
   };
 
-  native_.call('File_moveTo', data, callback);
+  native_.call('File_rename', data, callback);
 };
 
 File.prototype.createDirectory = function(dirPath) {