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:
-
requestRequestHTTP Request containing a multipart body, it will be altered with parsing properties
-
fileFieldsArrayA 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 optionalMultipart 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
Summary
Gets file destination path for the given field.
Parameters:
-
fieldNameStringThe name of the field containing files
Returns:
String | Null:
The file destination according to the file field configuration
getFileName
Syntax
getFileName
(
async
-
originalFileName -
fieldName -
callback
Summary
Builds final file name.
It avoids collisions with existing files and sanitizes file name.
parse
Syntax
parse
(
async
-
callback
Summary
Parses multipart content of the request and performs uploads if any.
Parameters:
-
callbackFunctionThe function to call when done
- Error The error if an error occurred, null otherwise