Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grid-filestorage

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grid-filestorage

File storage module to save files into gridfs

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

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' });
// db — is the native db connection to mongodb
// options.title — title of storage, optional. Title prepend with "-" before filename

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`));
// file Test-testfile 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

  1. stream — Readable stream
  2. filename — name of file, optional. If filename does not exist, filestorage will auto-generate it
  3. meta — 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

  1. filename — name of file to read. If filestorage has title, filename must exclude title + '-' part
  2. isExists — 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

  1. filename — name of file to remove. If filestorage has title, filename must exclude title + '-' part
  2. isExists — if this flag is true, method will check file before remove
  • returns Promise
  • reject FileStorageErrorFile does not exist, if file does not exist into GridFS

Keywords

FAQs

Package last updated on 22 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc