/** * @packageDocumentation * @module MediaManager * @description Handles media conversion between formats */ import sharp from "sharp"; export enum ConvertableMediaFormats { PNG = "png", WEBP = "webp", JPEG = "jpeg", JPG = "jpg", AVIF = "avif", JXL = "jxl", HEIF = "heif", } /** * Handles media conversion between formats */ export class MediaConverter { constructor( public fromFormat: ConvertableMediaFormats, public toFormat: ConvertableMediaFormats ) {} /** * Returns whether the media is convertable * @returns Whether the media is convertable */ public isConvertable() { return ( this.fromFormat !== this.toFormat && Object.values(ConvertableMediaFormats).includes(this.fromFormat) ); } /** * Returns the file name with the extension replaced * @param fileName File name to replace * @returns File name with extension replaced */ private getReplacedFileName(fileName: string) { return this.extractFilenameFromPath(fileName).replace( new RegExp(`\\.${this.fromFormat}$`), `.${this.toFormat}` ); } /** * Extracts the filename from a path * @param path Path to extract filename from * @returns Extracted filename */ private extractFilenameFromPath(path: string) { // Don't count escaped slashes as path separators const pathParts = path.split(/(?