import { AsyncService, IInstanceCheck, IMappableService } from '@spinajs/di'; import { ReadStream, WriteStream } from 'fs'; import { DateTime } from 'luxon'; import { PassThrough } from 'stream'; import type { BinaryToTextEncoding } from 'node:crypto'; import type { FsTarget, FsTargetSpec } from './actions.js'; /** * Class for handling fs URI eg. fs://fs-temp/path/to/file */ export declare class URI { Fs: fs; Path: string; constructor(uri: string); } export interface IProviderConfiguration { name: string; service: string; /** * Name of another configured fs provider this one depends on ( eg. fsTemp backend ). * fsService creates referenced providers first, regardless of config order. */ provider?: string; } export interface IFsConfiguration { defaultProvider: string; providers: IProviderConfiguration[]; } export interface IStat { IsDirectory?: boolean; IsFile?: boolean; Size?: number; AccessTime?: DateTime; ModifiedTime?: DateTime; CreationTime?: DateTime; /** * Opaque, provider-supplied token that changes when the file content changes. * Consumers must treat it as meaningless except for equality comparison. * Providers that cannot supply one leave it undefined. */ Version?: string; AdditionalData?: unknown; } export interface IFileInfo { /** * Size in bytes */ FileSize: number; Height?: number; Width?: number; Duration?: number; FrameCount?: number; FrameRate?: number; /** * Number of audio channels ( eg. 2 for stereo ) */ AudioChannels?: number; Bitrate?: number; Codec?: string; LineCount?: number; Encoding?: string; MimeType?: string; WordCount?: number; AccessDate?: DateTime; ModificationDate?: DateTime; CreationDate?: DateTime; /** * Raw unprocessed data obtained from file info */ Raw?: {}; /** * All other extracted tags are promoted here generically ( PascalCased tag * name -> value ). The well-known fields above are typed aliases over the * same data; everything the analyzer emits is also available in {@link Raw}. */ [key: string]: unknown; } export interface IFsLocalOptions { /** * Full path to local directory where files are hold. * All paths will be relative to this directory * * If path is not provided, it will behave like normal nodejs fs functions */ basePath?: string; /** * Instance name of this filesystem. Used to share fs instances. */ name: string; } export interface ITempOptions { /** * Should cleanup of old temp files be enabled */ cleanup?: boolean; /** * Cleanup interval in milliseconds, used by interval-based cleanup strategies. * Default is 10 minutes */ cleanupInterval?: number; /** * Max temp file age in seconds. Older than this will be deleted. * Default is 1 hour */ maxFileAge?: number; /** * Class name of TempCleanupStrategy implementation to use. * Default is MaxAgeTempCleanupStrategy */ cleanupStrategy?: string; /** * Cron expression used by CronTempCleanupStrategy ( 6-field with seconds supported, * eg. '0 0 * * * *' = hourly ). Required when that strategy is selected. * Ignored by interval-based strategies. */ cleanupCronExpression?: string; } export interface IFsTempOptions extends ITempOptions { /** * Instance name of this filesystem. Used to share fs instances. */ name: string; /** * Name of registered fs provider used as temp file storage. * Any configured fs can serve as backend ( local, s3, ftp, ... ). * Creation fails if provider with this name is not registered. */ provider: string; } export interface IZipResult { /** * Destination filesystem ( default is fs-temp for zipped files) */ fs: fs; asFilePath(): string; asStream(encoding?: BufferEncoding): ReadStream; asBase64(): string; unlink(): Promise; } export interface IFsUploadOptions { /** * Fail with `ResourceDuplicated` instead of overwriting when `destPath` already exists. * Providers do it atomically where the backend allows ( see each provider's docs ). */ exclusive?: boolean; } export interface IWriteStreamOptions { encoding?: BufferEncoding; /** Fail with ResourceDuplicated when the file exists; never overwrite. */ exclusive?: boolean; /** Providers that store a content type use it; fsNative ignores it. */ contentType?: string; /** Providers that store object metadata use it; fsNative ignores it. */ metadata?: Record; } export declare abstract class fs extends AsyncService implements IMappableService, IInstanceCheck { constructor(); get ServiceName(): string; __checkInstance__(creationOptions: any): boolean; abstract Name: string; /** Root that absolute paths are made relative to before any action; undefined when the provider has none. */ get BasePath(): string | undefined; /** * Downloads file to local storage and returns path to it. * On local providers it only returns the full path; remote providers download to local disk first. */ download(path: string): Promise; /** * Copies local file into fs * * @param srcPath source path ( full absolute path eg. file from local disk; not checked ) * @param destPath dest path ( relative to base path of provider ), default the source file name * @param options.exclusive throw ResourceDuplicated instead of overwriting an existing destination */ upload(srcPath: string, destPath?: string, options?: IFsUploadOptions): Promise; /** * Returns hash of file * * @param algo optional hash alghoritm, default is `fs.hasher.defaultAlgorithm`, then sha256 */ hash(path: string, algo?: string): Promise; metadata(path: string): Promise; /** * Returns full LOCAL path to file */ abstract resolvePath(path: string): string; read(path: string, encoding?: BufferEncoding): Promise; readStream(path: string, encoding?: BufferEncoding): Promise; write(path: string, data: string | Uint8Array, encoding?: BufferEncoding): Promise; writeStream(path: string, options?: IWriteStreamOptions | BufferEncoding): Promise; writeStream(path: string, readStream: NodeJS.ReadableStream, options?: IWriteStreamOptions | BufferEncoding): Promise; append(path: string, data: string | Uint8Array, encoding?: BufferEncoding): Promise; exists(path: string): Promise; dirExists(path: string): Promise; stat(path: string): Promise; isDir(path: string): Promise; list(path: string): Promise; /** Whether anything lives under `path` as a directory or, on a key store, as a prefix (nested keys included). */ hasEntries(path: string): Promise; mkdir(path: string): Promise; rm(path: string): Promise; copy(path: string, dest: string, dstFs?: fs): Promise; move(oldPath: string, newPath: string, dstFs?: fs): Promise; rename(oldPath: string, newPath: string): Promise; /** * Compress specified file or dir in provided path. Dir is compressed recursively. * An entry `[path, entryName]` names the entry in the archive. */ zip(path: string | (string | string[])[], dstFs?: fs, dstFile?: string): Promise; /** * Decompress given file to destination dir, default a temp name on the destination fs. * * @returns path to unzipped file */ unzip(srcPath: string, destPath?: string, dstFs?: fs): Promise; /** * Returns a time-limited public URL to the file. * * @param opts.until when the URL expires ( provider default when absent ) * @param opts.disposition content disposition the URL should serve the file with * @throws MethodNotImplemented when the provider has no public URLs */ signedUrl(path: string, opts?: { until?: DateTime; disposition?: string; }): Promise; getSignedUrl(path: string, until?: DateTime): Promise; abstract tmppath(ext?: string): string; tmpname(ext?: string): string; /** Where zip writes when the caller names no destination fs. */ protected zipDestination(): fs; /** Where unzip writes when the caller names no destination fs. */ protected unzipDestination(): fs; /** Every file action goes through here; provider methods that touch files outside the public API use it too. */ protected runAction(action: string, targets: readonly FsTargetSpec[], args: unknown[], impl: (targets: readonly FsTarget[], args: unknown[]) => Promise, opts?: { stream?: boolean; }): Promise; protected doHash(path: string, algo?: string): Promise; protected doMetadata(path: string): Promise; protected abstract doDownload(path: string): Promise; /** `options.exclusive`: throw ResourceDuplicated when `destPath` exists, atomically where the backend allows. */ protected abstract doUpload(srcPath: string, destPath: string, options?: IFsUploadOptions): Promise; protected abstract doRead(path: string, encoding?: BufferEncoding): Promise; protected abstract doReadStream(path: string, encoding?: BufferEncoding): Promise; protected abstract doWrite(path: string, data: string | Uint8Array, encoding?: BufferEncoding): Promise; /** Gets the writeStream arguments normalised: no source stream is `undefined`, and `options` is always an object. */ protected abstract doWriteStream(path: string, readStream: NodeJS.ReadableStream | undefined, options: IWriteStreamOptions): Promise; protected abstract doAppend(path: string, data: string | Uint8Array, encoding?: BufferEncoding): Promise; protected abstract doExists(path: string): Promise; protected abstract doDirExists(path: string): Promise; protected abstract doStat(path: string): Promise; protected abstract doIsDir(path: string): Promise; protected abstract doList(path: string): Promise; /** Key stores whose list stops at the first delimiter override this: keys nested deeper count too. */ protected doHasEntries(path: string): Promise; protected abstract doMkdir(path: string): Promise; protected abstract doRm(path: string): Promise; /** `dstFs` as the caller passed it; undefined copies within this fs. */ protected abstract doCopy(path: string, dest: string, dstFs?: fs): Promise; protected abstract doMove(oldPath: string, newPath: string, dstFs?: fs): Promise; protected abstract doRename(oldPath: string, newPath: string): Promise; /** `dstFs` and `dstFile` are resolved by the base class: the destination that was checked. */ protected abstract doZip(path: string | (string | string[])[], dstFs: fs, dstFile: string): Promise; protected abstract doUnzip(srcPath: string, destPath: string, dstFs: fs): Promise; protected doSignedUrl?(path: string, opts: { until?: DateTime; disposition?: string; }): Promise; /** * ------------------------------------------------------------------------- * * STATIC METHODS * * ------------------------------------------------------------------------- */ /** * Downloads file to local storage and returns path to it. * If used on local storage provider eg. hard drive it only returns full path to file * * On remote storage provivers eg. amazon s3 - it tries to download it to local disk first and returns * full path. * * Returns local path to file * * @param path path to download */ static download(path: URI): Promise; /** * Copies local file into fs * * @param srcPath source path ( full absolute path eg. file from local disk ) * @param destPath dest path ( relative to base path of provider ) */ static upload(srcPath: string, destPath?: URI): Promise; /** * * Returns hash of file * * @param srcPath file to calculate hash * @param algo optional hash alghoritm, default is `fs.hasher.defaultAlgorithm`, then sha256 */ static hash(path: URI, algo?: string): Promise; /** * * Returns full LOCAL path to file * * @param path path to resolve */ static resolvePath(path: URI): string; static read(path: URI, encoding?: BufferEncoding): Promise; static readStream(path: URI, encoding?: BufferEncoding): Promise; static write(path: URI, data: string | Uint8Array, encoding?: BufferEncoding): Promise; static writeStream(path: URI, options?: IWriteStreamOptions | BufferEncoding): Promise; static writeStream(path: URI, readStream: NodeJS.ReadableStream, options?: IWriteStreamOptions | BufferEncoding): Promise; static exists(path: URI): Promise; static dirExists(path: URI): Promise; static copy(src: URI, dest: URI): Promise; static move(src: URI, dest: URI): Promise; static rename(src: URI, dest: URI | string): Promise; static rm(path: URI): Promise; static mkdir(path: URI): Promise; static stat(path: URI): Promise; static list(path: URI): Promise; static tmppath(fs: string, ext?: string): string; static append(path: URI, data: string | Uint8Array, encoding?: BufferEncoding): Promise; /** * * Compress specified file or dir in provided path. * Dir is compressed recursively * * @param path - path to zip * @param dstFile - destination file name */ static zip(path: URI | URI[], dstFile: URI): Promise; /** * Decompress given file to destination path * * @param path path to zip file * @param destPath path to destination dir * * @returns path to unzipped file */ static unzip(srcPath: URI, destPath: URI): Promise; /** * * Checks if given path is dir * * @param path path to check */ static isDir(path: URI): Promise; } /** * File information service, obtain file props, metadata etc. * Eg. movie resolution, image, codec etc. if possible */ export declare abstract class FileInfoService { /** * Extracts file information from a file on disk. * * @param pathToFile absolute path to the file */ abstract getInfo(pathToFile: string): Promise; /** * Extracts file information from a readable stream ( content is fed to the * analyzer via stdin, so no temporary file is needed ). * * @param stream readable stream with the file content */ abstract getInfoFromStream(stream: NodeJS.ReadableStream): Promise; } /** * File hasher, to create unique hash for a file, raw data or a stream. */ export declare abstract class FileHasher { /** * Hashes the content of a file. * * @param pathToFile absolute path to the file * @param encoding digest output encoding ( default 'hex' ) */ abstract hash(pathToFile: string, encoding?: BinaryToTextEncoding): Promise; /** * Hashes raw in-memory data. * * @param data string or Buffer to hash * @param encoding digest output encoding ( default 'hex' ) */ abstract hashData(data: string | Uint8Array, encoding?: BinaryToTextEncoding): Promise; /** * Hashes the content of a readable stream. * * @param stream readable stream to consume * @param encoding digest output encoding ( default 'hex' ) */ abstract hashStream(stream: NodeJS.ReadableStream, encoding?: BinaryToTextEncoding): Promise; } //# sourceMappingURL=interfaces.d.ts.map