arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/requestFileSystem.js
plugins/cordova-plugin-file/www/requestFileSystem.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
22(function() {
23    //For browser platform: not all browsers use this file.
24    function checkBrowser() {
25        if (cordova.platformId === "browser" && require('./isChrome')()) {
26            module.exports = window.requestFileSystem || window.webkitRequestFileSystem;
27            return true;
28        }
29        return false;
30    }
31    if (checkBrowser()) {
32        return;
33    }
34
35    var argscheck = require('cordova/argscheck'),
36        FileError = require('./FileError'),
37        FileSystem = require('./FileSystem'),
38        exec = require('cordova/exec');
39    var fileSystems = require('./fileSystems');
40
41    /**
42     * Request a file system in which to store application data.
43     * @param type  local file system type
44     * @param size  indicates how much storage space, in bytes, the application expects to need
45     * @param successCallback  invoked with a FileSystem object
46     * @param errorCallback  invoked if error occurs retrieving file system
47     */
48    var requestFileSystem = function(type, size, successCallback, errorCallback) {
49        argscheck.checkArgs('nnFF', 'requestFileSystem', arguments);
50        var fail = function(code) {
51            if (errorCallback) {
52                errorCallback(new FileError(code));
53            }
54        };
55
56        if (type < 0) {
57            fail(FileError.SYNTAX_ERR);
58        } else {
59            // if successful, return a FileSystem object
60            var success = function(file_system) {
61                if (file_system) {
62                    if (successCallback) {
63                        fileSystems.getFs(file_system.name, function(fs) {
64                            // This should happen only on platforms that haven't implemented requestAllFileSystems (windows)
65                            if (!fs) {
66                                fs = new FileSystem(file_system.name, file_system.root);
67                            }
68                            successCallback(fs);
69                        });
70                    }
71                }
72                else {
73                    // no FileSystem object returned
74                    fail(FileError.NOT_FOUND_ERR);
75                }
76            };
77            exec(success, fail, "File", "requestFileSystem", [type, size]);
78        }
79    };
80
81    module.exports = requestFileSystem;
82})();