Cordova plugin file und file-transfer geadded

This commit is contained in:
2016-01-08 00:06:37 +01:00
parent 23a833a31b
commit 36c7e2c8e2
234 changed files with 49402 additions and 81 deletions

View File

@@ -0,0 +1,51 @@
/*
*
* 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.
*
*/
/*
* FileProxy
*
* Register all File exec calls to be handled by proxy
*/
module.exports = {
copyTo: require('cordova-plugin-file.copyToProxy'),
getDirectory: require('cordova-plugin-file.getDirectoryProxy'),
getFile: require('cordova-plugin-file.getFileProxy'),
getFileMetadata: require('cordova-plugin-file.getFileMetadataProxy'),
getMetadata: require('cordova-plugin-file.getMetadataProxy'),
getParent: require('cordova-plugin-file.getParentProxy'),
moveTo: require('cordova-plugin-file.moveToProxy'),
readAsArrayBuffer: require('cordova-plugin-file.readAsArrayBufferProxy'),
readAsBinaryString: require('cordova-plugin-file.readAsBinaryStringProxy'),
readAsDataURL: require('cordova-plugin-file.readAsDataURLProxy'),
readAsText: require('cordova-plugin-file.readAsTextProxy'),
readEntries: require('cordova-plugin-file.readEntriesProxy'),
remove: require('cordova-plugin-file.removeProxy'),
removeRecursively: require('cordova-plugin-file.removeRecursivelyProxy'),
resolveLocalFileSystemURI: require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAllFileSystems: require('cordova-plugin-file.requestAllFileSystemsProxy'),
requestFileSystem: require('cordova-plugin-file.requestFileSystemProxy'),
setMetadata: require('cordova-plugin-file.setMetadataProxy'),
truncate: require('cordova-plugin-file.truncateProxy'),
write: require('cordova-plugin-file.writeProxy')
};
require('cordova/exec/proxy').add('File', module.exports);

View File

@@ -0,0 +1,46 @@
/*
*
* 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.
*
*/
/*
* FileSystem
*
* Translate temporary / persistent / root file paths
*/
var info = require("cordova-plugin-file.bb10FileSystemInfo");
module.exports = {
__format__: function(fullPath) {
switch (this.name) {
case 'temporary':
path = info.temporaryPath + fullPath;
break;
case 'persistent':
path = info.persistentPath + fullPath;
break;
case 'root':
path = 'file://' + fullPath;
break;
}
return window.encodeURI(path);
}
};

View File

