From: Andrzej Popowski Date: Mon, 2 Nov 2015 12:04:15 +0000 (+0100) Subject: [Filesystem] - common errors and findFilesystem function X-Git-Tag: submit/tizen/20151221.111205^2~64^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=96cfe384901cbb3a1afd7275e98ecdbab0880f5e;p=platform%2Fcore%2Fapi%2Fcordova-plugins.git [Filesystem] - common errors and findFilesystem function Change-Id: Id5f732a8d78b656f6f0bdb17b651575cc3d48265 Signed-off-by: Andrzej Popowski --- diff --git a/src/file/cordova_file_api.js b/src/file/cordova_file_api.js index 0df4c7a..d28e754 100755 --- a/src/file/cordova_file_api.js +++ b/src/file/cordova_file_api.js @@ -14,6 +14,8 @@ * limitations under the License. */ +//= require('Errors.js'); +//= require('rootsUtils.js'); //= require('DirectoryEntry.js'); //= require('DirectoryReader.js'); //= require('Entry.js'); diff --git a/src/file/js/Errors.js b/src/file/js/Errors.js new file mode 100644 index 0000000..a0e71b0 --- /dev/null +++ b/src/file/js/Errors.js @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Function converting a tizen error to a cordova error + * + * {unsigned short} WebAPIError error code + */ +function ConvErrorCode(err_code) { + switch (err_code) { + case WebAPIException.INVALID_VALUES_ERR: + return FileError.ENCODING_ERR; + + case WebAPIException.NOT_FOUND_ERR: + return FileError.NOT_FOUND_ERR; + + default: + return FileError.ENCODING_ERR; + } +} diff --git a/src/file/js/fileSystems-roots.js b/src/file/js/fileSystems-roots.js index 6b85f63..86229f8 100644 --- a/src/file/js/fileSystems-roots.js +++ b/src/file/js/fileSystems-roots.js @@ -18,59 +18,18 @@ cordova.define('cordova-plugin-file.tizen.fileSystems-roots', function(require, exports, module) { // TODO: remove -> end -var info = (function() { - var roots_to_resolve = [ - { - filesystemName: 'temporary', - name: '', - fullPath: '', - nativeURL: 'wgt-private-tmp' - }, - { - filesystemName: 'persistent', - name: '', - fullPath: '', - nativeURL: 'wgt-private' - } - ]; +var channel = require('cordova/channel'); - var roots = [ - { - filesystemName: 'root', - name: '', - fullPath: 'file:///', - nativeURL: '/' - } - ]; - - 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(); - roots_to_resolve[0].name = roots_to_resolve[0].fullPath.replace(/^.*(\\|\/|\:)/, ''); // extract name of the directory - roots.push(roots_to_resolve[0]); - roots_to_resolve.splice(0, 1); // remove first item - - // we've resolved one root, check if there are any other - getRoots(successCallback); - }, function(e) { - console.error(e); - // in case of an error, return the roots we have so far - successCallback(roots); - }); - } else { - successCallback(roots.slice()); - } - } - - return { - getRoots: getRoots - }; -})(); +channel.waitForInitialization('onGetRootsReady'); +channel.onCordovaReady.subscribe(function() { + rootsUtils.getRoots(function () { + channel.initializationComplete('onGetRootsReady'); + }); +}); module.exports = { - requestAllFileSystems: function(successCallback, errorCallback, args) { - info.getRoots(successCallback); + requestAllFileSystems: function(successCallback, errorCallback, args) { + rootsUtils.getRoots(successCallback); } }; diff --git a/src/file/js/rootsUtils.js b/src/file/js/rootsUtils.js new file mode 100644 index 0000000..e8eb953 --- /dev/null +++ b/src/file/js/rootsUtils.js @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var rootsUtils = (function() { + var roots_to_resolve = [ + { + filesystemName: 'temporary', + name: '', + fullPath: '', + nativeURL: 'wgt-private-tmp' + }, + { + filesystemName: 'persistent', + name: '', + fullPath: '', + nativeURL: 'wgt-private' + } + ]; + + var roots = [ + { + filesystemName: 'root', + name: '', + fullPath: '/', + nativeURL: '/' + } + ]; + + 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.push(roots_to_resolve[0]); + roots_to_resolve.splice(0, 1); // remove first item + + // we've resolved one root, check if there are any other + getRoots(successCallback); + }, function(e) { + console.error(e); + // in case of an error, return the roots we have so far + successCallback(roots); + }); + } else { + successCallback(roots.slice()); + } + } + + function strncmp(str1, str2, n) { + str1 = str1.substring(0, n); + str2 = str2.substring(0, n); + 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]; + } + } + } + + return roots[0]; // root filesystem + } + + return { + getRoots: getRoots, + findFilesystem: findFilesystem + }; +})();