OpenVeo server API for plugins

API Docs for: 4.3.1
Show:

MultipartParser

Summary

Defines a multipart parser to parse multipart requests.

Use MultipartParser to get fields from multipart requests (including files).

Constructor

MultipartParser

Syntax

MultipartParser

(
  • request
  • fileFields
  • [limits]
)

Summary

Parameters:

  • request Request

    HTTP Request containing a multipart body, it will be altered with parsing properties

  • fileFields Array

    A list of file field descriptors with:

    • {String} name The field name which contains the file
    • {String} destinationPath The destination directory where the file will be uploaded
    • {Number} [maxCount] The maximum number of files allowed for this field
  • [limits] Object optional

    Multipart limits configuration, for more information about available limits see Multer documentation (https://www.npmjs.com/package/multer#limits).

Example:

// Get multipart parser
var MultipartParser = require('@openveo/api').multipart.MultipartParser;

// Create a request parser expecting several files: files in photos "field" and a file in "videos" field
var parser = new MultipartParser(request, [
  {
    name: 'photos',
    destinationPath: '/tmp/photos',
    maxCount: 2
  },
  {
    name: 'videos',
    destinationPath: '/tmp/videos',
    maxCount: 1
  }
], {
  fieldNameSize: 100,
  fieldSize: 1024,
  fields: Infinity,
  fileSize: Infinity,
  files: Infinity,
  parts: Infinity,
  headerPairs: 2000
});

parser.parse(function(error) {
  if (error)
    console.log('Something went wrong when uploading');
  else
    console.log(request.files);
});

Methods

getFileDestination

Syntax

getFileDestination

(
  • fieldName
)
String | Null

Summary

Gets file destination path for the given field.

Parameters:

  • fieldName String

    The name of the field containing files

Returns:

String | Null:

The file destination according to the file field configuration

getFileName

Syntax

getFileName

(
  • originalFileName
  • fieldName
  • callback
)
async

Summary

Builds final file name.

It avoids collisions with existing files and sanitizes file name.

Parameters:

  • originalFileName String

    The original file name

  • fieldName String

    The name of the field containing the file

  • callback Function

    The function to call when done

    • Error The error if an error occurred, null otherwise
    • String The computed file name

parse

Syntax

parse

(
  • callback
)
async

Summary

Parses multipart content of the request and performs uploads if any.

Parameters:

  • callback Function

    The function to call when done

    • Error The error if an error occurred, null otherwise

Properties

fileFields

Syntax

fileFields

Array final

Summary

The list of file field descriptors.

limits

Syntax

limits

Object final

Summary

Multipart limits configuration.

request

Syntax

request

Request final

Summary

The HTTP request containing a multipart body.