import { IncomingMessage, UploadxResponse } from '../types'; import { Cache, ErrorResponses, HttpError, HttpErrorBody, Locker, Logger, LogLevel, Validation, Validator } from '../utils'; import { File, FileInit, FilePart, FileQuery } from './file'; import { MetaStorage, UploadList } from './meta-storage'; export type UserIdentifier = (req: any, res: any) => string; export type OnCreate = (file: TFile) => Promise | TBody; export type OnUpdate = (file: TFile) => Promise | TBody; export type OnComplete = (file: TFile) => Promise | TBody; export type OnDelete = (file: TFile) => Promise | TBody; export type OnError = (error: HttpError) => any; export type PurgeList = UploadList & { maxAgeMs: number; }; export interface ExpirationOptions { /** * Age of the upload, after which it is considered expired and can be deleted */ maxAge: number | string; /** * Auto purging interval for expired uploads */ purgeInterval?: number | string; /** * Auto prolong expiring uploads */ rolling?: boolean; } export interface BaseStorageOptions { /** Allowed MIME types */ allowMIME?: string[]; /** File size limit */ maxUploadSize?: number | string; /** File naming function */ filename?: (file: T, req: any) => string; /** Get user identity */ userIdentifier?: UserIdentifier; /** Force relative URI in Location header */ useRelativeLocation?: boolean; /** Callback function that is called when a new upload is created */ onCreate?: OnCreate; /** Callback function that is called when an upload is updated */ onUpdate?: OnUpdate; /** Callback function that is called when an upload is completed */ onComplete?: OnComplete; /** Callback function that is called when an upload is cancelled */ onDelete?: OnDelete; /** Customize error response */ onError?: OnError; /** Node http base path */ path?: string; /** Upload validation options */ validation?: Validation; /** Limiting the size of custom metadata */ maxMetadataSize?: number | string; /** Provide custom meta storage */ metaStorage?: MetaStorage; /** * Automatic cleaning of abandoned and completed uploads * * @example * ```ts * app.use( * '/upload', * uploadx.upload({ * directory: 'upload', * expiration: { maxAge: '6h', purgeInterval: '30min' }, * onComplete * }) * ); * ``` */ expiration?: ExpirationOptions; /** Custom logger injection */ logger?: Logger; /** * Set built-in logger severity level * @defaultValue 'none' */ logLevel?: LogLevel; } export declare const locker: Locker; export declare abstract class BaseStorage { config: BaseStorageOptions; onCreate: (file: TFile) => Promise; onUpdate: (file: TFile) => Promise; onComplete: (file: TFile) => Promise; onDelete: (file: TFile) => Promise; onError: (err: HttpError) => UploadxResponse; maxUploadSize: number; maxMetadataSize: number; path: string; isReady: boolean; checksumTypes: string[]; errorResponses: ErrorResponses; cache: Cache; logger: Logger; protected namingFunction: (file: TFile, req: any) => string; protected validation: Validator; abstract meta: MetaStorage; protected constructor(config: BaseStorageOptions); validate(file: TFile): Promise; normalizeError(error: Error): HttpError; /** * Saves upload metadata */ saveMeta(file: TFile): Promise; /** * Deletes an upload metadata */ deleteMeta(id: string): Promise; /** * Retrieves upload metadata */ getMeta(id: string): Promise; checkIfExpired(file: TFile): Promise; /** * Searches for and purges expired uploads * @param maxAge - remove uploads older than a specified age * @param prefix - filter uploads */ purge(maxAge?: number | string, prefix?: string): Promise; get({ id }: FilePart): Promise; /** * Retrieves a list of uploads whose names begin with the prefix * @experimental */ list(prefix?: string): Promise; /** * Set user-provided metadata as key-value pairs * @experimental */ update({ id }: FileQuery, metadata: Partial): Promise; /** * Prevent upload from being accessed by multiple requests */ lock(key: string): Promise; unlock(key: string): Promise; protected isUnsupportedChecksum(algorithm?: string): boolean; protected startAutoPurge(purgeInterval: number): void; protected updateTimestamps(file: TFile): TFile; /** * Creates a new upload and saves its metadata */ abstract create(req: IncomingMessage, file: FileInit): Promise; /** * Write part and/or return status of an upload */ abstract write(part: FilePart | FileQuery): Promise; /** * Deletes an upload and its metadata */ abstract delete(query: FileQuery): Promise; }