arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/blackberry10/resolveLocalFileSystemURI.js
plugins/cordova-plugin-file/www/blackberry10/resolveLocalFileSystemURI.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/*
 23 * resolveLocalFileSystemURI
 24 *
 25 * IN
 26 *  args
 27 *   0 - escaped local filesystem URI
 28 *   1 - options (standard HTML5 file system options)
 29 *   2 - size
 30 * OUT
 31 *  success - Entry object
 32 *   - isDirectory
 33 *   - isFile
 34 *   - name
 35 *   - fullPath
 36 *   - nativeURL
 37 *   - fileSystemName
 38 *  fail - FileError code
 39 */
 40
 41var info = require('cordova-plugin-file.bb10FileSystemInfo'),
 42    requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
 43    createEntryFromNative = require('cordova-plugin-file.bb10CreateEntryFromNative'),
 44    SANDBOXED = true,
 45    UNSANDBOXED = false;
 46
 47module.exports = function (success, fail, args) {
 48    var request = args[0],
 49        options = args[1],
 50        size = args[2];
 51    if (request) {
 52        request = decodeURIComponent(request);
 53        if (request.indexOf('?') > -1) {
 54            //bb10 does not support params; strip them off
 55            request = request.substring(0, request.indexOf('?'));
 56        }
 57        if (request.indexOf('file://localhost/') === 0) {
 58            //remove localhost prefix
 59            request = request.replace('file://localhost/', 'file:///');
 60        }
 61        //requests to sandboxed locations should use cdvfile
 62        request = request.replace(info.persistentPath, 'cdvfile://localhost/persistent');
 63        request = request.replace(info.temporaryPath, 'cdvfile://localhost/temporary');
 64        //pick appropriate handler
 65        if (request.indexOf('file:///') === 0) {
 66            resolveFile(success, fail, request, options);
 67        } else if (request.indexOf('cdvfile://localhost/') === 0) {
 68            resolveCdvFile(success, fail, request, options, size);
 69        } else if (request.indexOf('local:///') === 0) {
 70            resolveLocal(success, fail, request, options);
 71        } else {
 72            fail(FileError.ENCODING_ERR);
 73        }
 74    } else {
 75        fail(FileError.NOT_FOUND_ERR);
 76    }
 77};
 78
 79//resolve file:///
 80function resolveFile(success, fail, request, options) {
 81    var path = request.substring(7);
 82    resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
 83}
 84
 85//resolve cdvfile://localhost/filesystemname/
 86function resolveCdvFile(success, fail, request, options, size) {
 87    var components = /cdvfile:\/\/localhost\/([^\/]+)\/(.*)/.exec(request),
 88        fsType = components[1],
 89        path = components[2];
 90    if (fsType === 'persistent') {
 91        resolve(success, fail, path, window.PERSISTENT, SANDBOXED, options, size);
 92    }
 93    else if (fsType === 'temporary') {
 94        resolve(success, fail, path, window.TEMPORARY, SANDBOXED, options, size);
 95    }
 96    else if (fsType === 'root') {
 97        resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
 98    }
 99    else {
100        fail(FileError.NOT_FOUND_ERR);
101    }
102}
103
104//resolve local:///
105function resolveLocal(success, fail, request, options) {
106    var path = localPath + request.substring(8);
107    resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
108}
109
110//validate parameters and set sandbox
111function resolve(success, fail, path, fsType, sandbox, options, size) {
112    options = options || { create: false };
113    size = size || info.MAX_SIZE;
114    if (size > info.MAX_SIZE) {
115        //bb10 does not respect quota; fail at unreasonably large size
116        fail(FileError.QUOTA_EXCEEDED_ERR);
117    } else if (path.indexOf(':') > -1) {
118        //files with : character are not valid in Cordova apps 
119        fail(FileError.ENCODING_ERR);
120    } else {
121        requestAnimationFrame(function () {
122            cordova.exec(function () {
123                requestAnimationFrame(function () {
124                    resolveNative(success, fail, path, fsType, options, size);
125                });
126            }, fail, 'File', 'setSandbox', [sandbox], false);
127        });
128    }
129}
130
131//find path using webkit file system
132function resolveNative(success, fail, path, fsType, options, size) {
133    window.webkitRequestFileSystem(
134        fsType,
135        size,
136        function (fs) {
137            if (path === '') {
138                //no path provided, call success with root file system
139                success(createEntryFromNative(fs.root));
140            } else {
141                //otherwise attempt to resolve as file
142                fs.root.getFile(
143                    path,
144                    options,
145                    function (entry) {
146                        success(createEntryFromNative(entry));
147                    },
148                    function (fileError) {
149                        //file not found, attempt to resolve as directory
150                        fs.root.getDirectory(
151                            path,
152                            options,
153                            function (entry) {
154                                success(createEntryFromNative(entry));
155                            },
156                            function (dirError) {
157                                //path cannot be resolved
158                                if (fileError.code === FileError.INVALID_MODIFICATION_ERR && 
159                                    options.exclusive) {
160                                    //mobile-spec expects this error code
161                                    fail(FileError.PATH_EXISTS_ERR);
162                                } else {
163                                    fail(FileError.NOT_FOUND_ERR);
164                                }
165                            }
166                        );
167                    }
168                );
169            }
170        }
171    );
172}