@@ -0,0 +1,141 @@
/*
*
* 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.
*
*/
/*
* copyTo
*
* IN:
* args
* 0 - URL of entry to copy
* 1 - URL of the directory into which to copy/move the entry
* 2 - the new name of the entry, defaults to the current name
* move - if true, delete the entry which was copied
* OUT:
* success - entry for the copied file or directory
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args, move) {
var uri = args[0],
destination = args[1],
fileName = args[2],
copiedEntry,
onSuccess = function () {
resolve(
function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail,
[destination + copiedEntry.name]
);
},
onFail = function (error) {
if (typeof(fail) === 'function') {
if (error && error.code) {
//set error codes expected by mobile spec
if (uri === destination) {
fail(FileError.INVALID_MODIFICATION_ERR);
} else if (error.code === FileError.SECURITY_ERR) {
fail(FileError.INVALID_MODIFICATION_ERR);
} else {
fail(error.code);
}
} else {
fail(error);
}
}
},
writeFile = function (fileEntry, blob, entry) {
copiedEntry = fileEntry;
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function () {
if (move) {
entry.nativeEntry.remove(onSuccess, function () {
console.error("Move operation failed. Files may exist at both source and destination");
});
} else {
onSuccess();
}
};
fileWriter.onerror = onFail;
fileWriter.write(blob);
}, onFail);
},
copyFile = function (entry) {
if (entry.nativeEntry.file) {
entry.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function (e) {
var contents = new Uint8Array(this.result),
blob = new Blob([contents]);
resolve(function (destEntry) {
requestAnimationFrame(function () {
destEntry.nativeEntry.getFile(fileName, {create: true}, function (fileEntry) {
writeFile(fileEntry, blob, entry);
}, onFail);
});
}, onFail, [destination]);
};
reader.onerror = onFail;
reader.readAsArrayBuffer(file);
}, onFail);
} else {
onFail(FileError.INVALID_MODIFICATION_ERR);
}
},
copyDirectory = function (entry) {
resolve(function (destEntry) {
if (entry.filesystemName !== destEntry.filesystemName) {
console.error("Copying directories between filesystems is not supported on BB10");
onFail(FileError.INVALID_MODIFICATION_ERR);
} else {
entry.nativeEntry.copyTo(destEntry.nativeEntry, fileName, function () {
resolve(function (copiedDir) {
copiedEntry = copiedDir;
if (move) {
entry.nativeEntry.removeRecursively(onSuccess, onFail);
} else {
onSuccess();
}
}, onFail, [destination + fileName]);
}, onFail);
}
}, onFail, [destination]);
};
if (destination + fileName === uri) {
onFail(FileError.INVALID_MODIFICATION_ERR);
} else if (fileName.indexOf(':') > -1) {
onFail(FileError.ENCODING_ERR);
} else {
resolve(function (entry) {
if (entry.isDirectory) {
copyDirectory(entry);
} else {
copyFile(entry);
}
}, onFail, [uri]);
}
};

View File

@@ -0,0 +1,77 @@
/*
*
* 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.
*
*/
/*
* createEntryFromNative
*
* IN
* native - webkit Entry
* OUT
* returns Cordova entry
*/
var info = require('cordova-plugin-file.bb10FileSystemInfo'),
fileSystems = require('cordova-plugin-file.fileSystems');
module.exports = function (native) {
var entry = {
nativeEntry: native,
isDirectory: !!native.isDirectory,
isFile: !!native.isFile,
name: native.name,
fullPath: native.fullPath,
filesystemName: native.filesystem.name,
nativeURL: native.toURL()
},
persistentPath = info.persistentPath.substring(7),
temporaryPath = info.temporaryPath.substring(7);
//fix bb10 webkit incorrect nativeURL
if (native.filesystem.name === 'root') {
entry.nativeURL = 'file:///' + native.fullPath;
} else if (entry.nativeURL.indexOf('filesystem:local:///persistent/') === 0) {
entry.nativeURL = info.persistentPath + native.fullPath;
} else if (entry.nativeURL.indexOf('filesystem:local:///temporary') === 0) {
entry.nativeURL = info.temporaryPath + native.fullPath;
}
//translate file system name from bb10 webkit
if (entry.filesystemName === 'local__0:Persistent' || entry.fullPath.indexOf(persistentPath) !== -1) {
entry.filesystemName = 'persistent';
} else if (entry.filesystemName === 'local__0:Temporary' || entry.fullPath.indexOf(temporaryPath) !== -1) {
entry.filesystemName = 'temporary';
}
//add file system property (will be called sync)
fileSystems.getFs(entry.filesystemName, function (fs) {
entry.filesystem = fs;
});
//set root on fullPath for persistent / temporary locations
entry.fullPath = entry.fullPath.replace(persistentPath, "");
entry.fullPath = entry.fullPath.replace(temporaryPath, "");
//set trailing slash on directory
if (entry.isDirectory && entry.fullPath.substring(entry.fullPath.length - 1) !== '/') {
entry.fullPath += '/';
}
if (entry.isDirectory && entry.nativeURL.substring(entry.nativeURL.length - 1) !== '/') {
entry.nativeURL += '/';
}
//encode URL
entry.nativeURL = window.encodeURI(entry.nativeURL);
return entry;
};

View File

