2.0.1 • Published 10 months ago

@thesameeric/fileguard v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Description

A library for validating files. It can be used to validate local files and also files from formData. Recommended to use as a middleware with;

  • Multer
  • or any other multipart/formdata parser.

Requirements

Install

$ npm install --save @thesameeric/fileguard

Usage

// Validate local file
import { FileGuard } from '@thesameeric/filguard';
import * as path from 'path';

const filepath = path.join(__dirname, './Hotpot.png');
const { file, error } = new FileGuard(filepath).type(['epub', 'pdf']).size(1);

if (error) throw new Error('Custom error message');

Usage with multer

import * as express from 'express';
import * as multer from 'multer';
import { FileGuard } from '@thesameeric/filguard';

const upload = multer({ dest: 'uploads/' })
const app = express()

app.post('/file', upload.single('avatar'), (req, res, next) => {
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
    const { file, error } = new FileGuard(req.file).type(['png', 'jpg', 'jpeg', 'gif']).size(3);
    if(error) {
        // Output error
    }
})
2.0.1

10 months ago

2.0.0

10 months ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago