cordova-promise-fs
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -114,2 +114,4 @@ var CordovaPromiseFS = | ||
var Promise = options.Promise || window.Promise; | ||
var CDV_INTERNAL_URL_ROOT = 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/'); | ||
var CDV_URL_ROOT = ''; | ||
if(!Promise) { throw new Error("No Promise library given in options.Promise"); } | ||
@@ -119,2 +121,3 @@ | ||
options = options || {}; | ||
options.crosswalk = !!options.crosswalk; | ||
options.persistent = options.persistent !== undefined? options.persistent: true; | ||
@@ -127,3 +130,5 @@ options.storageSize = options.storageSize || 20*1024*1024; | ||
/* Cordova deviceready promise */ | ||
var deviceready, isCordova = typeof cordova !== 'undefined'; | ||
var deviceready, | ||
isCordova = typeof cordova !== 'undefined' && !options.crosswalk, | ||
isCrosswalk = options.crosswalk; | ||
if(isCordova){ | ||
@@ -134,2 +139,4 @@ deviceready = new Promise(function(resolve,reject){ | ||
}); | ||
} else if(isCrosswalk) { | ||
deviceready = ResolvedPromise(true); | ||
} else { | ||
@@ -140,24 +147,2 @@ /* FileTransfer implementation for Chrome */ | ||
window.requestFileSystem = webkitRequestFileSystem; | ||
window.FileTransfer = function FileTransfer(){}; | ||
FileTransfer.prototype.download = function download(url,file,win,fail) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.responseType = "blob"; | ||
xhr.onreadystatechange = function(onSuccess, onError, cb) { | ||
if (xhr.readyState == 4) { | ||
if(xhr.status === 200 && !this._aborted){ | ||
write(file,xhr.response).then(win,fail); | ||
} else { | ||
fail(xhr.status); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
FileTransfer.prototype.abort = function(){ | ||
this._aborted = true; | ||
}; | ||
window.ProgressEvent = function ProgressEvent(){}; | ||
window.FileEntry = function FileEntry(){}; | ||
} else { | ||
@@ -170,2 +155,28 @@ window.requestFileSystem = function(x,y,z,fail){ | ||
// Polyfill Filetransfer | ||
if(!isCordova){ | ||
window.FileTransfer = function FileTransfer(){}; | ||
FileTransfer.prototype.download = function download(url,file,win,fail) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.responseType = "blob"; | ||
xhr.onreadystatechange = function(onSuccess, onError, cb) { | ||
if (xhr.readyState == 4) { | ||
if(xhr.status === 200 && !this._aborted){ | ||
write(file,xhr.response).then(win,fail); | ||
} else { | ||
fail(xhr.status); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
FileTransfer.prototype.abort = function(){ | ||
this._aborted = true; | ||
}; | ||
window.ProgressEvent = function ProgressEvent(){}; | ||
window.FileEntry = function FileEntry(){}; | ||
} | ||
/* Promise resolve helper */ | ||
@@ -185,17 +196,27 @@ function ResolvedPromise(value){ | ||
} | ||
// Crosswalk | ||
if(isCrosswalk){ | ||
const system = options.fileSystem || 'cachedir'; | ||
xwalk.experimental.native_file_system.requestNativeFileSystem(system,function(fs){ | ||
fs.root.getDirectory(system, {create: false}, function(dirEntry) { | ||
resolve({ | ||
name: fs.name, | ||
root: dirEntry | ||
}); | ||
}, reject); | ||
},reject); | ||
// On chrome, request quota to store persistent files | ||
if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
} else if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
navigator.webkitPersistentStorage.requestQuota(options.storageSize, function(grantedBytes) { | ||
window.requestFileSystem(type, grantedBytes, resolve, reject); | ||
}, reject); | ||
// Exotic Cordova Directories (options.fileSystem = string) | ||
} else if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
// Normal browser usage | ||
} else { | ||
// Exotic Cordova Directories (options.fileSystem = string) | ||
if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
// Normal browser usage | ||
} else { | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
@@ -349,4 +370,2 @@ | ||
var toInternalURL,toInternalURLSync,toURLSync; | ||
CDV_INTERNAL_URL_ROOT = 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/'); | ||
CDV_URL_ROOT = ''; | ||
if(isCordova) { | ||
@@ -369,2 +388,14 @@ /* synchronous helper to get internal URL. */ | ||
}; | ||
} else if(isCrosswalk){ | ||
/* synchronous helper to get internal URL. */ | ||
toInternalURLSync = function(path){ | ||
path = normalize(path); | ||
return path.indexOf('://') < 0? CDV_INTERNAL_URL_ROOT + path: path; | ||
}; | ||
toInternalURL = function(path) { | ||
return file(path).then(function(fileEntry) { | ||
return fileEntry.toInternalURL(); | ||
}); | ||
}; | ||
toURLSync = toInternalURLSync; | ||
} else { | ||
@@ -645,2 +676,3 @@ /* synchronous helper to get internal URL. */ | ||
toURL:toURL, | ||
toURLSync: toURLSync, | ||
isCordova:isCordova, | ||
@@ -647,0 +679,0 @@ toInternalURLSync: toInternalURLSync, |
102
index.js
@@ -67,2 +67,4 @@ /** | ||
var Promise = options.Promise || window.Promise; | ||
var CDV_INTERNAL_URL_ROOT = 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/'); | ||
var CDV_URL_ROOT = ''; | ||
if(!Promise) { throw new Error("No Promise library given in options.Promise"); } | ||
@@ -72,2 +74,3 @@ | ||
options = options || {}; | ||
options.crosswalk = !!options.crosswalk; | ||
options.persistent = options.persistent !== undefined? options.persistent: true; | ||
@@ -80,3 +83,5 @@ options.storageSize = options.storageSize || 20*1024*1024; | ||
/* Cordova deviceready promise */ | ||
var deviceready, isCordova = typeof cordova !== 'undefined'; | ||
var deviceready, | ||
isCordova = typeof cordova !== 'undefined' && !options.crosswalk, | ||
isCrosswalk = options.crosswalk; | ||
if(isCordova){ | ||
@@ -87,2 +92,4 @@ deviceready = new Promise(function(resolve,reject){ | ||
}); | ||
} else if(isCrosswalk) { | ||
deviceready = ResolvedPromise(true); | ||
} else { | ||
@@ -93,24 +100,2 @@ /* FileTransfer implementation for Chrome */ | ||
window.requestFileSystem = webkitRequestFileSystem; | ||
window.FileTransfer = function FileTransfer(){}; | ||
FileTransfer.prototype.download = function download(url,file,win,fail) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.responseType = "blob"; | ||
xhr.onreadystatechange = function(onSuccess, onError, cb) { | ||
if (xhr.readyState == 4) { | ||
if(xhr.status === 200 && !this._aborted){ | ||
write(file,xhr.response).then(win,fail); | ||
} else { | ||
fail(xhr.status); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
FileTransfer.prototype.abort = function(){ | ||
this._aborted = true; | ||
}; | ||
window.ProgressEvent = function ProgressEvent(){}; | ||
window.FileEntry = function FileEntry(){}; | ||
} else { | ||
@@ -123,2 +108,28 @@ window.requestFileSystem = function(x,y,z,fail){ | ||
// Polyfill Filetransfer | ||
if(!isCordova){ | ||
window.FileTransfer = function FileTransfer(){}; | ||
FileTransfer.prototype.download = function download(url,file,win,fail) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.responseType = "blob"; | ||
xhr.onreadystatechange = function(onSuccess, onError, cb) { | ||
if (xhr.readyState == 4) { | ||
if(xhr.status === 200 && !this._aborted){ | ||
write(file,xhr.response).then(win,fail); | ||
} else { | ||
fail(xhr.status); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
FileTransfer.prototype.abort = function(){ | ||
this._aborted = true; | ||
}; | ||
window.ProgressEvent = function ProgressEvent(){}; | ||
window.FileEntry = function FileEntry(){}; | ||
} | ||
/* Promise resolve helper */ | ||
@@ -138,17 +149,27 @@ function ResolvedPromise(value){ | ||
} | ||
// Crosswalk | ||
if(isCrosswalk){ | ||
const system = options.fileSystem || 'cachedir'; | ||
xwalk.experimental.native_file_system.requestNativeFileSystem(system,function(fs){ | ||
fs.root.getDirectory(system, {create: false}, function(dirEntry) { | ||
resolve({ | ||
name: fs.name, | ||
root: dirEntry | ||
}); | ||
}, reject); | ||
},reject); | ||
// On chrome, request quota to store persistent files | ||
if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
} else if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
navigator.webkitPersistentStorage.requestQuota(options.storageSize, function(grantedBytes) { | ||
window.requestFileSystem(type, grantedBytes, resolve, reject); | ||
}, reject); | ||
// Exotic Cordova Directories (options.fileSystem = string) | ||
} else if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
// Normal browser usage | ||
} else { | ||
// Exotic Cordova Directories (options.fileSystem = string) | ||
if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
// Normal browser usage | ||
} else { | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
@@ -302,4 +323,2 @@ | ||
var toInternalURL,toInternalURLSync,toURLSync; | ||
CDV_INTERNAL_URL_ROOT = 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/'); | ||
CDV_URL_ROOT = ''; | ||
if(isCordova) { | ||
@@ -322,2 +341,14 @@ /* synchronous helper to get internal URL. */ | ||
}; | ||
} else if(isCrosswalk){ | ||
/* synchronous helper to get internal URL. */ | ||
toInternalURLSync = function(path){ | ||
path = normalize(path); | ||
return path.indexOf('://') < 0? CDV_INTERNAL_URL_ROOT + path: path; | ||
}; | ||
toInternalURL = function(path) { | ||
return file(path).then(function(fileEntry) { | ||
return fileEntry.toInternalURL(); | ||
}); | ||
}; | ||
toURLSync = toInternalURLSync; | ||
} else { | ||
@@ -598,2 +629,3 @@ /* synchronous helper to get internal URL. */ | ||
toURL:toURL, | ||
toURLSync: toURLSync, | ||
isCordova:isCordova, | ||
@@ -600,0 +632,0 @@ toInternalURLSync: toInternalURLSync, |
{ | ||
"name": "cordova-promise-fs", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Cordova FileSystem convienence functions that return promises.", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish":"webpack index.js dist/CordovaPromiseFS.js --output-library CordovaPromiseFS --output-library-target var", | ||
"prepublish": "webpack index.js dist/CordovaPromiseFS.js --output-library CordovaPromiseFS --output-library-target var", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -9,0 +9,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132517
3928