@@ -0,0 +1,72 @@
/*
*
* 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.
*
*/
/*
* getDirectory
*
* IN:
* args
* 0 - local filesytem URI for the base directory to search
* 1 - directory to be created/returned; may be absolute path or relative path
* 2 - options object
* OUT:
* success - DirectoryEntry
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0] === "/" ? "" : args[0],
dir = args[1],
options = args[2],
onSuccess = function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail = function (error) {
if (typeof(fail) === 'function') {
if (error && error.code) {
//set error codes expected by mobile-spec tests
if (error.code === FileError.INVALID_MODIFICATION_ERR && options.exclusive) {
fail(FileError.PATH_EXISTS_ERR);
} else if ( error.code === FileError.NOT_FOUND_ERR && dir.indexOf(':') > 0) {
fail(FileError.ENCODING_ERR);
} else {
fail(error.code);
}
} else {
fail(error);
}
}
};
resolve(function (entry) {
requestAnimationFrame(function () {
entry.nativeEntry.getDirectory(dir, options, function (nativeEntry) {
resolve(function (entry) {
onSuccess(entry);
}, onFail, [uri + "/" + dir]);
}, onFail);
});
}, onFail, [uri]);
};

View File

@@ -0,0 +1,57 @@
/*
*
* 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.
*
*/
/*
* getFile
*
* IN:
* args
* 0 - local filesytem URI for the base directory to search
* 1 - file to be created/returned; may be absolute path or relative path
* 2 - options object
* OUT:
* success - FileEntry
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy');
module.exports = function (success, fail, args) {
var uri = args[0] === "/" ? "" : args[0] + "/" + args[1],
options = args[2],
onSuccess = function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail = function (code) {
if (typeof(fail) === 'function') {
fail(code);
}
};
resolve(function (entry) {
if (!entry.isFile) {
onFail(FileError.TYPE_MISMATCH_ERR);
} else {
onSuccess(entry);
}
}, onFail, [uri, options]);
};

View File

@@ -0,0 +1,65 @@
/*
*
* 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.
*
*/
/*
* getFileMetadata
*
* IN:
* args
* 0 - local filesytem URI
* OUT:
* success - file
* - name
* - type
* - lastModifiedDate
* - size
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail = function (error) {
if (typeof(fail) === 'function') {
if (error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (entry) {
requestAnimationFrame(function () {
if (entry.nativeEntry.file) {
entry.nativeEntry.file(onSuccess, onFail);
} else {
entry.nativeEntry.getMetadata(onSuccess, onFail);
}
});
}, onFail, [uri]);
};

View File

@@ -0,0 +1,54 @@
/*
*
* 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.
*
*/
/*
* getMetadata
*
* IN:
* args
* 0 - local filesytem URI
* OUT:
* success - metadata
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail = function (error) {
if (typeof(fail) === 'function') {
if (error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (entry) {
entry.nativeEntry.getMetadata(onSuccess, onFail);
}, onFail, [uri]);
};

View File

@@ -0,0 +1,57 @@
/*
*
* 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.
*
*/
/*
* getParent
*
* IN:
* args
* 0 - local filesytem URI
* OUT:
* success - DirectoryEntry of parent
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (entry) {
if (typeof(success) === 'function') {
success(entry);
}
},
onFail = function (error) {
if (typeof(fail) === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (entry) {
requestAnimationFrame(function () {
entry.nativeEntry.getParent(onSuccess, onFail);
});
}, onFail, [uri]);
};

View File

@@ -0,0 +1,52 @@
/*
*
* 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.
*
*/
/*
* info
*
* persistentPath - full path to app sandboxed persistent storage
* temporaryPath - full path to app sandboxed temporary storage
* localPath - full path to app source (www dir)
* MAX_SIZE - maximum size for filesystem request
*/
var info = {
persistentPath: "",
temporaryPath: "",
localPath: "",
MAX_SIZE: 64 * 1024 * 1024 * 1024
};
cordova.exec(
function (path) {
info.persistentPath = 'file://' + path + '/webviews/webfs/persistent/local__0';
info.temporaryPath = 'file://' + path + '/webviews/webfs/temporary/local__0';
info.localPath = path.replace('/data', '/app/native');
},
function () {
console.error('Unable to determine local storage file path');
},
'File',
'getHomePath',
false
);
module.exports = info;

View File

