resolveLocalFileSystemURI.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /*
  22. * resolveLocalFileSystemURI
  23. *
  24. * IN
  25. * args
  26. * 0 - escaped local filesystem URI
  27. * 1 - options (standard HTML5 file system options)
  28. * 2 - size
  29. * OUT
  30. * success - Entry object
  31. * - isDirectory
  32. * - isFile
  33. * - name
  34. * - fullPath
  35. * - nativeURL
  36. * - fileSystemName
  37. * fail - FileError code
  38. */
  39. var info = require('cordova-plugin-file.bb10FileSystemInfo'),
  40. requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
  41. createEntryFromNative = require('cordova-plugin-file.bb10CreateEntryFromNative'),
  42. SANDBOXED = true,
  43. UNSANDBOXED = false;
  44. module.exports = function (success, fail, args) {
  45. var request = args[0],
  46. options = args[1],
  47. size = args[2];
  48. if (request) {
  49. request = decodeURIComponent(request);
  50. if (request.indexOf('?') > -1) {
  51. //bb10 does not support params; strip them off
  52. request = request.substring(0, request.indexOf('?'));
  53. }
  54. if (request.indexOf('file://localhost/') === 0) {
  55. //remove localhost prefix
  56. request = request.replace('file://localhost/', 'file:///');
  57. }
  58. //requests to sandboxed locations should use cdvfile
  59. request = request.replace(info.persistentPath, 'cdvfile://localhost/persistent');
  60. request = request.replace(info.temporaryPath, 'cdvfile://localhost/temporary');
  61. //pick appropriate handler
  62. if (request.indexOf('file:///') === 0) {
  63. resolveFile(success, fail, request, options);
  64. } else if (request.indexOf('cdvfile://localhost/') === 0) {
  65. resolveCdvFile(success, fail, request, options, size);
  66. } else if (request.indexOf('local:///') === 0) {
  67. resolveLocal(success, fail, request, options);
  68. } else {
  69. fail(FileError.ENCODING_ERR);
  70. }
  71. } else {
  72. fail(FileError.NOT_FOUND_ERR);
  73. }
  74. };
  75. //resolve file:///
  76. function resolveFile(success, fail, request, options) {
  77. var path = request.substring(7);
  78. resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
  79. }
  80. //resolve cdvfile://localhost/filesystemname/
  81. function resolveCdvFile(success, fail, request, options, size) {
  82. var components = /cdvfile:\/\/localhost\/([^\/]+)\/(.*)/.exec(request),
  83. fsType = components[1],
  84. path = components[2];
  85. if (fsType === 'persistent') {
  86. resolve(success, fail, path, window.PERSISTENT, SANDBOXED, options, size);
  87. }
  88. else if (fsType === 'temporary') {
  89. resolve(success, fail, path, window.TEMPORARY, SANDBOXED, options, size);
  90. }
  91. else if (fsType === 'root') {
  92. resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
  93. }
  94. else {
  95. fail(FileError.NOT_FOUND_ERR);
  96. }
  97. }
  98. //resolve local:///
  99. function resolveLocal(success, fail, request, options) {
  100. var path = localPath + request.substring(8);
  101. resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
  102. }
  103. //validate parameters and set sandbox
  104. function resolve(success, fail, path, fsType, sandbox, options, size) {
  105. options = options || { create: false };
  106. size = size || info.MAX_SIZE;
  107. if (size > info.MAX_SIZE) {
  108. //bb10 does not respect quota; fail at unreasonably large size
  109. fail(FileError.QUOTA_EXCEEDED_ERR);
  110. } else if (path.indexOf(':') > -1) {
  111. //files with : character are not valid in Cordova apps
  112. fail(FileError.ENCODING_ERR);
  113. } else {
  114. requestAnimationFrame(function () {
  115. cordova.exec(function () {
  116. requestAnimationFrame(function () {
  117. resolveNative(success, fail, path, fsType, options, size);
  118. });
  119. }, fail, 'File', 'setSandbox', [sandbox], false);
  120. });
  121. }
  122. }
  123. //find path using webkit file system
  124. function resolveNative(success, fail, path, fsType, options, size) {
  125. window.webkitRequestFileSystem(
  126. fsType,
  127. size,
  128. function (fs) {
  129. if (path === '') {
  130. //no path provided, call success with root file system
  131. success(createEntryFromNative(fs.root));
  132. } else {
  133. //otherwise attempt to resolve as file
  134. fs.root.getFile(
  135. path,
  136. options,
  137. function (entry) {
  138. success(createEntryFromNative(entry));
  139. },
  140. function (fileError) {
  141. //file not found, attempt to resolve as directory
  142. fs.root.getDirectory(
  143. path,
  144. options,
  145. function (entry) {
  146. success(createEntryFromNative(entry));
  147. },
  148. function (dirError) {
  149. //path cannot be resolved
  150. if (fileError.code === FileError.INVALID_MODIFICATION_ERR &&
  151. options.exclusive) {
  152. //mobile-spec expects this error code
  153. fail(FileError.PATH_EXISTS_ERR);
  154. } else {
  155. fail(FileError.NOT_FOUND_ERR);
  156. }
  157. }
  158. );
  159. }
  160. );
  161. }
  162. }
  163. );
  164. }