arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/DirectoryEntry.js
plugins/cordova-plugin-file/www/DirectoryEntry.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    utils = require('cordova/utils'),
 24    exec = require('cordova/exec'),
 25    Entry = require('./Entry'),
 26    FileError = require('./FileError'),
 27    DirectoryReader = require('./DirectoryReader');
 28
 29/**
 30 * An interface representing a directory on the file system.
 31 *
 32 * {boolean} isFile always false (readonly)
 33 * {boolean} isDirectory always true (readonly)
 34 * {DOMString} name of the directory, excluding the path leading to it (readonly)
 35 * {DOMString} fullPath the absolute full path to the directory (readonly)
 36 * {FileSystem} filesystem on which the directory resides (readonly)
 37 */
 38var DirectoryEntry = function(name, fullPath, fileSystem, nativeURL) {
 39
 40    // add trailing slash if it is missing
 41    if ((fullPath) && !/\/$/.test(fullPath)) {
 42        fullPath += "/";
 43    }
 44    // add trailing slash if it is missing
 45    if (nativeURL && !/\/$/.test(nativeURL)) {
 46        nativeURL += "/";
 47    }
 48    DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath, fileSystem, nativeURL);
 49};
 50
 51utils.extend(DirectoryEntry, Entry);
 52
 53/**
 54 * Creates a new DirectoryReader to read entries from this directory
 55 */
 56DirectoryEntry.prototype.createReader = function() {
 57    return new DirectoryReader(this.toInternalURL());
 58};
 59
 60/**
 61 * Creates or looks up a directory
 62 *
 63 * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a directory
 64 * @param {Flags} options to create or exclusively create the directory
 65 * @param {Function} successCallback is called with the new entry
 66 * @param {Function} errorCallback is called with a FileError
 67 */
 68DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback) {
 69    argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments);
 70    var fs = this.filesystem;
 71    var win = successCallback && function(result) {
 72        var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL);
 73        successCallback(entry);
 74    };
 75    var fail = errorCallback && function(code) {
 76        errorCallback(new FileError(code));
 77    };
 78    exec(win, fail, "File", "getDirectory", [this.toInternalURL(), path, options]);
 79};
 80
 81/**
 82 * Deletes a directory and all of it's contents
 83 *
 84 * @param {Function} successCallback is called with no parameters
 85 * @param {Function} errorCallback is called with a FileError
 86 */
 87DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) {
 88    argscheck.checkArgs('FF', 'DirectoryEntry.removeRecursively', arguments);
 89    var fail = errorCallback && function(code) {
 90        errorCallback(new FileError(code));
 91    };
 92    exec(successCallback, fail, "File", "removeRecursively", [this.toInternalURL()]);
 93};
 94
 95/**
 96 * Creates or looks up a file
 97 *
 98 * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a file
 99 * @param {Flags} options to create or exclusively create the file
100 * @param {Function} successCallback is called with the new entry
101 * @param {Function} errorCallback is called with a FileError
102 */
103DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback) {
104    argscheck.checkArgs('sOFF', 'DirectoryEntry.getFile', arguments);
105    var fs = this.filesystem;
106    var win = successCallback && function(result) {
107        var FileEntry = require('./FileEntry');
108        var entry = new FileEntry(result.name, result.fullPath, fs, result.nativeURL);
109        successCallback(entry);
110    };
111    var fail = errorCallback && function(code) {
112        errorCallback(new FileError(code));
113    };
114    exec(win, fail, "File", "getFile", [this.toInternalURL(), path, options]);
115};
116
117module.exports = DirectoryEntry;