@@ -0,0 +1,39 @@
/*
*
* 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.
*
*/
/*
* moveTo
*
* IN:
* args
* 0 - URL of entry to move
* 1 - URL of the directory into which to move the entry
* 2 - the new name of the entry, defaults to the current name
* OUT:
* success - entry for the copied file or directory
* fail - FileError
*/
var copy = cordova.require('cordova-plugin-file.copyToProxy');
module.exports = function (success, fail, args) {
copy(success, fail, args, true);
};

View File

@@ -0,0 +1,68 @@
/*
*
* 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.
*
*/
/*
* readAsArrayBuffer
*
* IN:
* args
* 0 - URL of file to read
* 1 - start position
* 2 - end position
* OUT:
* success - ArrayBuffer of file
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
start = args[1],
end = args[2],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function () {
onSuccess(this.result.slice(start, end));
};
reader.onerror = onFail;
reader.readAsArrayBuffer(file);
}, onFail);
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,68 @@
/*
*
* 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.
*
*/
/*
* readAsBinaryString
*
* IN:
* args
* 0 - URL of file to read
* 1 - start position
* 2 - end position
* OUT:
* success - BinaryString contents of file
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
start = args[1],
end = args[2],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function () {
onSuccess(this.result.substring(start, end));
};
reader.onerror = onFail;
reader.readAsBinaryString(file);
}, onFail);
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,65 @@
/*
*
* 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.
*
*/
/*
* readAsDataURL
*
* IN:
* args
* 0 - URL of file to read
* OUT:
* success - DataURL representation of file contents
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function () {
onSuccess(this.result);
};
reader.onerror = onFail;
reader.readAsDataURL(file);
}, onFail);
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,77 @@
/*
*
* 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.
*
*/
/*
* readAsText
*
* IN:
* args
* 0 - URL of file to read
* 1 - encoding
* 2 - start position
* 3 - end position
* OUT:
* success - text contents of file
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
start = args[2],
end = args[3],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function () {
var contents = new Uint8Array(this.result).subarray(start, end),
blob = new Blob([contents]),
textReader = new FileReader()._realReader;
textReader.onloadend = function () {
onSuccess(this.result);
};
textReader.onerror = onFail;
textReader.readAsText(blob);
};
reader.onerror = onFail;
reader.readAsArrayBuffer(file);
}, onFail);
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,71 @@
/*
*
* 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.
*
*/
/*
* readEntries
*
* IN:
* args
* 0 - URL of directory to list
* OUT:
* success - Array of Entry objects
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
info = require('cordova-plugin-file.bb10FileSystemInfo'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
createEntryFromNative = cordova.require('cordova-plugin-file.bb10CreateEntryFromNative');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
var reader = fs.nativeEntry.createReader(),
entries = [],
readEntries = function() {
reader.readEntries(function (results) {
if (!results.length) {
onSuccess(entries.sort().map(createEntryFromNative));
} else {
entries = entries.concat(Array.prototype.slice.call(results || [], 0));
readEntries();
}
}, onFail);
};
readEntries();
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,61 @@
/*
*
* 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.
*
*/
/*
* remove
*
* IN:
* args
* 0 - URL of Entry to remove
* OUT:
* success - (no args)
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
if (fs.fullPath === '/') {
onFail(FileError.NO_MODIFICATION_ALLOWED_ERR);
} else {
fs.nativeEntry.remove(onSuccess, onFail);
}
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,62 @@
/*
*
* 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.
*
*/
/*
* removeRecursively
*
* IN:
* args
* 0 - URL of DirectoryEntry to remove recursively
* OUT:
* success - (no args)
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error.code) {
if (error.code === FileError.INVALID_MODIFICATION_ERR) {
//mobile-spec expects this error code
fail(FileError.NO_MODIFICATION_ALLOWED_ERR);
} else {
fail(error.code);
}
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.removeRecursively(onSuccess, onFail);
});
}, fail, [uri]);
};

View File

@@ -0,0 +1,42 @@
/*
*
* 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.
*
*/
/*
* requestAllFileSystems
*
* IN - no arguments
* OUT
* success - Array of FileSystems
* - filesystemName
* - fullPath
* - name
* - nativeURL
*/
var info = require('cordova-plugin-file.bb10FileSystemInfo');
module.exports = function (success, fail, args) {
success([
{ filesystemName: 'persistent', name: 'persistent', fullPath: '/', nativeURL: info.persistentPath + '/' },
{ filesystemName: 'temporary', name: 'temporary', fullPath: '/', nativeURL: info.temporaryPath + '/' },
{ filesystemName: 'root', name: 'root', fullPath: '/', nativeURL: 'file:///' }
]);
}

