2.2.0 • Published 2 years ago

busboy-upload v2.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

busboy-upload

This express middleware is handling fileuploads on top of connect-busboy. It provides an simple solution to deal with large files.

$ npm install busboy-upload

Content

  • Usage
  • API Response
  • Set up the express middleware
  • Informations about the uploaded file
  • Error Handling
  • Test your code

Usage

First of all, we need to define the callbacks to get informations about the upload

/**
 * Is fired when file has been successfully uploaded
 * @param {Object} fileInfo 
 */
const success = (file) => {

    console.log(file.stats); // { total: 20, error: 10, success: 10 }
    console.log(file.files); // Array of all files
    console.log(file.duration); //file upload duration in ms
    
}

const config = {
    uploadPath: __dirname + '/uploads/',
    uploadName: Date.now(),
    maxSize: 100000,
    mimeTypes: ['image/jpeg'], // Take a look into all aviable mime types here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
    maxsize: 100000, // In bytes
    filter: (file, deny) => {
        if(file.originalFile.filename == 'test.jpesg') return deny('INVALID_BIT_AMOUNT'); // Method needs to be returned!
    },
    onlyRead: true // Only reads the incoming form-data, appends an parameter chunks to file as a Buffer of the file content
}

Note: If you do not want to apply a filter, just leave the config parameter blanc.

The parameter file in filter will have the format like this.

API Response

{
  stats: { total: 20, error: 10, success: 10 },
  files: [],
  duration: 1041 //in ms
}

Set up the middleware

The API needs to be included in one of the express Routers:

const busboy = require('busboy');
const fileUpload = require('busboy-upload');

app.use(busboy({ immediate: true }));

app.use('/upload', (req, res) => {

  // Callbacks from above
  
  fileUpload(req, success, config);
});

File Object

File has been successfully uploaded:

{
  errors: [],
  originalFile: {
    filename: 'test.jpeg',
    encoding: '7bit',
    mimeType: 'image/jpeg',
    uploadedPath: 'uploads/file9-test.jpeg-1655893178067.jpeg'
  },
  uploadedFile: {
    uploadedName: 'file9-test.jpeg-1655893178067',
    uploadedPath: 'uploads/file9-test.jpeg-1655893178067.jpeg',
    uploadDuration: 1,
    success: true,
    fileInfo: {
      _readableState: [Object],
      _events: {},
      _eventsCount: 0,
      truncated: false,
      _readcb: null
    },
    size: 89090
  }
}

File has been not been successfully uploaded:

{
  errors: [ 'FILE_TYPE_NOT_ALLOWED' ],
  originalFile: {
    filename: 'bomb.zip',
    encoding: '7bit',
    mimeType: 'application/zip'
  },
  uploadedFile: {
    uploadedName: 'bomb9-bomb.zip-1655893178067',
    uploadedPath: 'uploads/bomb9-bomb.zip-1655893178067.zip',
    uploadDuration: 0,
    success: false,
    fileInfo: {
      _readableState: [Object],
      _events: {},
      _eventsCount: 0,
      truncated: false,
      _readcb: null
    }
  }
}

Note: The file size is provided in Bytes

Errors

The parameter error of a file returns one of these following errors. In the table below, you can learn how to deal with errors

ErrorMeaning
FILE_TYPE_NOT_ALLOWEDThe filetype does not match the provided query
UPLOADED_FILE_TO_BIGThe provided file exeedes the provided limit of bytes
MAXIMUM_FILE_AMOUNT_REACHEDThe maximum file size has been reached, no files after the limit will be uploaded
FILE_TYPE_NOT_ALLOWEDThe mime type is not allowed to be uploaded

Note: Other errors could occoure, in this case you probably did something wrong whith the config, open a new issues

Test your code with axios

To test your code, you can start uploading file with axios. For that, you need to have form-data, fs and axios installed, you can do that by running:

$ npm install fs form-data axios

Sample code:

const formData = require('form-data');
const fs = require('fs');

const axios = require('axios').default;

const init = async () => {
    const fd = new formData();

    fd.append('cat', fs.createReadStream('./miau.jpeg'));
    fd.append('dog', fs.createReadStream('./dog.jpeg'));

    const res = await axios.post('http://127.0.0.1:443/upload', fd, {
        maxBodyLength: Infinity,
        maxContentLength: Infinity,
            headers: {
                ...fd.getHeaders()
            }
    }).then(res => {
        console.log(res.data);
    }).catch(err => console.log(err));
}

init();

Made by Robert J. Kratz

License

MIT

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago