Entry.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. var argscheck = require('cordova/argscheck'),
  22. exec = require('cordova/exec'),
  23. FileError = require('./FileError'),
  24. Metadata = require('./Metadata');
  25. /**
  26. * Represents a file or directory on the local file system.
  27. *
  28. * @param isFile
  29. * {boolean} true if Entry is a file (readonly)
  30. * @param isDirectory
  31. * {boolean} true if Entry is a directory (readonly)
  32. * @param name
  33. * {DOMString} name of the file or directory, excluding the path
  34. * leading to it (readonly)
  35. * @param fullPath
  36. * {DOMString} the absolute full path to the file or directory
  37. * (readonly)
  38. * @param fileSystem
  39. * {FileSystem} the filesystem on which this entry resides
  40. * (readonly)
  41. * @param nativeURL
  42. * {DOMString} an alternate URL which can be used by native
  43. * webview controls, for example media players.
  44. * (optional, readonly)
  45. */
  46. function Entry(isFile, isDirectory, name, fullPath, fileSystem, nativeURL) {
  47. this.isFile = !!isFile;
  48. this.isDirectory = !!isDirectory;
  49. this.name = name || '';
  50. this.fullPath = fullPath || '';
  51. this.filesystem = fileSystem || null;
  52. this.nativeURL = nativeURL || null;
  53. }
  54. /**
  55. * Look up the metadata of the entry.
  56. *
  57. * @param successCallback
  58. * {Function} is called with a Metadata object
  59. * @param errorCallback
  60. * {Function} is called with a FileError
  61. */
  62. Entry.prototype.getMetadata = function(successCallback, errorCallback) {
  63. argscheck.checkArgs('FF', 'Entry.getMetadata', arguments);
  64. var success = successCallback && function(entryMetadata) {
  65. var metadata = new Metadata({
  66. size: entryMetadata.size,
  67. modificationTime: entryMetadata.lastModifiedDate
  68. });
  69. successCallback(metadata);
  70. };
  71. var fail = errorCallback && function(code) {
  72. errorCallback(new FileError(code));
  73. };
  74. exec(success, fail, "File", "getFileMetadata", [this.toInternalURL()]);
  75. };
  76. /**
  77. * Set the metadata of the entry.
  78. *
  79. * @param successCallback
  80. * {Function} is called with a Metadata object
  81. * @param errorCallback
  82. * {Function} is called with a FileError
  83. * @param metadataObject
  84. * {Object} keys and values to set
  85. */
  86. Entry.prototype.setMetadata = function(successCallback, errorCallback, metadataObject) {
  87. argscheck.checkArgs('FFO', 'Entry.setMetadata', arguments);
  88. exec(successCallback, errorCallback, "File", "setMetadata", [this.toInternalURL(), metadataObject]);
  89. };
  90. /**
  91. * Move a file or directory to a new location.
  92. *
  93. * @param parent
  94. * {DirectoryEntry} the directory to which to move this entry
  95. * @param newName
  96. * {DOMString} new name of the entry, defaults to the current name
  97. * @param successCallback
  98. * {Function} called with the new DirectoryEntry object
  99. * @param errorCallback
  100. * {Function} called with a FileError
  101. */
  102. Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
  103. argscheck.checkArgs('oSFF', 'Entry.moveTo', arguments);
  104. var fail = errorCallback && function(code) {
  105. errorCallback(new FileError(code));
  106. };
  107. var srcURL = this.toInternalURL(),
  108. // entry name
  109. name = newName || this.name,
  110. success = function(entry) {
  111. if (entry) {
  112. if (successCallback) {
  113. // create appropriate Entry object
  114. var newFSName = entry.filesystemName || (entry.filesystem && entry.filesystem.name);
  115. var fs = newFSName ? new FileSystem(newFSName, { name: "", fullPath: "/" }) : new FileSystem(parent.filesystem.name, { name: "", fullPath: "/" });
  116. var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('cordova-plugin-file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL);
  117. successCallback(result);
  118. }
  119. }
  120. else {
  121. // no Entry object returned
  122. if (fail) {
  123. fail(FileError.NOT_FOUND_ERR);
  124. }
  125. }
  126. };
  127. // copy
  128. exec(success, fail, "File", "moveTo", [srcURL, parent.toInternalURL(), name]);
  129. };
  130. /**
  131. * Copy a directory to a different location.
  132. *
  133. * @param parent
  134. * {DirectoryEntry} the directory to which to copy the entry
  135. * @param newName
  136. * {DOMString} new name of the entry, defaults to the current name
  137. * @param successCallback
  138. * {Function} called with the new Entry object
  139. * @param errorCallback
  140. * {Function} called with a FileError
  141. */
  142. Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
  143. argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments);
  144. var fail = errorCallback && function(code) {
  145. errorCallback(new FileError(code));
  146. };
  147. var srcURL = this.toInternalURL(),
  148. // entry name
  149. name = newName || this.name,
  150. // success callback
  151. success = function(entry) {
  152. if (entry) {
  153. if (successCallback) {
  154. // create appropriate Entry object
  155. var newFSName = entry.filesystemName || (entry.filesystem && entry.filesystem.name);
  156. var fs = newFSName ? new FileSystem(newFSName, { name: "", fullPath: "/" }) : new FileSystem(parent.filesystem.name, { name: "", fullPath: "/" });
  157. var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('cordova-plugin-file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL);
  158. successCallback(result);
  159. }
  160. }
  161. else {
  162. // no Entry object returned
  163. if (fail) {
  164. fail(FileError.NOT_FOUND_ERR);
  165. }
  166. }
  167. };
  168. // copy
  169. exec(success, fail, "File", "copyTo", [srcURL, parent.toInternalURL(), name]);
  170. };
  171. /**
  172. * Return a URL that can be passed across the bridge to identify this entry.
  173. */
  174. Entry.prototype.toInternalURL = function() {
  175. if (this.filesystem && this.filesystem.__format__) {
  176. return this.filesystem.__format__(this.fullPath, this.nativeURL);
  177. }
  178. };
  179. /**
  180. * Return a URL that can be used to identify this entry.
  181. * Use a URL that can be used to as the src attribute of a <video> or
  182. * <audio> tag. If that is not possible, construct a cdvfile:// URL.
  183. */
  184. Entry.prototype.toURL = function() {
  185. if (this.nativeURL) {
  186. return this.nativeURL;
  187. }
  188. // fullPath attribute may contain the full URL in the case that
  189. // toInternalURL fails.
  190. return this.toInternalURL() || "file://localhost" + this.fullPath;
  191. };
  192. /**
  193. * Backwards-compatibility: In v1.0.0 - 1.0.2, .toURL would only return a
  194. * cdvfile:// URL, and this method was necessary to obtain URLs usable by the
  195. * webview.
  196. * See CB-6051, CB-6106, CB-6117, CB-6152, CB-6199, CB-6201, CB-6243, CB-6249,
  197. * and CB-6300.
  198. */
  199. Entry.prototype.toNativeURL = function() {
  200. console.log("DEPRECATED: Update your code to use 'toURL'");
  201. return this.toURL();
  202. };
  203. /**
  204. * Returns a URI that can be used to identify this entry.
  205. *
  206. * @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI.
  207. * @return uri
  208. */
  209. Entry.prototype.toURI = function(mimeType) {
  210. console.log("DEPRECATED: Update your code to use 'toURL'");
  211. return this.toURL();
  212. };
  213. /**
  214. * Remove a file or directory. It is an error to attempt to delete a
  215. * directory that is not empty. It is an error to attempt to delete a
  216. * root directory of a file system.
  217. *
  218. * @param successCallback {Function} called with no parameters
  219. * @param errorCallback {Function} called with a FileError
  220. */
  221. Entry.prototype.remove = function(successCallback, errorCallback) {
  222. argscheck.checkArgs('FF', 'Entry.remove', arguments);
  223. var fail = errorCallback && function(code) {
  224. errorCallback(new FileError(code));
  225. };
  226. exec(successCallback, fail, "File", "remove", [this.toInternalURL()]);
  227. };
  228. /**
  229. * Look up the parent DirectoryEntry of this entry.
  230. *
  231. * @param successCallback {Function} called with the parent DirectoryEntry object
  232. * @param errorCallback {Function} called with a FileError
  233. */
  234. Entry.prototype.getParent = function(successCallback, errorCallback) {
  235. argscheck.checkArgs('FF', 'Entry.getParent', arguments);
  236. var fs = this.filesystem;
  237. var win = successCallback && function(result) {
  238. var DirectoryEntry = require('./DirectoryEntry');
  239. var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL);
  240. successCallback(entry);
  241. };
  242. var fail = errorCallback && function(code) {
  243. errorCallback(new FileError(code));
  244. };
  245. exec(win, fail, "File", "getParent", [this.toInternalURL()]);
  246. };
  247. module.exports = Entry;