cordova-promise-fs
Advanced tools
Comparing version 0.2.0 to 0.3.0
67
index.js
@@ -32,27 +32,4 @@ /** | ||
/* default fileErrorHandler */ | ||
var __defaultErrorHandler = function(err) { | ||
var msg = ''; | ||
switch (err.code) { | ||
case FileError.QUOTA_EXCEEDED_ERR: | ||
msg = 'QUOTA_EXCEEDED_ERR'; | ||
break; | ||
case FileError.NOT_FOUND_ERR: | ||
msg = 'NOT_FOUND_ERR'; | ||
break; | ||
case FileError.SECURITY_ERR: | ||
msg = 'SECURITY_ERR'; | ||
break; | ||
case FileError.INVALID_MODIFICATION_ERR: | ||
msg = 'INVALID_MODIFICATION_ERR'; | ||
break; | ||
case FileError.INVALID_STATE_ERR: | ||
msg = 'INVALID_STATE_ERR'; | ||
break; | ||
default: | ||
msg = err; | ||
break; | ||
} | ||
console.error(msg); | ||
}; | ||
var transferQueue = [], // queued fileTransfers | ||
inprogress = 0; // currently active filetransfers | ||
@@ -63,8 +40,5 @@ /** | ||
module.exports = function(options){ | ||
/* default error handler */ | ||
var handleError = options.errorHandler;// || __defaultErrorHandler; | ||
/* Promise implementation */ | ||
var Promise = options.Promise || window.Promise; | ||
if(!Promise) { handleError(new Error("No Promise library given in options.Promise")); } | ||
if(!Promise) { throw new Error("No Promise library given in options.Promise"); } | ||
@@ -263,3 +237,12 @@ /* default options */ | ||
/* list contents of a directory */ | ||
function list(path,getAsEntries) { | ||
function list(path,mode) { | ||
var recursive = mode.indexOf('r') > -1; | ||
var getAsEntries = mode.indexOf('e') > -1; | ||
var onlyFiles = mode.indexOf('f') > -1; | ||
var onlyDirs = mode.indexOf('d') > -1; | ||
if(onlyFiles && onlyDirs) { | ||
onlyFiles = false; | ||
onlyDirs = false; | ||
} | ||
return dir(path).then(function(dirEntry){ | ||
@@ -269,4 +252,18 @@ return new Promise(function(resolve,reject){ | ||
dirReader.readEntries(function(entries) { | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
var promises = [Promise.resolve(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
@@ -277,6 +274,2 @@ }); | ||
var transferQueue = [], // queued fileTransfers | ||
inprogress = 0; // currently active filetransfers | ||
// Whenever we want to start a transfer, we call popTransferQueue | ||
@@ -350,3 +343,3 @@ function popTransferQueue(){ | ||
return window.fs = { | ||
return { | ||
fs: fs, | ||
@@ -353,0 +346,0 @@ file: file, |
{ | ||
"name": "cordova-promise-fs", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Cordova FileSystem convienence functions that return promises.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
cordova-promise-fs | ||
========== | ||
> Wraps the Cordova File API in convenient functions (that return a Promise) | ||
Are you entangled in a async callback mess to get even the simplest task done? | ||
Are you entangled in a async callback mess to get even the simplest task done? Wait no longer -- here is **cordova-promise-fs**! | ||
Wait no longer -- here is **cordova-promise-fs**! | ||
## Getting started | ||
@@ -22,6 +21,5 @@ | ||
### Initialize & configuration | ||
```javascript | ||
var CordovaFS = require('cordova-fs-promise'); | ||
// Initialize a 'CordovaFsPromise' | ||
var fs = CordovaFS({ | ||
@@ -33,11 +31,22 @@ persistent: true, // or false | ||
}); | ||
``` | ||
// Browsing files | ||
**Note on concurrency:** Concurrent uploads/downloads completely trash your mobile application. That's why I've put a concurrency limit on the number of downloads/uploads. Meteor sets this number on 30. In my experimental testing, I found 3 much more reasonable. | ||
### Browsing files | ||
```javascript | ||
fs.exists(filename) // checks if file exists. returns fileEntry or false. | ||
fs.file(filename) // returns a fileEntry | ||
fs.dir(path) // returns a dirEntry | ||
fs.list(path) // return array with filenames (including path) | ||
fs.list(path,true) // return an array with entries. | ||
fs.list(path,optionString)// return array with filenames (including path) | ||
// Read and get data | ||
optionString = 'r' // recursive list | ||
optionString = 'd' // only list directories | ||
optionString = 'f' // only list files | ||
optionString = 'e' // return results as FileEntry/DirectoryEntry (instead as path-string) | ||
optionString = 'rfe' // mix options! return entries of all files, recursively | ||
``` | ||
### Reading files | ||
```javascript | ||
fs.read(filename) // returns text-content of a file | ||
@@ -48,7 +57,11 @@ fs.readJSON(filename) // returns JSON-parsed contents of a file | ||
fs.toDataURL(filename) // returns Base64 encoded Data URI | ||
``` | ||
// Write data | ||
### Writing files | ||
```javascript | ||
fs.write(filename,data) // writes a Blob, a String, or data (as JSON). Ensures directory exists. | ||
``` | ||
// Modifying files | ||
### File operations | ||
```javascript | ||
fs.create(filename) // creates a file | ||
@@ -61,4 +74,6 @@ fs.ensure(path) // ensures directory exists | ||
fs.removeDir(path) | ||
``` | ||
// Upload and Download | ||
### Upload and download | ||
```javascript | ||
var promise = fs.upload(source,destination,[options],[onprogress]); | ||
@@ -75,4 +90,6 @@ var promise = fs.upload(source,destination,[onprogress]); | ||
fs.upload(...).then(...) // won't return the augmented promise, just an ordinary one! | ||
``` | ||
// Utilities | ||
### Utilities | ||
```javascript | ||
fs.fs // returns promise for the FileSystem | ||
@@ -83,2 +100,12 @@ fs.filename(path) // converts path to filename (last part after /) | ||
## Changelog | ||
### 0.3.0 (05/11/2014) | ||
* Added `list` options (list `r`ecursively, only `f`iles, only `d`irectories, return result as `e`ntries) | ||
### 0.2.0 (05/11/2014) | ||
* Added `upload` and `download` methods with concurrency limit | ||
## Contribute | ||
@@ -85,0 +112,0 @@ |
15056
114
325