import path from "node:path" import fs from "node:fs/promises" import sharp from "sharp" const IMAGE_EXTENSIONS = new Set([ ".jpg", ".jpeg", ".png", ".heic", ".heif", ".gif", ".tiff", ".bmp", ".svg", ".avif", ".raw", ".dng" ]) const VIDEO_EXTENSIONS = new Set([ ".mp4", ".mov", ".avi", ".mkv", ".webm", ".flv", ".wmv", ".m4v", ".3gp", ".ts" ]) function getMediaType(filePath) { if (!filePath) return "unknown" const ext = path.extname(filePath).toLowerCase() if (IMAGE_EXTENSIONS.has(ext)) return "image" if (VIDEO_EXTENSIONS.has(ext)) return "video" return "unknown" } function changeExtension(filePath, newExt) { const formattedExt = newExt.startsWith(".") ? newExt : `.${newExt}` const parsed = path.parse(filePath) parsed.base = `${parsed.name}${formattedExt}` parsed.ext = formattedExt return path.format(parsed) } async function processCompression($file) { const file = $file.replace("/data", process.env.IMMICH_DATA_PATH) const newFilePath = changeExtension(file, process.env.IMG_COMPRESSION_OUTPUT_FORMAT) const compressedFile = await sharp(file) .toFormat(process.env.IMG_COMPRESSION_OUTPUT_FORMAT, { quality: parseInt(process.env.IMG_COMPRESSION_OUTPUT_QUALITY) || 80 } ) .toFile(newFilePath) } export default { getMediaType, changeExtension, processCompression }