/** * @packageDocumentation * @module MediaManager * @description Handles media conversion between formats */ import sharp from "sharp"; export const supportedMediaFormats = [ "image/png", "image/jpeg", "image/webp", "image/avif", "image/svg+xml", "image/gif", "image/tiff", ]; export const supportedOutputFormats = [ "image/jpeg", "image/png", "image/webp", "image/avif", "image/gif", "image/tiff", ]; /** * Handles media conversion between formats */ export class MediaConverter { /** * Returns whether the media is convertable * @returns Whether the media is convertable */ public isConvertable(file: File) { return supportedMediaFormats.includes(file.type); } /** * Returns the file name with the extension replaced * @param fileName File name to replace * @returns File name with extension replaced */ private getReplacedFileName(fileName: string, newExtension: string) { return this.extractFilenameFromPath(fileName).replace( /\.[^/.]+$/, `.${newExtension}`, ); } /** * 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(/(?