| var exec = require('cordova/exec'), |
| FileError = require('cordova/plugin/FileError'), |
| Metadata = require('cordova/plugin/Metadata'); |
| |
| /** |
| * Represents a file or directory on the local file system. |
| * |
| * @param isFile |
| * {boolean} true if Entry is a file (readonly) |
| * @param isDirectory |
| * {boolean} true if Entry is a directory (readonly) |
| * @param name |
| * {DOMString} name of the file or directory, excluding the path |
| * leading to it (readonly) |
| * @param fullPath |
| * {DOMString} the absolute full path to the file or directory |
| * (readonly) |
| */ |
| function Entry(isFile, isDirectory, name, fullPath, fileSystem) { |
| this.isFile = (typeof isFile != 'undefined'?isFile:false); |
| this.isDirectory = (typeof isDirectory != 'undefined'?isDirectory:false); |
| this.name = name || ''; |
| this.fullPath = fullPath || ''; |
| this.filesystem = fileSystem || null; |
| } |
| |
| /** |
| * Look up the metadata of the entry. |
| * |
| * @param successCallback |
| * {Function} is called with a Metadata object |
| * @param errorCallback |
| * {Function} is called with a FileError |
| */ |
| Entry.prototype.getMetadata = function(successCallback, errorCallback) { |
| var success = typeof successCallback !== 'function' ? null : function(lastModified) { |
| var metadata = new Metadata(lastModified); |
| successCallback(metadata); |
| }; |
| var fail = typeof errorCallback !== 'function' ? null : function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| |
| exec(success, fail, "File", "getMetadata", [this.fullPath]); |
| }; |
| |
| /** |
| * Set the metadata of the entry. |
| * |
| * @param successCallback |
| * {Function} is called with a Metadata object |
| * @param errorCallback |
| * {Function} is called with a FileError |
| * @param metadataObject |
| * {Object} keys and values to set |
| */ |
| Entry.prototype.setMetadata = function(successCallback, errorCallback, metadataObject) { |
| |
| exec(successCallback, errorCallback, "File", "setMetadata", [this.fullPath, metadataObject]); |
| }; |
| |
| /** |
| * Move a file or directory to a new location. |
| * |
| * @param parent |
| * {DirectoryEntry} the directory to which to move this entry |
| * @param newName |
| * {DOMString} new name of the entry, defaults to the current name |
| * @param successCallback |
| * {Function} called with the new DirectoryEntry object |
| * @param errorCallback |
| * {Function} called with a FileError |
| */ |
| Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) { |
| var fail = function(code) { |
| if (typeof errorCallback === 'function') { |
| errorCallback(new FileError(code)); |
| } |
| }; |
| // user must specify parent Entry |
| if (!parent) { |
| fail(FileError.NOT_FOUND_ERR); |
| return; |
| } |
| // source path |
| var srcPath = this.fullPath, |
| // entry name |
| name = newName || this.name, |
| success = function(entry) { |
| if (entry) { |
| if (typeof successCallback === 'function') { |
| // create appropriate Entry object |
| var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); |
| try { |
| successCallback(result); |
| } |
| catch (e) { |
| console.log('Error invoking callback: ' + e); |
| } |
| } |
| } |
| else { |
| // no Entry object returned |
| fail(FileError.NOT_FOUND_ERR); |
| } |
| }; |
| |
| // copy |
| exec(success, fail, "File", "moveTo", [srcPath, parent.fullPath, name]); |
| }; |
| |
| /** |
| * Copy a directory to a different location. |
| * |
| * @param parent |
| * {DirectoryEntry} the directory to which to copy the entry |
| * @param newName |
| * {DOMString} new name of the entry, defaults to the current name |
| * @param successCallback |
| * {Function} called with the new Entry object |
| * @param errorCallback |
| * {Function} called with a FileError |
| */ |
| Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) { |
| var fail = function(code) { |
| if (typeof errorCallback === 'function') { |
| errorCallback(new FileError(code)); |
| } |
| }; |
| |
| // user must specify parent Entry |
| if (!parent) { |
| fail(FileError.NOT_FOUND_ERR); |
| return; |
| } |
| |
| // source path |
| var srcPath = this.fullPath, |
| // entry name |
| name = newName || this.name, |
| // success callback |
| success = function(entry) { |
| if (entry) { |
| if (typeof successCallback === 'function') { |
| // create appropriate Entry object |
| var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); |
| try { |
| successCallback(result); |
| } |
| catch (e) { |
| console.log('Error invoking callback: ' + e); |
| } |
| } |
| } |
| else { |
| // no Entry object returned |
| fail(FileError.NOT_FOUND_ERR); |
| } |
| }; |
| |
| // copy |
| exec(success, fail, "File", "copyTo", [srcPath, parent.fullPath, name]); |
| }; |
| |
| /** |
| * Return a URL that can be used to identify this entry. |
| */ |
| Entry.prototype.toURL = function() { |
| // fullPath attribute contains the full URL |
| return this.fullPath; |
| }; |
| |
| /** |
| * Returns a URI that can be used to identify this entry. |
| * |
| * @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI. |
| * @return uri |
| */ |
| Entry.prototype.toURI = function(mimeType) { |
| console.log("DEPRECATED: Update your code to use 'toURL'"); |
| // fullPath attribute contains the full URI |
| return this.toURL(); |
| }; |
| |
| /** |
| * Remove a file or directory. It is an error to attempt to delete a |
| * directory that is not empty. It is an error to attempt to delete a |
| * root directory of a file system. |
| * |
| * @param successCallback {Function} called with no parameters |
| * @param errorCallback {Function} called with a FileError |
| */ |
| Entry.prototype.remove = function(successCallback, errorCallback) { |
| var fail = typeof errorCallback !== 'function' ? null : function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| exec(successCallback, fail, "File", "remove", [this.fullPath]); |
| }; |
| |
| /** |
| * Look up the parent DirectoryEntry of this entry. |
| * |
| * @param successCallback {Function} called with the parent DirectoryEntry object |
| * @param errorCallback {Function} called with a FileError |
| */ |
| Entry.prototype.getParent = function(successCallback, errorCallback) { |
| var win = typeof successCallback !== 'function' ? null : function(result) { |
| var DirectoryEntry = require('cordova/plugin/DirectoryEntry'); |
| var entry = new DirectoryEntry(result.name, result.fullPath); |
| successCallback(entry); |
| }; |
| var fail = typeof errorCallback !== 'function' ? null : function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| exec(win, fail, "File", "getParent", [this.fullPath]); |
| }; |
| |
| module.exports = Entry; |