Skip to main content

Upload

This is responsible for filtering and uploading files to a specified directory with customizable naming conventions.

API Reference

Upload

Kind: Exported class

new Upload(files)

Create an instance of the Upload class

Example Basic usage:

import { Upload } from "@ecoflow/utils";

const file = ctx.request.files; //reference of the file that uploaded.
const upload = new Upload(file);

Available arguments :

ParameterTypeDescription
filesFile | File[]Object referencing to the uploaded files

Set Prefix

upload.setPrefix(prefix)Upload

Sets a prefix for an upload files.

Example Basic usage:

upload.setPrefix("upload");

Return: The Upload instance after setting the file prefix.

Available arguments :

ParameterTypeDescription
prefixstringSet a prefix for the upload files names.

Set Time Format

upload.setTimeFormat(timeFormat)Upload

Sets the time format for an upload file.

Example Basic usage:

upload.setTimeFormat("HH-mm-ss");

Return: The Upload instance after setting the file time format.

Available arguments :

ParameterTypeDescription
timeFormatstringText containing time format

Available Time Format Representations :

tokenmeaningexamples of output
HH24-hour with zero-padding23, 08
H24-hour23, 8
hh12-hour with zero-padding11, 08
h12-hour11, 8
Ameridiem (uppercase)AM, PM
mmminute with zero-padding14, 07
sssecond with zero-padding05, 10
ssecond5, 10
SSSmillisecond (high accuracy)753, 022
SSmillisecond (middle accuracy)75, 02
Smillisecond (low accuracy)7, 0
Ztime zone offset value+0100, -0800
ZZtime zone offset value with dash+01-00, -08-00

Set Date Format

upload.setDateFormat(dateFormat)Upload

Sets the date format for an upload file.

Example Basic usage:

upload.setDateFormat("DD-MM-YYYY");

Return: The Upload instance after setting the file date format.

Available arguments :

ParameterTypeDescription
dateFormatstringText containing date format

Available Date Format Representations :

tokenmeaningexamples of output
YYYYfour-digit year0999, 2015
YYtwo-digit year99, 01, 15
Yfour-digit year without zero-padding2, 44, 888, 2015
MMMMmonth name (long)January, December
MMMmonth name (short)Jan, Dec
MMmonth with zero-padding01, 12
Mmonth1, 12
DDdate with zero-padding02, 31
Ddate2, 31
ddddday of week (long)Friday, Sunday
dddday of week (short)Fri, Sun
ddday of weekFr, Su

Set Upload Location

upload.setUploadLocation(location)Upload

Sets the upload location and creates the directory if it doesn't exist.

Return: The Upload instance after setting the file location.

Available arguments :

ParameterTypeDescription
locationstringPath of the directory where to store the uploaded files

Filter Upload files

upload.filterTypes(types)Upload

Filters files based on their mimetype

Return: The Upload instance after filtering the files.

Available arguments :

ParameterTypeDescription
typesstring | string[]A single string or an array of strings containing filer mimetype

Filter Only Zips

upload.filterZips()Upload

Filters out files that are of type ZIP

Return: The Upload instance after filtering the files.

Upload files

upload.upload()Promise<string[]>

Asynchronously uploads files, renames them with a timestamp and prefix, moves them to a specified directory, and returns an array of the uploaded file names.

Return: A Promise of string containing the uploaded names

Static Methods

Default Upload Directory

getUploadDirectory()string

Return: The default Upload directory