UNPKG

thumbkit

Version:

A comprehensive TypeScript library for generating thumbnails from images, PDFs, videos, office documents, and archives.

138 lines 6.42 kB
import { BatchResult, FileInput, FileType, SupportedTypes, ThumbnailConfig, ThumbnailResult } from "./types"; export declare class ThumbnailGenerator { private config; private readonly supportedTypes; constructor(options?: ThumbnailConfig); /** * Generates a standardized thumbnail name based on the original file name. * * @param originalFileName - The original file name from which to generate the thumbnail name. * @returns The generated thumbnail file name with the configured suffix and format. */ private generateThumbnailName; /** * Creates a ThumbnailResult object from a generated thumbnail buffer. * * @param thumbnailBuffer Thumbnail buffer * @param originalFileName Original filename * @param fileType File type (image, document, video, or office) * @param metadata Additional metadata * @returns ThumbnailResult object */ private createThumbnailObject; /** * Determines the file type category based on the file extension. * * @param fileName The name of the file with its extension. * @returns The file type category as 'image', 'document', 'video', 'office', 'archive', or 'unsupported'. */ getFileType(fileName: string): FileType; /** * Generates a thumbnail for an image file using sharp. * @param fileBuffer The image file buffer. * @param fileName The original file name. * @returns A promise that resolves with a ThumbnailResult object. * @throws An Error if the thumbnail generation fails. */ private generateImageThumbnail; /** * Generates a thumbnail for a PDF file using PDF.js. * @param fileBuffer The PDF file buffer. * @param fileName The original file name. * @returns A promise that resolves with a ThumbnailResult object containing the thumbnail image buffer and additional PDF metadata. * @throws An Error if the thumbnail generation fails. */ private generatePDFThumbnail; /** * Generate thumbnail for video files * @param fileBuffer The video file buffer * @param fileName The original file name * @returns A promise that resolves with a ThumbnailResult object * @throws An Error if the thumbnail generation fails * @note Video thumbnails currently generate a placeholder thumbnail * requiring ffmpeg integration for frame extraction */ private generateVideoThumbnail; /** * Generates a thumbnail for office documents. * * This method creates a placeholder thumbnail for office documents based on * the file extension. It generates an SVG icon specific to the document type * (e.g., DOC, XLS, PPT) and converts it to a thumbnail image. * * @param fileBuffer - The buffer of the office document file. * @param fileName - The original file name of the office document. * @returns A promise that resolves with a ThumbnailResult object containing * the generated thumbnail image buffer and additional metadata. * @throws An Error if the thumbnail generation fails. */ private generateOfficeThumbnail; /** * Generates a thumbnail for archive files. * * This method creates a placeholder thumbnail for archive files using an SVG representation * that visually indicates the file is an archive. The SVG is converted to a thumbnail image * using the specified configuration settings. * * @param fileBuffer - The buffer of the archive file. * @param fileName - The original file name of the archive. * @returns A promise that resolves with a ThumbnailResult object containing * the generated thumbnail image buffer and additional metadata. * @throws An Error if the thumbnail generation fails. */ private generateArchiveThumbnail; /** * Generates a thumbnail for a given file or buffer. * * This method takes various types of input (Buffer, file path string, or FileInput object) * and generates a thumbnail image based on the file type. It supports images, PDF documents, * videos, office documents, and archives. The generated thumbnail is returned as a ThumbnailResult * object containing the image buffer, file name, and additional metadata. * * @param input - The file or buffer to generate a thumbnail for. * @param fileName? - The original file name, required when input is a Buffer. * @param options? - Optional configuration overrides for thumbnail generation. * @returns A promise that resolves with a ThumbnailResult object containing the generated thumbnail * image buffer and additional metadata. * @throws An Error if the thumbnail generation fails. */ generate(input: Buffer | string | FileInput, fileName?: string, options?: Partial<ThumbnailConfig>): Promise<ThumbnailResult>; /** * Generates thumbnails for multiple files in a single call. * @param files An array of file paths or FileInput objects * @param options Optional configuration object to override the default config * @returns A promise that resolves with an object containing two arrays: results and errors. * results contains ThumbnailResult objects for the successfully generated thumbnails. * errors contains objects with index, fileName, and error properties for each failed thumbnail generation. */ generateBatch(files: Array<string | FileInput>, options?: Partial<ThumbnailConfig>): Promise<BatchResult>; /** * Checks if a given file type is supported by the generator. * * @param fileName - The name of the file, including its extension. * @returns A boolean indicating whether the file type is supported. */ isSupported(fileName: string): boolean; /** * Get list of all supported file extensions */ getSupportedTypes(): SupportedTypes; /** * Get list of supported extensions as flat array */ getSupportedExtensions(): string[]; /** * Update the configuration */ updateConfig(newConfig: Partial<ThumbnailConfig>): void; /** * Get current configuration */ getConfig(): Required<ThumbnailConfig>; /** * Reset configuration to defaults */ resetConfig(): void; } export default ThumbnailGenerator; //# sourceMappingURL=index.d.ts.map