Grid file storage
Simple GridFS filestorage with simple API
Install
npm i --save grid-filestorage
Usage
const fs = require('fs');
const path = require('path');
const FileStorage = require('grid-filestorage');
const filestorage = new FileStorage(db, { title: 'Test' });
const filestream = fs.createReadStream(path.join(__dirname, './test.png'));
const filename = 'testfile';
const meta = {
"content_type": 'image/png'
}
filestorage.write(filestream, filename, meta).
then(filename => console.info(`file ${filename} in storage`));
With mongoose
const mongoose = require('mongoose');
const FileStorage = require('grid-filestorage');
const filestorage = new FileStorage(mongoose.connection.db);
Methods
Write a file — filestorage.write(stream, filename = new ObjectID(), meta = {})
This mehod streams stream
into GridFS with filename
and meta
stream
— Readable streamfilename
— name of file, optional. If filename does not exist, filestorage will auto-generate itmeta
— meta information about file for GridFS, optional
{
content_type: 'image/png',
metadata:{
author: 'Daniel'
},
chunk_size: 1024 * 4
}
- returns Promise
- resolve saved filename
Read a file — filestorage.read(filename, isExists = true)
This method read file from GridFS by filename
filename
— name of file to read. If filestorage has title, filename must exclude title + '-'
partisExists
— if this flag is true, method will check file before read
- returns Promise
- resolve Readable Stream or null, if file does not exist
Remove a file — filestorage.remove(filename, isExists = true)
This method remove file from GridFS by filename
filename
— name of file to remove. If filestorage has title, filename must exclude title + '-'
partisExists
— if this flag is true, method will check file before remove
- returns Promise
- reject
FileStorageError
— File does not exist
, if file does not exist into GridFS