Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

Entry.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. var exec = require('cordova/exec');
  23. var FileError = require('./FileError');
  24. var 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. var name = newName || this.name;
  110. var 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: '/' }); // eslint-disable-line no-undef
  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. } else {
  120. // no Entry object returned
  121. if (fail) {
  122. fail(FileError.NOT_FOUND_ERR);
  123. }
  124. }
  125. };
  126. // copy
  127. exec(success, fail, 'File', 'moveTo', [srcURL, parent.toInternalURL(), name]);
  128. };
  129. /**
  130. * Copy a directory to a different location.
  131. *
  132. * @param parent
  133. * {DirectoryEntry} the directory to which to copy the entry
  134. * @param newName
  135. * {DOMString} new name of the entry, defaults to the current name
  136. * @param successCallback
  137. * {Function} called with the new Entry object
  138. * @param errorCallback
  139. * {Function} called with a FileError
  140. */
  141. Entry.prototype.copyTo = function (parent, newName, successCallback, errorCallback) {
  142. argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments);
  143. var fail = errorCallback && function (code) {
  144. errorCallback(new FileError(code));
  145. };
  146. var srcURL = this.toInternalURL();
  147. // entry name
  148. var name = newName || this.name;
  149. // success callback
  150. var success = function (entry) {
  151. if (entry) {
  152. if (successCallback) {
  153. // create appropriate Entry object
  154. var newFSName = entry.filesystemName || (entry.filesystem && entry.filesystem.name);
  155. var fs = newFSName ? new FileSystem(newFSName, { name: '', fullPath: '/' }) : new FileSystem(parent.filesystem.name, { name: '', fullPath: '/' }); // eslint-disable-line no-undef
  156. 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);
  157. successCallback(result);
  158. }
  159. } else {
  160. // no Entry object returned
  161. if (fail) {
  162. fail(FileError.NOT_FOUND_ERR);
  163. }
  164. }
  165. };
  166. // copy
  167. exec(success, fail, 'File', 'copyTo', [srcURL, parent.toInternalURL(), name]);
  168. };
  169. /**
  170. * Return a URL that can be passed across the bridge to identify this entry.
  171. */
  172. Entry.prototype.toInternalURL = function () {
  173. if (this.filesystem && this.filesystem.__format__) {
  174. return this.filesystem.__format__(this.fullPath, this.nativeURL);
  175. }
  176. };
  177. /**
  178. * Return a URL that can be used to identify this entry.
  179. * Use a URL that can be used to as the src attribute of a <video> or
  180. * <audio> tag. If that is not possible, construct a cdvfile:// URL.
  181. */
  182. Entry.prototype.toURL = function () {
  183. if (this.nativeURL) {
  184. return this.nativeURL;
  185. }
  186. // fullPath attribute may contain the full URL in the case that
  187. // toInternalURL fails.
  188. return this.toInternalURL() || 'file://localhost' + this.fullPath;
  189. };
  190. /**
  191. * Backwards-compatibility: In v1.0.0 - 1.0.2, .toURL would only return a
  192. * cdvfile:// URL, and this method was necessary to obtain URLs usable by the
  193. * webview.
  194. * See CB-6051, CB-6106, CB-6117, CB-6152, CB-6199, CB-6201, CB-6243, CB-6249,
  195. * and CB-6300.
  196. */
  197. Entry.prototype.toNativeURL = function () {
  198. console.log("DEPRECATED: Update your code to use 'toURL'");
  199. return this.toURL();
  200. };
  201. /**
  202. * Returns a URI that can be used to identify this entry.
  203. *
  204. * @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI.
  205. * @return uri
  206. */
  207. Entry.prototype.toURI = function (mimeType) {
  208. console.log("DEPRECATED: Update your code to use 'toURL'");
  209. return this.toURL();
  210. };
  211. /**
  212. * Remove a file or directory. It is an error to attempt to delete a
  213. * directory that is not empty. It is an error to attempt to delete a
  214. * root directory of a file system.
  215. *
  216. * @param successCallback {Function} called with no parameters
  217. * @param errorCallback {Function} called with a FileError
  218. */
  219. Entry.prototype.remove = function (successCallback, errorCallback) {
  220. argscheck.checkArgs('FF', 'Entry.remove', arguments);
  221. var fail = errorCallback && function (code) {
  222. errorCallback(new FileError(code));
  223. };
  224. exec(successCallback, fail, 'File', 'remove', [this.toInternalURL()]);
  225. };
  226. /**
  227. * Look up the parent DirectoryEntry of this entry.
  228. *
  229. * @param successCallback {Function} called with the parent DirectoryEntry object
  230. * @param errorCallback {Function} called with a FileError
  231. */
  232. Entry.prototype.getParent = function (successCallback, errorCallback) {
  233. argscheck.checkArgs('FF', 'Entry.getParent', arguments);
  234. var fs = this.filesystem;
  235. var win = successCallback && function (result) {
  236. var DirectoryEntry = require('./DirectoryEntry');
  237. var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL);
  238. successCallback(entry);
  239. };
  240. var fail = errorCallback && function (code) {
  241. errorCallback(new FileError(code));
  242. };
  243. exec(win, fail, 'File', 'getParent', [this.toInternalURL()]);
  244. };
  245. module.exports = Entry;