resolveLocalFileSystemURI.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. (function() {
  22. //For browser platform: not all browsers use overrided `resolveLocalFileSystemURL`.
  23. function checkBrowser() {
  24. if (cordova.platformId === "browser" && require('./isChrome')()) {
  25. module.exports.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
  26. return true;
  27. }
  28. return false;
  29. }
  30. if (checkBrowser()) {
  31. return;
  32. }
  33. var argscheck = require('cordova/argscheck'),
  34. DirectoryEntry = require('./DirectoryEntry'),
  35. FileEntry = require('./FileEntry'),
  36. FileError = require('./FileError'),
  37. exec = require('cordova/exec');
  38. var fileSystems = require('./fileSystems');
  39. /**
  40. * Look up file system Entry referred to by local URI.
  41. * @param {DOMString} uri URI referring to a local file or directory
  42. * @param successCallback invoked with Entry object corresponding to URI
  43. * @param errorCallback invoked if error occurs retrieving file system entry
  44. */
  45. module.exports.resolveLocalFileSystemURL = module.exports.resolveLocalFileSystemURL || function(uri, successCallback, errorCallback) {
  46. argscheck.checkArgs('sFF', 'resolveLocalFileSystemURI', arguments);
  47. // error callback
  48. var fail = function(error) {
  49. if (errorCallback) {
  50. errorCallback(new FileError(error));
  51. }
  52. };
  53. // sanity check for 'not:valid:filename' or '/not:valid:filename'
  54. // file.spec.12 window.resolveLocalFileSystemURI should error (ENCODING_ERR) when resolving invalid URI with leading /.
  55. if(!uri || uri.split(":").length > 2) {
  56. setTimeout( function() {
  57. fail(FileError.ENCODING_ERR);
  58. },0);
  59. return;
  60. }
  61. // if successful, return either a file or directory entry
  62. var success = function(entry) {
  63. if (entry) {
  64. if (successCallback) {
  65. // create appropriate Entry object
  66. var fsName = entry.filesystemName || (entry.filesystem && entry.filesystem.name) || (entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary');
  67. fileSystems.getFs(fsName, function(fs) {
  68. // This should happen only on platforms that haven't implemented requestAllFileSystems (windows)
  69. if (!fs) {
  70. fs = new FileSystem(fsName, {name:"", fullPath:"/"});
  71. }
  72. var result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath, fs, entry.nativeURL) : new FileEntry(entry.name, entry.fullPath, fs, entry.nativeURL);
  73. successCallback(result);
  74. });
  75. }
  76. }
  77. else {
  78. // no Entry object returned
  79. fail(FileError.NOT_FOUND_ERR);
  80. }
  81. };
  82. exec(success, fail, "File", "resolveLocalFileSystemURI", [uri]);
  83. };
  84. module.exports.resolveLocalFileSystemURI = function() {
  85. console.log("resolveLocalFileSystemURI is deprecated. Please call resolveLocalFileSystemURL instead.");
  86. module.exports.resolveLocalFileSystemURL.apply(this, arguments);
  87. };
  88. })();