DFStorage
DFStorage is a module for the storage of file in different way. Currently supports storage in file system and in MongoDb GridFs.
Quickstart
var storages = ;var myGridFs = db: someMongoDbConnection;storages; //... var myStorage = storages;myStorage; //... { var myStorage = storages; myStorage;}
FileFsStorage
var FileFs = require('df-storage').FileFS;
var fileFs = new FileFs({
path: rootPath,
deep : 3,
contentType : 'text/html'
});
path
, define the root path of the storage in the filesystemdeep
, define the number of subfolders used for saving the file. (eg. If the file name/uid issomeFileName
and the deep value is equal to 3, will be saved in s/o/m/someFileName)contentType
, the default content type of the file saved
n addition to the original file will be saved another file containing
metadata with the name someFileName.json
.
filename
, the name/uid of the filecontentType
, The content type of the filelength
, The size of the filemetadata
, additional custom metadata
GridFsStorage
var GridFs = require('df-storage').GridFS;
var gridFs = new GridFs({
db: connectionManager.getConnection(),
collectionRoot: 'my-collection-root',
chunkSize: 10000,
contentType: 'html/zip'
});
db
, a mongodb conncetioncollectionRoot
, the base name of the mongoDb collection used by the gridFs.chunkSize
, the default size of the chunkcontentType
, the default content type of the file saved
in addition to the original file will be saved some metadata.
filename
, the name/uid of the filecontentType
, The content type of the filechunkSize
, the size of the chunklength
, The size of the filemetadata
, additional custom metadata
API
save(originalPath, UID, options) -> Promise()
Save a file from a source (originalPath) with the Id (UID)
- originalPath, the source
- UID, the UID in the storage
- options:
- metadata, additional custom metadata
- chunkSize, the size of chunk (only in the GridFs)
- contentType, the contentType
If chunkSize and contentType aren't defined the default values are used.
load(UID, readStream)
Load a file (UID) into the readStream
- UID, the UID in the storage
- readStream, a readStream
sendToResponse(UID, response)
Send the file directly to the responseObject
- UID, the UID in the storage
- response, http response object
Adds to the response data also contentType and fileSize
loadMeta(UID) -> Promise(metadata)
Load the metadata of the file
- UID, the UID in the storage
remove(UID) -> Promise()
Remove the file
- UID, the UID in the storage
exists(UID) -> Promise(exists)
Check if file exists
- UID, the UID in the storage
TODO
- Improve documentation
- Implement more storage (Eg. Amazon S3)
- Implement methods for different storage comunication
- Copy
- Sync
- Improve my English!! :)
Contacts
- Davide Fiorello davide@codeflyer.com
- GitHub
License
Copyright 2014 Davide Fiorello MIT License (enclosed)