View File

@@ -0,0 +1,38 @@
/*
*
* 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.
*
*/
/*
* requestAnimationFrame
*
* This is used throughout the BB10 File implementation to wrap
* native webkit calls. There is a bug in the webkit implementation
* which causes callbacks to never return when multiple file system
* APIs are called in sequence. This should also make the UI more
* responsive during file operations.
*
* Supported on BB10 OS > 10.1
*/
var requestAnimationFrame = window.requestAnimationFrame;
if (typeof(requestAnimationFrame) !== 'function') {
requestAnimationFrame = function (cb) { cb(); };
}
module.exports = requestAnimationFrame;

View File

@@ -0,0 +1,53 @@
/*
*
* 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.
*
*/
/*
* requestFileSystem
*
* IN:
* args
* 0 - type (TEMPORARY = 0, PERSISTENT = 1)
* 1 - size
* OUT:
* success - FileSystem object
* - name - the human readable directory name
* - root - DirectoryEntry object
* - isDirectory
* - isFile
* - name
* - fullPath
* fail - FileError code
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy');
module.exports = function (success, fail, args) {
var fsType = args[0] === 0 ? 'temporary' : 'persistent',
size = args[1],
onSuccess = function (fs) {
var directory = {
name: fsType,
root: fs
};
success(directory);
};
resolve(onSuccess, fail, ['cdvfile://localhost/' + fsType + '/', undefined, size]);
};

View File

@@ -0,0 +1,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);
}
}
);
}
);
}
}
);
}

View File

@@ -0,0 +1,33 @@
/*
*
* 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.
*
*/
/*
* setMetadata
*
* BB10 OS does not support setting file metadata via HTML5 File System
*/
module.exports = function (success, fail, args) {
console.error("setMetadata not supported on BB10", arguments);
if (typeof(fail) === 'function') {
fail();
}
};

View File

@@ -0,0 +1,74 @@
/*
*
* 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.
*
*/
/*
* truncate
*
* IN:
* args
* 0 - URL of file to truncate
* 1 - start position
* OUT:
* success - new length of file
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
length = args[1],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data.loaded);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.file(function (file) {
var reader = new FileReader()._realReader;
reader.onloadend = function () {
var contents = new Uint8Array(this.result).subarray(0, length);
blob = new Blob([contents]);
window.requestAnimationFrame(function () {
fs.nativeEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = onSuccess;
fileWriter.onerror = onFail;
fileWriter.write(blob);
}, onFail);
});
};
reader.onerror = onFail;
reader.readAsArrayBuffer(file);
}, onFail);
});
}, onFail, [uri]);
};

View File

@@ -0,0 +1,73 @@
/*
*
* 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.
*
*/
/*
* write
*
* IN:
* args
* 0 - URL of file to write
* 1 - data to write
* 2 - offset
* 3 - isBinary
* OUT:
* success - bytes written
* fail - FileError
*/
var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
module.exports = function (success, fail, args) {
var uri = args[0],
data = args[1],
offset = args[2],
isBinary = args[3],
onSuccess = function (data) {
if (typeof success === 'function') {
success(data.loaded);
}
},
onFail = function (error) {
if (typeof fail === 'function') {
if (error && error.code) {
fail(error.code);
} else if (error && error.target && error.target.code) {
fail(error.target.code);
} else {
fail(error);
}
}
};
resolve(function (fs) {
requestAnimationFrame(function () {
fs.nativeEntry.createWriter(function (writer) {
var blob = new Blob([data]);
if (offset) {
writer.seek(offset);
}
writer.onwriteend = onSuccess;
writer.onerror = onFail;
writer.write(blob);
}, onFail);
});
}, fail, [uri, { create: true }]);
};