resolveLocalFileSystemURI.js 6.17 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
*/

/*
 * resolveLocalFileSystemURI
 *
 * IN
 *  args
 *   0 - escaped local filesystem URI
 *   1 - options (standard HTML5 file system options)
 *   2 - size
 * OUT
 *  success - Entry object
 *   - isDirectory
 *   - isFile
 *   - name
 *   - fullPath
 *   - nativeURL
 *   - fileSystemName
 *  fail - FileError code
 */

var info = require('cordova-plugin-file.bb10FileSystemInfo'),
    requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
    createEntryFromNative = require('cordova-plugin-file.bb10CreateEntryFromNative'),
    SANDBOXED = true,
    UNSANDBOXED = false;

module.exports = function (success, fail, args) {
    var request = args[0],
        options = args[1],
        size = args[2];
    if (request) {
        request = decodeURIComponent(request);
        if (request.indexOf('?') > -1) {
            //bb10 does not support params; strip them off
            request = request.substring(0, request.indexOf('?'));
        }
        if (request.indexOf('file://localhost/') === 0) {
            //remove localhost prefix
            request = request.replace('file://localhost/', 'file:///');
        }
        //requests to sandboxed locations should use cdvfile
        request = request.replace(info.persistentPath, 'cdvfile://localhost/persistent');
        request = request.replace(info.temporaryPath, 'cdvfile://localhost/temporary');
        //pick appropriate handler
        if (request.indexOf('file:///') === 0) {
            resolveFile(success, fail, request, options);
        } else if (request.indexOf('cdvfile://localhost/') === 0) {
            resolveCdvFile(success, fail, request, options, size);
        } else if (request.indexOf('local:///') === 0) {
            resolveLocal(success, fail, request, options);
        } else {
            fail(FileError.ENCODING_ERR);
        }
    } else {
        fail(FileError.NOT_FOUND_ERR);
    }
};

//resolve file:///
function resolveFile(success, fail, request, options) {
    var path = request.substring(7);
    resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
}

//resolve cdvfile://localhost/filesystemname/
function resolveCdvFile(success, fail, request, options, size) {
    var components = /cdvfile:\/\/localhost\/([^\/]+)\/(.*)/.exec(request),
        fsType = components[1],
        path = components[2];
    if (fsType === 'persistent') {
        resolve(success, fail, path, window.PERSISTENT, SANDBOXED, options, size);
    }
    else if (fsType === 'temporary') {
        resolve(success, fail, path, window.TEMPORARY, SANDBOXED, options, size);
    }
    else if (fsType === 'root') {
        resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
    }
    else {
        fail(FileError.NOT_FOUND_ERR);
    }
}

//resolve local:///
function resolveLocal(success, fail, request, options) {
    var path = localPath + request.substring(8);
    resolve(success, fail, path, window.PERSISTENT, UNSANDBOXED, options);
}

//validate parameters and set sandbox
function resolve(success, fail, path, fsType, sandbox, options, size) {
    options = options || { create: false };
    size = size || info.MAX_SIZE;
    if (size > info.MAX_SIZE) {
        //bb10 does not respect quota; fail at unreasonably large size
        fail(FileError.QUOTA_EXCEEDED_ERR);
    } else if (path.indexOf(':') > -1) {
        //files with : character are not valid in Cordova apps 
        fail(FileError.ENCODING_ERR);
    } else {
        requestAnimationFrame(function () {
            cordova.exec(function () {
                requestAnimationFrame(function () {
                    resolveNative(success, fail, path, fsType, options, size);
                });
            }, fail, 'File', 'setSandbox', [sandbox], false);
        });
    }
}

//find path using webkit file system
function resolveNative(success, fail, path, fsType, options, size) {
    window.webkitRequestFileSystem(
        fsType,
        size,
        function (fs) {
            if (path === '') {
                //no path provided, call success with root file system
                success(createEntryFromNative(fs.root));
            } else {
                //otherwise attempt to resolve as file
                fs.root.getFile(
                    path,
                    options,
                    function (entry) {
                        success(createEntryFromNative(entry));
                    },
                    function (fileError) {
                        //file not found, attempt to resolve as directory
                        fs.root.getDirectory(
                            path,
                            options,
                            function (entry) {
                                success(createEntryFromNative(entry));
                            },
                            function (dirError) {
                                //path cannot be resolved
                                if (fileError.code === FileError.INVALID_MODIFICATION_ERR && 
                                    options.exclusive) {
                                    //mobile-spec expects this error code
                                    fail(FileError.PATH_EXISTS_ERR);
                                } else {
                                    fail(FileError.NOT_FOUND_ERR);
                                }
                            }
                        );
                    }
                );
            }
        }
    );
}