cordova-plugin-file
Advanced tools
Comparing version 0.2.1 to 0.2.3
{ | ||
"name": "cordova-plugin-file", | ||
"version": "0.2.1", | ||
"version": "0.2.3", | ||
"description": "Cordova File Plugin", | ||
@@ -5,0 +5,0 @@ "cordova": { |
@@ -845,2 +845,2 @@ /* | ||
require("cordova/commandProxy").add("File",module.exports); | ||
require("cordova/windows8/commandProxy").add("File",module.exports); |
@@ -28,4 +28,4 @@ /* | ||
fileUtils = require('./BB10Utils'), | ||
DirectoryEntry = function (name, fullPath) { | ||
DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath); | ||
DirectoryEntry = function (name, fullPath, fileSystem) { | ||
DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath, fileSystem); | ||
}; | ||
@@ -35,2 +35,9 @@ | ||
function err(sandboxState, errorCallback) { | ||
return function (e) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
errorCallback(e); | ||
} | ||
}; | ||
DirectoryEntry.prototype.createReader = function () { | ||
@@ -41,6 +48,30 @@ return new DirectoryReader(this.fullPath); | ||
DirectoryEntry.prototype.getDirectory = function (path, options, successCallback, errorCallback) { | ||
var sandboxState, | ||
currentPath = this.nativeEntry.fullPath; | ||
cordova.exec(function (sandboxed) { | ||
sandboxState = sandboxed; | ||
}, function (e) { | ||
console.log("[ERROR]: Could not retrieve sandbox state ", e); | ||
}, "org.apache.cordova.file", "isSandboxed"); | ||
argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments); | ||
this.nativeEntry.getDirectory(path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, errorCallback); | ||
if (fileUtils.isOutsideSandbox(path)) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [false]); | ||
window.webkitRequestFileSystem(window.PERSISTENT, this.filesystem._size, function (fs) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
fs.root.getDirectory(currentPath + '/' + path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, err(sandboxState, errorCallback)); | ||
}, err(sandboxState, errorCallback)); | ||
} else { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [true]); | ||
window.webkitRequestFileSystem(fileUtils.getFileSystemName(this.filesystem) === "persistent" ? window.PERSISTENT : window.TEMPORARY, this.filesystem._size, function (fs) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
fs.root.getDirectory(currentPath + '/' + path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, err(sandboxState, errorCallback)); | ||
}, err(sandboxState, errorCallback)); | ||
} | ||
}; | ||
@@ -54,8 +85,32 @@ | ||
DirectoryEntry.prototype.getFile = function (path, options, successCallback, errorCallback) { | ||
var sandboxState, | ||
currentPath = this.nativeEntry.fullPath; | ||
cordova.exec(function (sandboxed) { | ||
sandboxState = sandboxed; | ||
}, function (e) { | ||
console.log("[ERROR]: Could not retrieve sandbox state ", e); | ||
}, "org.apache.cordova.file", "isSandboxed"); | ||
argscheck.checkArgs('sOFF', 'DirectoryEntry.getFile', arguments); | ||
this.nativeEntry.getFile(path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, errorCallback); | ||
if (fileUtils.isOutsideSandbox(path)) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [false]); | ||
window.webkitRequestFileSystem(window.PERSISTENT, this.filesystem._size, function (fs) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
fs.root.getFile(currentPath + '/' + path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, err(sandboxState, errorCallback)); | ||
}, err(sandboxState, errorCallback)); | ||
} else { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [true]); | ||
window.webkitRequestFileSystem(fileUtils.getFileSystemName(this.filesystem) === "persistent" ? window.PERSISTENT: window.TEMPORARY, this.filesystem._size, function (fs) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
fs.root.getFile(currentPath + '/' + path, options, function (entry) { | ||
successCallback(fileUtils.createEntry(entry)); | ||
}, err(sandboxState, errorCallback)); | ||
}, err(sandboxState, errorCallback)); | ||
} | ||
}; | ||
module.exports = DirectoryEntry; |
@@ -27,6 +27,8 @@ /* | ||
this.path = path; | ||
this.nativeReader = null; | ||
} | ||
DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) { | ||
var win = typeof successCallback !== 'function' ? null : function(result) { | ||
var self = this, | ||
win = typeof successCallback !== 'function' ? null : function(result) { | ||
var retVal = []; | ||
@@ -41,9 +43,14 @@ for (var i=0; i<result.length; i++) { | ||
}; | ||
fileUtils.getEntryForURI(this.path, function (entry) { | ||
entry.nativeEntry.createReader().readEntries(win, fail); | ||
}, function () { | ||
fail(FileError.NOT_FOUND_ERR); | ||
}); | ||
if (this.nativeReader) { | ||
this.nativeReader.readEntries(win, fail); | ||
} else { | ||
resolveLocalFileSystemURI("filesystem:local:///persistent/" + this.path, function (entry) { | ||
self.nativeReader = entry.nativeEntry.createReader() | ||
self.nativeReader.readEntries(win, fail); | ||
}, function () { | ||
fail(FileError.NOT_FOUND_ERR); | ||
}); | ||
} | ||
}; | ||
module.exports = DirectoryReader; |
@@ -28,6 +28,10 @@ /* | ||
function Entry(isFile, isDirectory, name, fullPath, fileSystem) { | ||
var strippedPath; | ||
if (fullPath && fullPath.charAt(fullPath.length - 1) === '/') { | ||
strippedPath = fullPath.slice(0, -1); | ||
} | ||
this.isFile = !!isFile; | ||
this.isDirectory = !!isDirectory; | ||
this.name = name || ''; | ||
this.fullPath = fullPath || ''; | ||
this.fullPath = typeof strippedPath !== "undefined" ? strippedPath : (fullPath || ''); | ||
this.filesystem = fileSystem || null; | ||
@@ -92,3 +96,7 @@ } | ||
Entry.prototype.toURL = function() { | ||
return this.fullPath; | ||
var nativeURI = this.nativeEntry.toURL(); | ||
if (nativeURI.charAt(nativeURI.length - 1) === '/') { | ||
return nativeURI.slice(0, -1); | ||
} | ||
return nativeURI; | ||
}; | ||
@@ -95,0 +103,0 @@ |
@@ -25,6 +25,7 @@ /* | ||
FileWriter = require('./BB10FileWriter'), | ||
File = require('./File'), | ||
File = require('./BB10File'), | ||
fileUtils = require('./BB10Utils'), | ||
FileError = require('./FileError'), | ||
FileEntry = function (name, fullPath) { | ||
FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath]); | ||
FileEntry = function (name, fullPath, fileSystem) { | ||
FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath, fileSystem]); | ||
}; | ||
@@ -43,3 +44,4 @@ | ||
success = function (file) { | ||
successCallback(new File(file.name, fullPath, file.type, file.lastModifiedDate, file.size)); | ||
file.fullPath = fullPath; | ||
successCallback(fileUtils.createFile(file)); | ||
}; | ||
@@ -46,0 +48,0 @@ this.nativeEntry.file(success, errorCallback); |
@@ -63,3 +63,3 @@ /* | ||
if (file.fullPath) { | ||
fileUtils.getEntryForURI(file.fullPath, function (entry) { | ||
resolveLocalFileSystemURI("filesystem:local:///persistent/" + file.fullPath, function (entry) { | ||
entry.nativeEntry.file(function (nativeFile) { | ||
@@ -66,0 +66,0 @@ context.nativeReader[method].call(context.nativeReader, nativeFile, encoding); |
@@ -27,9 +27,8 @@ /* | ||
module.exports = { | ||
createEntry: function (entry) { | ||
var cordovaEntry; | ||
if (entry.isFile) { | ||
cordovaEntry = new window.FileEntry(entry.name, convertPath(entry.toURL())); | ||
cordovaEntry = new window.FileEntry(entry.name, entry.fullPath, entry.filesystem); | ||
} else { | ||
cordovaEntry = new window.DirectoryEntry(entry.name, convertPath(entry.toURL())); | ||
cordovaEntry = new window.DirectoryEntry(entry.name, entry.fullPath, entry.filesystem); | ||
} | ||
@@ -40,10 +39,16 @@ cordovaEntry.nativeEntry = entry; | ||
getEntryForURI: function (uri, success, fail) { | ||
//TODO: account for local vs file system | ||
window.resolveLocalFileSystemURI(uri, success, fail); | ||
createFile: function (file) { | ||
var cordovaFile = new File(file.name, file.fullPath, file.type, file.lastModifiedDate, file.size); | ||
cordovaFile.nativeFile = file; | ||
cordovaFile.fullPath = file.name; | ||
return cordovaFile; | ||
}, | ||
getFileSystemName: function (fs) { | ||
return (fs.name.indexOf('Persistent') != -1) ? 'persistent' : 'temporary'; | ||
return ((fs.name.indexOf('Persistent') != -1) || (fs.name === "persistent")) ? 'persistent' : 'temporary'; | ||
}, | ||
isOutsideSandbox: function (path) { | ||
return (path.indexOf("accounts/1000/") === 0 || path.indexOf("/accounts/1000/") === 0); | ||
} | ||
}; |
@@ -32,3 +32,3 @@ /* | ||
this.pending = []; | ||
fileUtils.getEntryForURI(file.fullPath, function (entry) { | ||
resolveLocalFileSystemURI("filesystem:local:///persistent/" + file.fullPath, function (entry) { | ||
entry.nativeEntry.createWriter(function (writer) { | ||
@@ -35,0 +35,0 @@ var i, |
@@ -27,2 +27,4 @@ /* | ||
module.exports = function (type, size, success, fail) { | ||
var cordovaFs, | ||
cordovaFsRoot; | ||
if (size >= 1000000000000000) { | ||
@@ -34,3 +36,7 @@ fail(new FileError(FileError.QUOTA_EXCEEDED_ERR)); | ||
window.webkitRequestFileSystem(type, size, function (fs) { | ||
success((new FileSystem(fileUtils.getFileSystemName(fs), fileUtils.createEntry(fs.root)))); | ||
cordovaFsRoot = fileUtils.createEntry(fs.root); | ||
cordovaFs = new FileSystem(fileUtils.getFileSystemName(fs), cordovaFsRoot); | ||
cordovaFsRoot.filesystem = cordovaFs; | ||
cordovaFs._size = size; | ||
success(cordovaFs); | ||
}, function (error) { | ||
@@ -37,0 +43,0 @@ fail(new FileError(error)); |
@@ -25,32 +25,32 @@ /* | ||
function stripURI(uri) { | ||
var rmFsLocal = uri.substring("filesystem:local:///".length); | ||
return rmFsLocal.substring(rmFsLocal.indexOf('/') + 1); | ||
} | ||
module.exports = function (uri, success, fail) { | ||
var type, | ||
path, | ||
paramPath; | ||
if (!uri || uri.indexOf("/") === 0) { | ||
fail(new FileError(FileError.ENCODING_ERR)); | ||
var sandboxState, | ||
decodedURI = decodeURI(uri); | ||
cordova.exec(function (sandboxed) { | ||
sandboxState = sandboxed; | ||
}, function (e) { | ||
console.log("[ERROR]: Could not retrieve sandbox state ", e); | ||
}, "org.apache.cordova.file", "isSandboxed"); | ||
if (fileUtils.isOutsideSandbox(stripURI(decodedURI))) { | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [false]); | ||
} else { | ||
type = uri.indexOf("persistent") === -1 ? 0 : 1; | ||
path = uri.substring(type === 1 ? uri.indexOf("persistent") + 11 : uri.indexOf("temporary") + 10); | ||
if (path.substring(0,1) == "/") { | ||
path = path.substring(1); | ||
} | ||
paramPath = path.indexOf("?"); | ||
if (paramPath > -1) { | ||
path = path.substring(0, paramPath); | ||
} | ||
window.webkitRequestFileSystem(type, 25*1024*1024, function (fs) { | ||
if (path === "") { | ||
success(fileUtils.createEntry(fs.root)); | ||
} else { | ||
fs.root.getDirectory(path, {}, function (entry) { | ||
success(fileUtils.createEntry(entry)); | ||
}, function () { | ||
fs.root.getFile(path, {}, function (entry) { | ||
success(fileUtils.createEntry(entry)); | ||
}, fail); | ||
}); | ||
} | ||
}, fail); | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [true]); | ||
} | ||
window.webkitResolveLocalFileSystemURL(decodedURI, function (entry) { | ||
success(fileUtils.createEntry(entry)); | ||
}, function (e) { | ||
window.webkitResolveLocalFileSystemURL(decodedURI + '/', function (entry) { | ||
success(fileUtils.createEntry(entry)); | ||
}, function (e) { | ||
fail(e); | ||
}); | ||
}); | ||
cordova.exec(null, null, "org.apache.cordova.file", "setSandbox", [sandboxState]); | ||
}; |
@@ -110,3 +110,3 @@ /* | ||
// create appropriate Entry object | ||
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath) : new (require('org.apache.cordova.core.file.FileEntry'))(entry.name, entry.fullPath); | ||
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath); | ||
successCallback(result); | ||
@@ -152,3 +152,3 @@ } | ||
// create appropriate Entry object | ||
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath) : new (require('org.apache.cordova.core.file.FileEntry'))(entry.name, entry.fullPath); | ||
var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath); | ||
successCallback(result); | ||
@@ -155,0 +155,0 @@ } |
@@ -22,14 +22,3 @@ /* | ||
var exec = require('cordova/exec'); | ||
/** | ||
* Represents a local file system. | ||
*/ | ||
var LocalFileSystem = function() { | ||
}; | ||
LocalFileSystem.TEMPORARY = 0; //temporary, with no guarantee of persistence | ||
LocalFileSystem.PERSISTENT = 1; //persistent | ||
module.exports = LocalFileSystem; | ||
exports.TEMPORARY = 0; | ||
exports.PERSISTENT = 1; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
677160
79
10703