File storage for file uploading.
npm install filestorageStorage for storing files
=========================
- IMPORTANT: __CLUSTER NOT SUPPORTED__
- Best use with www.totaljs.com
- perfect solution for web sites
- supports custom path
- supports insert, update and remove files
- supports reading files
- supports sending files via HTTP
- supports file listing
- supports changelog (insert, update, remove)
- __supports auto-pipe stream to response__
- __supports auto HTTP CACHING (via ETag)__
- supports adding custom attribute to each file
- supports file streaming via HTTP - Content Range
- supports custom handler executing before saving file to the storage
- __auto-read picture properties: width and height__
- auto-create directories and the good logic
- supports stat() and copy()
- 100% pure JavaScript
- MIT license
Installation
------------
```
npm install filestorage
How to storage store files?
---------------------------
> Directory contains max 1000 files. Each file has .data extension and each file contains internal META information (2 kB). Each directory contains the config file with the informations about all files.
/your-path/000-000-001/000000001.data
> if directory contains more than 999 files then storage automatically create a new directory:
/your-path/000-000-002/000001000.data
> The benefit: querying by the file ID {Number} and auto-pipe to HttpResponse
*
`javascript
var storage = require('filestorage').create('/path/to/directory/');
// Do you want to re-assign id of removed files? If yes, set:
// storage.reassign = true;
// You can create more file storages
// EXAMPLE:
var storage_users = require('filestorage').create('/path/to/users/');
var storage_products = require('filestorage').create('/path/to/products/');
var storage_logs = require('filestorage').create('/path/to/logs/');
var storage_default = require('filestorage').create();
// default path: /process–directory/filestorage/
// ================================================
// FILESTORAGE INSERT
// ================================================
/*
Insert a file
@name {String}
@buffer {String, Stream, Buffer}
@custom {String, Object} :: optional
@fnCallback {Function} :: optional, params: @err {Error}, @id {Number}, @stat {Object}
@change {String} :: optional, changelog
return {Number} :: file id
*/
storage.insert(name, buffer, [custom], [fnCallback], [changelog]);
// EXAMPLE:
storage.insert('logo.png', '/users/petersirka/desktop/logo.png', 'my custom data', function(err, id, stat) {
console.log(id);
console.log(stat);
// stat.name - file name
// stat.extension - file extension
// stat.length - file length
// stat.type - content type
// stat.width - picture width
// stat.height - picture height
// stat.custom - your custom value
// stat.stamp - date created ticks, new Date(stat.stamp)
}, 'new logo');
// OR
var id = storage.insert('logo.png', fs.createReadStream('/users/petersirka/desktop/logo.png'));
console.log(id);
// OR
var id = storage.insert('plaintext.txt', 'YW55IGNhcm5hbCBwbGVhc3VyZS4=');
console.log(id);
// OR
var id = storage.insert('plaintext.txt', new Buffer('YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'base64'));
console.log(id);
// ================================================
// FILESTORAGE UPDATE
// ================================================
/*
Update a file
@id {String or Number}
@name {String}
@buffer {String, Stream, Buffer}
@custom {String, Object} :: optional
@fnCallback {Function} :: optional, params: @err {Error}, @id {Number}, @stat {Object}
@change {String} :: optional, changelog
return {Number}
*/
storage.update(id, name, buffer, [custom], [fnCallback], [change]);
// EXAMPLE:
storage.update(1, 'logo.jpg', '/users/petersirka/desktop/logo.jpg', function(err, id, stat) {
console.log(id);
console.log(stat);
// stat.name - file name
// stat.extension - file extension
// stat.length - file length
// stat.type - content type
// stat.width - picture width
// stat.height - picture height
// stat.custom - your custom value
// stat.stamp - date created ticks, new Date(stat.stamp)
}, 'update logo');
// OR
storage.update(1, 'plaintext.txt', new Buffer('YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'base64'));
/*
Update a file
@id {String or Number}
@fnCallback {Function(err, header)} ---> MUST RETURN A NEW HEADER
@change {String} :: optional, changelog
return {Number}
*/
storage.update(id, fnCallback, [change]);
// EXAMPLE:
storage.update(1, function(err, header) {
if (err)
return;
header.custom = 'SOME NEW VAUE';
return header;
});
// ================================================
// FILESTORAGE REMOVE
// ================================================
/*
Remove a file
@id {String or Number}
@fnCallback {Function} :: optional, params: @err {Error}
@change {String} :: optional, changelog
return {FileStorage}
*/
storage.remove(id, [fnCallback], [change]);
// EXAMPLE:
storage.remove(1, function(err) {
// your code here
}, 'remove logo');
// OR
storage.remove(1);
// ================================================
// FILESTORAGE STAT
// ================================================
/*
A file information
@id {String or Number}
@fnCallback {Function} :: params: @err {Error}, @stat {Object}
return {FileStorage}
*/
storage.stat(id, fnCallback);
// EXAMPLE:
storage.stat(1, function(err, stat) {
// stat.name - file name
// stat.extension - file extension
// stat.length - file length
// stat.type - content type
// stat.width - picture width
// stat.height - picture height
// stat.custom - your custom value
// stat.stamp - date created ticks, new Date(stat.stamp)
});
// ================================================
// FILESTORAGE READ
// ================================================
/*
Read a file
@id {String or Number}
@fnCallback {Function} :: params: @err {Error}, @stream {ReadStream}, @stat {Object}
return {FileStorage}
*/
storage.read(id, fnCallback);
// EXAMPLE:
storage.read(1, function(err, stream, stat) {
// stat.name - file name
// stat.extension - file extension
// stat.length - file length
// stat.type - content type
// stat.width - picture width
// stat.height - picture height
// stat.custom - your custom value
// stat.stamp - date created ticks, new Date(stat.stamp)
// stream.pipe(yourstream)
});
// ================================================
// FILESTORAGE PIPE
// ================================================
/*
Pipe a stream to Stream or HttpResponse
@id {String or Number}
@req {HttpRequest} :: optional,
@res {HttpResponse or Stream}
@download {String or Boolean} :: optional, attachment - if string filename is download else if boolean filename will a stat.name
return {FileStorage}
*/
storage.pipe(id, req, res, download);
// EXAMPLE:
storage.pipe(1, request, response, true);
// OR
storage.pipe(1, request, response, 'mynewlogo.jpg');
// OR
storage.pipe(1, mystream);
// ================================================
// FILESTORAGE SEND
// ================================================
/*
Send a file through HTTP
@id {String or Number}
@url {String}
@fnCallback {Function} :: optional, params: @err {Error}, @response {String}
@headers {Object} :: optional, additional headers
return {FileStorage}
*/
storage.send(id, url, [fnCallback], [headers]);
// EXAMPLE:
//