import { DriveDirectory, DriveFile, DriveManager } from "flydrive"; import { Readable } from "node:stream"; import { DriverContract, ObjectMetaData, ObjectVisibility, SignedURLOptions, WriteOptions } from "flydrive/types"; import { GCSDriverOptions } from "flydrive/drivers/gcs/types"; //#region src/types.d.ts interface FileLike { originalname: string; buffer: Buffer; mimetype: string; } interface CustomDiskDriverRegistry {} type GcsDiskDriverConfig = GCSDriverOptions; interface FtpDriverConfig { host: string; username: string; password: string; port?: number; verbose?: boolean | undefined; privateKey?: string | undefined; } interface S3DriverConfig { credentials?: { accessKeyId: string; secretAccessKey: string; sessionToken?: string | undefined; credentialScope?: string | undefined; accountId?: string | undefined; }; url?: string; key?: string; secret?: string; endpoint?: string; region?: string; bucket: string; visibility: ObjectVisibility; cdnUrl?: string; } interface LocalDriverConfig { root?: string; location?: string | URL; visibility: ObjectVisibility; url?: string; } type CustomDiskConfig = keyof CustomDiskDriverRegistry extends never ? { driver: string; [key: string]: any; } : { [K in keyof CustomDiskDriverRegistry]: CustomDiskDriverRegistry[K] & { driver: K; }; }[keyof CustomDiskDriverRegistry]; type DiskConfig = LocalDriverConfig & { driver: 'local' | 'public'; } | FtpDriverConfig & { driver: 'ftp'; } | S3DriverConfig & { driver: 's3'; } | CustomDiskConfig; type DriverConfig = K extends 'ftp' ? FtpDriverConfig : K extends 's3' ? S3DriverConfig : K extends 'gcs' ? GcsDiskDriverConfig : K extends 'local' ? LocalDriverConfig : K extends keyof CustomDiskDriverRegistry ? CustomDiskDriverRegistry[K] : DiskConfig; type KnownDisks = { local: LocalDriverConfig & { driver: 'local'; }; public: LocalDriverConfig & { driver: 'local'; }; ftp: FtpDriverConfig & { driver: 'ftp'; }; gcs: GcsDiskDriverConfig & { driver: 'gcs'; }; s3: S3DriverConfig & { driver: 's3'; }; }; interface FilesystemConfig { default: 'local' | 'ftp' | 'gcs' | 's3' | keyof CustomDiskDriverRegistry | (string & {}); disks: KnownDisks & CustomDiskDriverRegistry; links: Record; custom_drivers?: Record DriverContract)>; fileNameGenerator?: (originalName: string) => string; } //#endregion //#region src/Storage.d.ts declare class Storage implements DriverContract { driver: DriveManager; services: Record DriverContract>; diskName: D; driverName: FilesystemConfig['disks'][D]['driver']; constructor(); /** * Static method to get a disk instance directly from the Storage class without needing to instantiate it first. * * @param diskName The name of the disk to use. If not provided, the default disk will be used. * @returns A Storage instance */ static disk(diskName?: K): Storage; /** * Generate a unique name for the file based on random numbers and original extension * * @param file The file object containing the original name * @returns A unique file name */ static generateName: (file: { name?: string; originalname?: string; }) => string; /** * Save the file to the storage and return the public URL and the file path * * @param file The file object containing the file data * @param filePath The path where the file should be saved * @param fileName The name to save the file as (optional) * @returns A tuple containing the public URL and the file path */ static saveFile: (file: FileLike, filePath?: string, fileName?: string) => Promise<[string, string]>; /** * Save the file to the storage and return the public URL and the file path * * @param file The file object containing the file data * @param filePath The path where the file should be saved * @param fileName The name to save the file as (optional) * @returns A tuple containing the public URL and the file path */ saveFile: (file: FileLike, filePath?: string, fileName?: string) => Promise<[string, string]>; /** * Return a boolean indicating if the file exists * * @param key * @returns */ exists(key: string): Promise; /** * Return contents of a object for the given key as a UTF-8 string. * Should throw "E_CANNOT_READ_FILE" error when the file * does not exists. * * @param key * @returns */ get(key: string): Promise; /** * Get the name of the disk currently in use. * * @returns */ getDiskName(): D; /** * Get the name of the driver currently in use. * * @returns */ getDriverName(): (KnownDisks & CustomDiskDriverRegistry)[D]["driver"]; /** * Get the driver currently in use. * * @returns */ getDriver(): DriveManager; /** * Return contents of a object for the given key as a Readable stream. * Should throw "E_CANNOT_READ_FILE" error when the file * does not exists. * * @param key * @returns */ getStream(key: string): Promise; /** * Return contents of an object for the given key as an Uint8Array. * Should throw "E_CANNOT_READ_FILE" error when the file * does not exists. * * @param key * @returns */ getBytes(key: string): Promise; /** * Return metadata of an object for the given key. * * @param key * @returns */ getMetaData(key: string): Promise; /** * Return the visibility of the file * * @param key * @returns */ getVisibility(key: string): Promise; /** * Return the public URL to access the file * * @param key * @returns */ getUrl(key: string): Promise; /** * Return the signed/temporary URL to access the file * * @param key * @param options * @returns */ getSignedUrl(key: string, options?: SignedURLOptions): Promise; /** * Return the signed/temporary URL that can be used to directly upload * the file contents to the storage. * * @param key * @param options * @returns */ getSignedUploadUrl(key: string, options?: SignedURLOptions): Promise; /** * Update the visibility of the file * * @param key * @param visibility * @returns */ setVisibility(key: string, visibility: ObjectVisibility): Promise; /** * Write object to the destination with the provided * contents. * * @param key * @param contents * @param options * @returns */ put(key: string, contents: string | Uint8Array | FileLike, options?: WriteOptions): Promise; /** * Write object to the destination with the provided * contents as a readable stream * * @param key * @param contents * @param options * @returns */ putStream(key: string, contents: Readable, options?: WriteOptions): Promise; /** * Copy the file from within the disk root location. Both * the "source" and "destination" will be the key names * and not absolute paths. * * @param source * @param destination * @param options * @returns */ copy(source: string, destination: string, options?: WriteOptions): Promise; /** * Move the file from within the disk root location. Both * the "source" and "destination" will be the key names * and not absolute paths. * * @param source * @param destination * @param options * @returns */ move(source: string, destination: string, options?: WriteOptions): Promise; /** * Delete the file for the given key. Should not throw * error when file does not exist in first place * * @param key * @returns */ delete(key: string): Promise; /** * Delete the files and directories matching the provided prefix. * * @param prefix * @returns */ deleteAll(prefix: string): Promise; /** * The list all method must return an array of objects with * the ability to paginate results (if supported). * * @param prefix * @param options * @returns */ listAll(prefix: string, options?: { recursive?: boolean; paginationToken?: string; }): Promise<{ paginationToken?: string; objects: Iterable; }>; /** * Switch bucket at runtime if supported. * * @param bucket * @returns */ bucket(bucket: string): DriverContract; /** * Create symbolic links for all configured links in the application configuration. * * @param param0 */ static link({ force }?: { force?: boolean; }): void; } //#endregion //#region src/FtpDriver.d.ts declare class FtpDriver implements DriverContract { private config; constructor(config: string | { host: string; username: string; password: string; port?: number; verbose?: boolean; privateKey?: string; }); getConfig(): { host: string; username: string; password: string; port?: number; verbose?: boolean; privateKey?: string; }; private init; private load; /** * Return a boolean value indicating if the file exists * or not. */ exists(key: string): Promise; /** * Return the file contents as a UTF-8 string. Throw an exception * if the file is missing. */ get(key: string): Promise; /** * Return the file contents as a Readable stream. Throw an exception * if the file is missing. */ getStream(key: string): Promise; /** * Return the file contents as a Uint8Array. Throw an exception * if the file is missing. */ getBytes(key: string): Promise; /** * Return metadata of the file. Throw an exception * if the file is missing. */ getMetaData(key: string): Promise; /** * Return visibility of the file. Infer visibility from the initial * config, when the driver does not support the concept of visibility. */ getVisibility(key: string): Promise; /** * Return the public URL of the file. Throw an exception when the driver * does not support generating URLs. */ getUrl(key: string): Promise; /** * Return the signed URL to serve a private file. Throw exception * when the driver does not support generating URLs. */ getSignedUrl(key: string, options?: SignedURLOptions): Promise; /** * Return the signed/temporary URL that can be used to directly upload * the file contents to the storage. */ getSignedUploadUrl(key: string, options?: SignedURLOptions): Promise; /** * Update the visibility of the file. Result in a NOOP * when the driver does not support the concept of * visibility. */ setVisibility(key: string, visibility: ObjectVisibility): Promise; /** * Create a new file or update an existing file. The contents * will be a UTF-8 string or "Uint8Array". */ put(key: string, contents: string | Uint8Array, options?: WriteOptions): Promise; /** * Create a new file or update an existing file. The contents * will be a Readable stream. */ putStream(key: string, contents: Readable, options?: WriteOptions): Promise; /** * Copy the existing file to the destination. Make sure the new file * has the same visibility as the existing file. It might require * manually fetching the visibility of the "source" file. */ copy(source: string, destination: string, options?: WriteOptions): Promise; /** * Move the existing file to the destination. Make sure the new file * has the same visibility as the existing file. It might require * manually fetching the visibility of the "source" file. */ move(source: string, destination: string, options?: WriteOptions): Promise; /** * Delete an existing file. Do not throw an error if the * file is already missing */ delete(key: string): Promise; /** * Delete all files inside a folder. Do not throw an error * if the folder does not exist or is empty. */ deleteAll(prefix: string): Promise; /** * Switch bucket at runtime if supported. */ bucket(config: string | { host: string; username: string; password: string; port?: number; verbose?: boolean; privateKey?: string; }): DriverContract; /** * List all files from a given folder or the root of the storage. * Do not throw an error if the request folder does not exist. */ listAll(prefix: string, options?: { recursive?: boolean; paginationToken?: string; }): Promise<{ paginationToken?: string; objects: Iterable; }>; } //#endregion export { CustomDiskConfig, CustomDiskDriverRegistry, DiskConfig, DriverConfig, FileLike, FilesystemConfig, FtpDriver, FtpDriverConfig, GcsDiskDriverConfig, KnownDisks, LocalDriverConfig, S3DriverConfig, Storage };