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 = ; // 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 = ;var storage_products = ;var storage_logs = ; var storage_default = ; // 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; // EXAMPLE: storage; // OR var id = storage;console; // OR var id = storage;console; // OR var id = storage;console; // ================================================// 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; // EXAMPLE: storage; // OR storage; /* Update a file @id {String or Number} @fnCallback {Function(err, header)} ---> MUST RETURN A NEW HEADER @change {String} :: optional, changelog return {Number}*/storage; // EXAMPLE: storage; // ================================================// FILESTORAGE REMOVE// ================================================ /* Remove a file @id {String or Number} @fnCallback {Function} :: optional, params: @err {Error} @change {String} :: optional, changelog return {FileStorage}*/storage; // EXAMPLE: storage; // OR storage; // ================================================// FILESTORAGE STAT// ================================================ /* A file information @id {String or Number} @fnCallback {Function} :: params: @err {Error}, @stat {Object} return {FileStorage}*/storage; // EXAMPLE: storage; // ================================================// FILESTORAGE READ// ================================================ /* Read a file @id {String or Number} @fnCallback {Function} :: params: @err {Error}, @stream {ReadStream}, @stat {Object} return {FileStorage}*/storage; // EXAMPLE: storage; // ================================================// 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; // EXAMPLE: storage; // OR storage; // OR storage; // ================================================// 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; // EXAMPLE: // <form method="POST" action="http://yoururladdress.com/upload/" enctype="multipart/form-data">// <input type="file" name="File" /> storage; storage; // ================================================// FILESTORAGE COPY// ================================================ /* Copy file @id {String or Number} @directory {String} @fnCallback {Function} :: params: @err {Error} @name {String} :: optional, new filename return {FileStorage}*/storage; // EXAMPLE: storage;storage; storage; // ================================================// FILESTORAGE LISTING// ================================================ /* Get all file names @fnCallback {Function} :: params: @err {Error}, @arr {String Array} return {FileStorage}*/storage; // EXAMPLE: storage; // ================================================// +v1.7.0 FIND FILES// ================================================ storage; // ================================================// FILESTORAGE CHANGELOG// ================================================ /* Read the changelog @fnCallback {Function} :: params: @err {Error}, @changes {String Array} return {FileStorage}*/storage; // EXAMPLE: storage; // HOW TO REMOVE CHANGELOG? storage;
// ================================================// FILESTORAGE PROPERTIES// ================================================ // {String}, readonlystoragepath; // {Object}, readonlystorageoptions; // ================================================// FILESTORAGE DELEGATES// ================================================ ///* Prepare file before store in storage @filename {String} :: path to TEMPORARY FILE in a storage, this file will be replaced @stat {Object} @next {Function}*/storage { // 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 ;}; // ================================================// FILESTORAGE EVENTS// ================================================ storage; storage; storage; storage; storage; storage; storage; storage; storage; storage;
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.