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