| /* |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| * |
| */ |
| |
| var argscheck = require('cordova/argscheck'), |
| exec = require('cordova/exec'), |
| FileError = require('./FileError'), |
| Metadata = require('./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) |
| * @param fileSystem |
| * {FileSystem} the filesystem on which this entry resides |
| * (readonly) |
| */ |
| function Entry(isFile, isDirectory, name, fullPath, fileSystem) { |
| this.isFile = !!isFile; |
| this.isDirectory = !!isDirectory; |
| 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) { |
| argscheck.checkArgs('FF', 'Entry.getMetadata', arguments); |
| var success = successCallback && function(lastModified) { |
| var metadata = new Metadata(lastModified); |
| successCallback(metadata); |
| }; |
| var fail = errorCallback && 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) { |
| argscheck.checkArgs('FFO', 'Entry.setMetadata', arguments); |
| 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) { |
| argscheck.checkArgs('oSFF', 'Entry.moveTo', arguments); |
| var fail = errorCallback && function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| // source path |
| var srcPath = this.fullPath, |
| // entry name |
| name = newName || this.name, |
| success = function(entry) { |
| if (entry) { |
| if (successCallback) { |
| // create appropriate Entry object |
| var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, entry.filesystem) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, entry.filesystem); |
| successCallback(result); |
| } |
| } |
| else { |
| // no Entry object returned |
| fail && 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) { |
| argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments); |
| var fail = errorCallback && function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| |
| // source path |
| var srcPath = this.fullPath, |
| // entry name |
| name = newName || this.name, |
| // success callback |
| success = function(entry) { |
| if (entry) { |
| if (successCallback) { |
| // create appropriate Entry object |
| var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, entry.filesystem) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, entry.filesystem); |
| successCallback(result); |
| } |
| } |
| else { |
| // no Entry object returned |
| fail && 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) { |
| argscheck.checkArgs('FF', 'Entry.remove', arguments); |
| var fail = errorCallback && 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) { |
| argscheck.checkArgs('FF', 'Entry.getParent', arguments); |
| var fs = this.filesystem; |
| var win = successCallback && function(result) { |
| var DirectoryEntry = require('./DirectoryEntry'); |
| var entry = new DirectoryEntry(result.name, result.fullPath, fs); |
| successCallback(entry); |
| }; |
| var fail = errorCallback && function(code) { |
| errorCallback(new FileError(code)); |
| }; |
| exec(win, fail, "File", "getParent", [this.fullPath]); |
| }; |
| |
| module.exports = Entry; |