[File] Unified handling of name/fullPath/nativeURL.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 6 Nov 2015 13:32:43 +0000 (14:32 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 9 Nov 2015 07:49:26 +0000 (08:49 +0100)
[Verification] Code compiles.

Change-Id: If2fadb394effbaca25bd677d7a7beba92f79971a
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/file/js/DirectoryEntry.js
src/file/js/DirectoryReader.js
src/file/js/Entry.js
src/file/js/requestFileSystem.js
src/file/js/resolveLocalFileSystemURI.js
src/file/js/rootsUtils.js

index 35c8d8a..ee53ec8 100644 (file)
@@ -56,12 +56,7 @@ cordova.define('cordova-plugin-file.tizen.DirectoryEntry', function(require, exp
         console.error('Error while resolving dir - already exist file');
         errorCallback && errorCallback(FileError.TYPE_MISMATCH_ERR);
       } else {
-        var result = {
-            'name' : dir.name,
-            'fullPath' : dir.fullPath,
-            'nativeURL' : dir.toURI()
-        };
-        successCallback && successCallback(result);
+        successCallback && successCallback(rootsUtils.createEntry(dir));
       }
     };
 
@@ -80,12 +75,7 @@ cordova.define('cordova-plugin-file.tizen.DirectoryEntry', function(require, exp
                   new_obj = dir.createFile(child_name);
                 }
 
-                var result = {
-                    'name' : new_obj.name,
-                    'fullPath' : new_obj.fullPath,
-                    'nativeURL' : new_obj.toURI()
-                };
-                successCallback && successCallback(result);
+                successCallback && successCallback(rootsUtils.createEntry(new_obj));
               },
               function () {
                 console.error('Error -  immediate parent does not exist');
index 94df89a..805e237 100644 (file)
@@ -30,13 +30,9 @@ module.exports = {
           f.listFiles(function(v) {
             var retVal = [];
             for (var i = 0; i < v.length; ++i) {
-              var obj = {};
+              var obj = rootsUtils.createEntry(v[i], rootsUtils.findFilesystem(v[i].toURI()).filesystemName);
               obj.isDirectory = v[i].isDirectory;
               obj.isFile = v[i].isFile;
-              obj.name = v[i].name;
-              obj.fullPath = v[i].fullPath;
-              obj.filesystemName = rootsUtils.findFilesystem(v[i].fullPath).filesystemName;
-              obj.nativeURL = v[i].toURI();
               retVal.push(obj);
             };
             successCallback(retVal);
index 488a9aa..870c411 100644 (file)
@@ -59,12 +59,8 @@ var changeFile = function(method, successCallback, errorCallback, args) {
                     tizen.filesystem.resolve(
                       destURL,
                       function (destFile) {
-                        var destEntry = {
-                            'isDirectory' : destFile.isDirectory,
-                            'name' : name,
-                            'fullPath' : destFile.fullPath,
-                            'nativeURL' : destFile.toURI()
-                        };
+                        var destEntry = rootsUtils.createEntry(destFile);
+                        destEntry.isDirectory = destFile.isDirectory;
                         successCallback && successCallback(destEntry);
                       }, function (err) {
                         console.error('Error - resolve result entry failed');
@@ -150,12 +146,7 @@ module.exports = {
 
     resolveParent(url, errorCallback,
         function(srcFile, parentDir){
-          var resultEntry = {
-              'name' : srcFile.parent.name,
-              'fullPath' : srcFile.parent.fullPath,
-              'nativeURL' : srcFile.parent.toURI()
-          };
-          successCallback && successCallback(resultEntry);
+          successCallback && successCallback(rootsUtils.createEntry(srcFile.parent));
         }
     );
   }
index 66d12d0..460fdd7 100644 (file)
@@ -21,30 +21,34 @@ cordova.define('cordova-plugin-file.tizen.requestFileSystem', function(require,
 module.exports = {
   requestFileSystem: function(successCallback, errorCallback, args) {
     var type = args[0];
+    var fsName;
 
-    //TEMPORARY = 0 PERSISTENT = 1 cordova layer checks only if type is less than 0
-    if (type >= 2) {
-      errorCallback && errorCallback(FileError.TYPE_MISMATCH_ERR);
-      return;
-    }
-
-    var path = type === LocalFileSystem.PERSISTENT ?
-        cordova.file.dataDirectory : cordova.file.cacheDirectory;
+    switch(type) {
+      case LocalFileSystem.TEMPORARY:
+        fsName = 'temporary';
+        break;
 
-    var filesystem = rootsUtils.findFilesystem(path.substr(0, path.length-1));
+      case LocalFileSystem.PERSISTENT:
+        fsName = 'persistent';
+        break;
 
-    if (filesystem.filesystemName !== 'temporary' && filesystem.filesystemName !== 'persistent') {
-      errorCallback && errorCallback(FileError.NOT_FOUND_ERR);
-      return;
+      default:
+        console.error('Unknown FS type: ' + type);
+        errorCallback && errorCallback(FileError.TYPE_MISMATCH_ERR);
+        return;
     }
 
-    var root = {
-      'name' : filesystem.name,
-      'fullPath' : filesystem.fullPath,
-      'nativeURL' : filesystem.nativeURL,
-    };
+    rootsUtils.getRoots(function(roots) {
+      for (var i = 0; i < roots.length; ++i) {
+        if (fsName === roots[i].filesystemName) {
+          successCallback({ 'name': fsName, 'root': roots[i] });
+          return;
+        }
+      }
 
-    successCallback({'name' : filesystem.filesystemName, 'root' : root});
+      console.error('Filesystem not found: ' + fsName);
+      errorCallback && errorCallback(FileError.NOT_FOUND_ERR);
+    });
   }
 };
 
index f6830cc..3faa520 100644 (file)
@@ -25,13 +25,9 @@ module.exports = {
     function onResolve(file) {
       var filesystem = rootsUtils.findFilesystem(file.toURI());
 
-      var entry = {
-        'filesystemName' : filesystem.filesystemName,
-        'name' : file.name,
-        'fullPath' : file.fullPath,
-        'isDirectory' : file.isDirectory,
-        'nativeURL' : file.toURI(),
-      };
+      var entry = rootsUtils.createEntry(file, filesystem.filesystemName);
+      entry.isDirectory = file.isDirectory;
+
       successCallback(entry);
     }
 
index e8eb953..a8c81db 100644 (file)
  */
 
 var rootsUtils = (function() {
+  var uriPrefix = 'file://';
+
+  function stripTrailingSlash(str) {
+    if ('/' === str.substr(-1)) {
+      return str.substr(0, str.length - 1);
+    }
+    return str;
+  }
+
+  function getName(uri) {
+    return stripTrailingSlash(uri).replace(/^.*(\\|\/|\:)/, '');
+  }
+
+  function getFullPath(uri) {
+    if (0 === uri.indexOf(uriPrefix)) {
+      uri = uri.substr(uriPrefix.length);
+    }
+    return stripTrailingSlash(uri);
+  }
+
+  function getNativeUrl(uri) {
+    return stripTrailingSlash(uri);
+  }
+
+  function createEntry(file, fsName) {
+    var uri = file.toURI();
+    return {
+      name: getName(uri),
+      fullPath: getFullPath(uri),
+      nativeURL: getNativeUrl(uri),
+      filesystemName: fsName
+    };
+  }
+
   var roots_to_resolve = [
     {
       filesystemName: 'temporary',
@@ -30,20 +64,21 @@ var rootsUtils = (function() {
     }
   ];
 
+  var rootDirUri = 'file:///';
+
   var roots = [
     {
       filesystemName: 'root',
-      name: '',
-      fullPath: '/',
-      nativeURL: '/'
+      name: getName(rootDirUri),
+      fullPath: getFullPath(rootDirUri),
+      nativeURL: getNativeUrl(rootDirUri)
     }
   ];
 
   function getRoots(successCallback) {
     if (roots_to_resolve.length > 0) {
       tizen.filesystem.resolve(roots_to_resolve[0].nativeURL, function(dir) {
-        roots_to_resolve[0].fullPath = dir.toURI().substr(7);
-        roots_to_resolve[0].name = roots_to_resolve[0].fullPath.replace(/^.*(\\|\/|\:)/, '');  // extract name of the directory
+        roots_to_resolve[0] = createEntry(dir, roots_to_resolve[0].filesystemName);
         roots.push(roots_to_resolve[0]);
         roots_to_resolve.splice(0, 1); // remove first item
 
@@ -65,24 +100,11 @@ var rootsUtils = (function() {
     return ((str1 === str2) ? 0 : ((str1 > str2) ? 1 : -1 ));
   }
 
-  function findFilesystem(url) {
-    for (var i = roots.length - 1; i >= 0; i--) {
-      if (url.indexOf('file://') == 0) {
-        url = url.substr(7);
-      }
-
-      if (url.charAt(0) == '/') {
-        if (url == roots[i].fullPath) {
-          return roots[i];
-        }
-
-        if (strncmp(url, roots[i].fullPath + '/', roots[i].fullPath.length + 1) == 0) {
-          return roots[i];
-        }
-      } else {
-        if (url == roots[i].nativeURL) {
-          return roots[i];
-        }
+  function findFilesystem(uri) {
+    var fullPath = getFullPath(uri);
+    for (var i = roots.length - 1; i > 0; --i) {
+      if (0 === strncmp(fullPath, roots[i].fullPath, roots[i].fullPath.length)) {
+        return roots[i];
       }
     }
 
@@ -91,6 +113,11 @@ var rootsUtils = (function() {
 
   return {
     getRoots: getRoots,
-    findFilesystem: findFilesystem
+    findFilesystem: findFilesystem,
+    getName: getName,
+    getFullPath: getFullPath,
+    getNativeUrl: getNativeUrl,
+    stripTrailingSlash: stripTrailingSlash,
+    createEntry: createEntry
   };
 })();