Storage 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
var storage = require('filestorage').create('/path/to/directory/');
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();
storage.insert(name, buffer, [custom], [fnCallback], [changelog]);
storage.insert('logo.png', '/users/petersirka/desktop/logo.png', 'my custom data', function(err, id, stat) {
console.log(id);
console.log(stat);
}, 'new logo');
var id = storage.insert('logo.png', fs.createReadStream('/users/petersirka/desktop/logo.png'));
console.log(id);
var id = storage.insert('plaintext.txt', 'YW55IGNhcm5hbCBwbGVhc3VyZS4=');
console.log(id);
var id = storage.insert('plaintext.txt', new Buffer('YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'base64'));
console.log(id);
storage.update(id, name, buffer, [custom], [fnCallback], [change]);
storage.update(1, 'logo.jpg', '/users/petersirka/desktop/logo.jpg', function(err, id, stat) {
console.log(id);
console.log(stat);
}, 'update logo');
storage.update(1, 'plaintext.txt', new Buffer('YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'base64'));
storage.update(id, fnCallback, [change]);
storage.update(1, function(err, header) {
if (err)
return;
header.custom = 'SOME NEW VAUE';
return header;
});
storage.remove(id, [fnCallback], [change]);
storage.remove(1, function(err) {
}, 'remove logo');
storage.remove(1);
storage.stat(id, fnCallback);
storage.stat(1, function(err, stat) {
});
storage.read(id, fnCallback);
storage.read(1, function(err, stream, stat) {
});
storage.pipe(id, req, res, download);
storage.pipe(1, request, response, true);
storage.pipe(1, request, response, 'mynewlogo.jpg');
storage.pipe(1, mystream);
storage.send(id, url, [fnCallback], [headers]);
storage.send(1, 'http://yoururladdress.com/upload/', function(err, response) {
if (!err)
console.log(response);
});
storage.send(1, 'http://yoururladdress.com/upload/');
storage.copy(id, directory, [fnCallback], [name]);
storage.copy(1, '/users/petersirka/desktop/');
storage.copy(1, '/users/petersirka/desktop/', 'mynewlogofromstorage.jpg');
storage.copy(1, '/users/petersirka/desktop/', function(err) {
});
storage.listing(fnCallback);
storage.listing(function(err, arr) {
console.log(arr);
});
storage.find(function(file) {
return file.custom && file.custom.license === true;
}, function(err, response) {
console.log(response);
});
storage.changelog(fnCallback);
storage.changelog(function(err, changes) {
console.log(changes);
});
storage.remove('changelog');
storage.path;
storage.options;
storage.onPrepare = function(filename, stat, next) {
next();
};
storage.on('error', function(err) {
});
storage.on('insert', function(id, stat) {
});
storage.on('update', function(id, stat) {
});
storage.on('remove', function(id) {
});
storage.on('send', function(id, stat, url) {
});
storage.on('copy', function(id, stat, stream, directory) {
});
storage.on('read', function(id, stat, stream) {
});
storage.on('pipe', function(id, stat, stream, req) {
});
storage.on('listing', function(arr) {
});
storage.on('changelog', function(arr) {
});
The MIT License
Copyright (c) 2012-2017 Peter Širka petersirka@gmail.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.
Contact
https://www.totaljs.com - petersirka@gmail.com