openEventsPlatformApp/plugins/cordova-plugin-file/www/FileEntry.js
plugins/cordova-plugin-file/www/FileEntry.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 utils = require('cordova/utils'),
23 exec = require('cordova/exec'),
24 Entry = require('./Entry'),
25 FileWriter = require('./FileWriter'),
26 File = require('./File'),
27 FileError = require('./FileError');
28
29/**
30 * An interface representing a file on the file system.
31 *
32 * {boolean} isFile always true (readonly)
33 * {boolean} isDirectory always false (readonly)
34 * {DOMString} name of the file, excluding the path leading to it (readonly)
35 * {DOMString} fullPath the absolute full path to the file (readonly)
36 * {FileSystem} filesystem on which the file resides (readonly)
37 */
38var FileEntry = function(name, fullPath, fileSystem, nativeURL) {
39 // remove trailing slash if it is present
40 if (fullPath && /\/$/.test(fullPath)) {
41 fullPath = fullPath.substring(0, fullPath.length - 1);
42 }
43 if (nativeURL && /\/$/.test(nativeURL)) {
44 nativeURL = nativeURL.substring(0, nativeURL.length - 1);
45 }
46
47 FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath, fileSystem, nativeURL]);
48};
49
50utils.extend(FileEntry, Entry);
51
52/**
53 * Creates a new FileWriter associated with the file that this FileEntry represents.
54 *
55 * @param {Function} successCallback is called with the new FileWriter
56 * @param {Function} errorCallback is called with a FileError
57 */
58FileEntry.prototype.createWriter = function(successCallback, errorCallback) {
59 this.file(function(filePointer) {
60 var writer = new FileWriter(filePointer);
61
62 if (writer.localURL === null || writer.localURL === "") {
63 if (errorCallback) {
64 errorCallback(new FileError(FileError.INVALID_STATE_ERR));
65 }
66 } else {
67 if (successCallback) {
68 successCallback(writer);
69 }
70 }
71 }, errorCallback);
72};
73
74/**
75 * Returns a File that represents the current state of the file that this FileEntry represents.
76 *
77 * @param {Function} successCallback is called with the new File object
78 * @param {Function} errorCallback is called with a FileError
79 */
80FileEntry.prototype.file = function(successCallback, errorCallback) {
81 var localURL = this.toInternalURL();
82 var win = successCallback && function(f) {
83 var file = new File(f.name, localURL, f.type, f.lastModifiedDate, f.size);
84 successCallback(file);
85 };
86 var fail = errorCallback && function(code) {
87 errorCallback(new FileError(code));
88 };
89 exec(win, fail, "File", "getFileMetadata", [localURL]);
90};
91
92
93module.exports = FileEntry;