##fs.js
This module provides a wrapper for the HTML5 File System API inspired in nodejs fs module. While the underlying HTML5 API is quite feature rich, it is low level and difficult to use.
fs.js provides a good tradeoff between easy to use and flexibility with a cleaner api.
Note that this module is inspired by node js, not a port, and will therefore not be totally compatible with it. Also the module provides some useful methods that only make sense on a browser environment (such as getting the url pointing to a local file).
Follow optimalbits for news and updates regarding this library.
##Install
Just pick up fs.js and include it in a script tag or use it with an AMD compatible loader. If you want to run the unit tests you can install the module via npm:
npm install fs.js
##Documentation
- rename
- stats
- exists
- remove
- rmdir
- mkdir
- mkpath
- readdir
- readFile
- readFileAsBlob
- readFileAsUrl
- writeFile
- appendFile
- wipe
##Unit tests
The unit tests are written using mocha. For running the unit tests you need to use node and a webbrowser with File System API capabilities (only Chrome at the moment).
Go to the test directory and run the test server:
node server.js
Open a webbrowser pointing to http://localhost:8080
##Quick Start
Start by creating a file system. Note that we support a prefix for every file system. This prefix allows us to have several independent file systems:
var sizeInBytes = 1024 * 1024,
prefix = 'filetest';
FSFactory.create(sizeInBytes, 'test_fs', function(err, fs) {
fs.read('foo', function(err, data){
// data contains file contents.
});
});
Check the methods section for all the methods provided by the file system object.
Renames a file or directory.
Arguments
oldPath {String} Old path.
newPath {String} New path name.
callback {Function} Callback called when finished.
Gives stats metadata of a file or directory
The received metadata in the callback contains the following properties: isFile, isDirectory, size, mtime (modification time)
Arguments
path {String} Path to file or directory.
callback {Function} Callback called when finished.
Checks if a file exists or not.
Arguments
path {String} Path to file or directory.
callback {Function} Callback called when finished.
Removes the given file from the filesystem.
Arguments
path {String} Path to file.
callback {Function} Callback called when finished.
Removes the dir at the given path (and all its contents including subdirs and files).
Arguments
path {String} Path to directory.
callback {Function} Callback called when finished.
Makes a directory in the given path.
Arguments
path {String} Path to directory.
callback {Function} Callback called when finished.
Makes a directory and all the subdirectories (if needed) to the given path.
Arguments
path {String} Path to directory.
callback {Function} Callback called when finished.
Reads the contents of a directory at the given path.
Arguments
path {String} Path to directory.
callback {Function} Callback called when finished.
Reads the content of a file specified at the given path. The contents of the file are returned as binary data (ArrayBuffer), or as text depending on the selected encoding.
Arguments
path {String} Path to a file.
encoding [{String}] Optional encoding (such as 'UTF-8'), otherwise returns data as
an ArrayBuffer.
callback {Function} Callback called when finished.
Reads the content of a file specified at the given path as a Blob.
Arguments
path {String} Path to a file.
callback {Function} Callback called when finished.
Reads the content of a file specified at the given path as a URL. This function is useful for setting local binary files as images, videos, sounds, fonts, etc.
Arguments
path {String} Path to a file.
callback {Function} Callback called when finished.
Writes a string, Blob or ArrayBuffer data to a file.
Arguments
path {String} Path to a file.
data {String|Blob|ArrayBuffer} Data to write to the file.
callback {Function} Callback called when finished.
Appends a string, Blob or ArrayBuffer to a file.
Arguments
path {String} Path to a file.
data {String|Blob|ArrayBuffer} Data to append to the file.
callback {Function} Callback called when finished.
Wipes the whole file system.
Use full = true if you want to wipe the root dir of the filesystem, after doing this, the instance cannot be used anymore.
Arguments
callback {Function} Callback called when finished.
full {Boolean} true if all filesystems should be removed.
##License
(The MIT License)
Copyright (c) 2012 Optimal Bits www.optimalbits.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.