Add HEIF decode fallback for uploads
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
const heifExtensions = new Set([".heic", ".heif"]);
|
||||
const decoderCommands = ["heif-dec", "heif-convert"];
|
||||
|
||||
export const isHeifSource = (sourcePath: string) => heifExtensions.has(path.extname(sourcePath).toLowerCase());
|
||||
|
||||
export const decodeHeifToJpeg = async (sourcePath: string, prefix: string) => {
|
||||
const tempDir = await mkdtemp(path.join(os.tmpdir(), `${prefix}-`));
|
||||
const outputPath = path.join(tempDir, "decoded.jpg");
|
||||
|
||||
try {
|
||||
let lastError: unknown;
|
||||
for (const command of decoderCommands) {
|
||||
try {
|
||||
await execFileAsync(command, ["-q", "92", sourcePath, outputPath]);
|
||||
return {
|
||||
outputPath,
|
||||
cleanup: async () => rm(tempDir, { recursive: true, force: true })
|
||||
};
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError instanceof Error ? lastError : new Error("HEIF decoder command failed.");
|
||||
} catch (error) {
|
||||
await rm(tempDir, { recursive: true, force: true });
|
||||
const message = error instanceof Error ? error.message : "Unknown HEIF decode error.";
|
||||
throw new Error(`HEIF decoding failed. ${message}`);
|
||||
}
|
||||
};
|
||||
+57
-48
@@ -3,6 +3,7 @@ import path from "node:path";
|
||||
import sharp from "sharp";
|
||||
import type { ContributorConsent, PhotoAsset, Submission } from "@goodgrief/shared-types";
|
||||
import { config } from "./config.ts";
|
||||
import { decodeHeifToJpeg, isHeifSource } from "./heif.ts";
|
||||
|
||||
interface ImportedAssetRecord {
|
||||
asset: PhotoAsset;
|
||||
@@ -142,57 +143,65 @@ export const createLibraryAssets = async () => {
|
||||
const originalRelativePath = path.join("runtime", "library", `${baseId}-original.jpg`);
|
||||
const originalAbsolutePath = path.join(config.storageDir, originalRelativePath);
|
||||
|
||||
const sourceImage = sharp(file.absolutePath).rotate();
|
||||
const metadata = await sourceImage.metadata();
|
||||
const width = metadata.width ?? 1600;
|
||||
const height = metadata.height ?? 900;
|
||||
const decoded = isHeifSource(file.absolutePath)
|
||||
? await decodeHeifToJpeg(file.absolutePath, `goodgrief-library-${baseId}`)
|
||||
: null;
|
||||
|
||||
await sourceImage.clone().jpeg({ quality: 92 }).toFile(originalAbsolutePath);
|
||||
const dominantColor = await getDominantColor(sourceImage);
|
||||
const thumbRelativePath = path.join("runtime", "library", `${baseId}-thumb.jpg`);
|
||||
const previewRelativePath = path.join("runtime", "library", `${baseId}-preview.jpg`);
|
||||
const renderRelativePath = path.join("runtime", "library", `${baseId}-render.jpg`);
|
||||
try {
|
||||
const sourceImage = sharp(decoded?.outputPath ?? file.absolutePath).rotate();
|
||||
const metadata = await sourceImage.metadata();
|
||||
const width = metadata.width ?? 1600;
|
||||
const height = metadata.height ?? 900;
|
||||
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({ width: 320, height: 320, fit: "cover", position: "attention" })
|
||||
.jpeg({ quality: 84 })
|
||||
.toFile(path.join(config.storageDir, thumbRelativePath));
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({
|
||||
width: 1280,
|
||||
height: 1280,
|
||||
fit: "inside",
|
||||
withoutEnlargement: true
|
||||
})
|
||||
.jpeg({ quality: 86 })
|
||||
.toFile(path.join(config.storageDir, previewRelativePath));
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({
|
||||
width: 1920,
|
||||
height: 1920,
|
||||
fit: "inside",
|
||||
withoutEnlargement: true
|
||||
})
|
||||
.jpeg({ quality: 88 })
|
||||
.toFile(path.join(config.storageDir, renderRelativePath));
|
||||
await sourceImage.clone().jpeg({ quality: 92 }).toFile(originalAbsolutePath);
|
||||
const dominantColor = await getDominantColor(sourceImage);
|
||||
const thumbRelativePath = path.join("runtime", "library", `${baseId}-thumb.jpg`);
|
||||
const previewRelativePath = path.join("runtime", "library", `${baseId}-preview.jpg`);
|
||||
const renderRelativePath = path.join("runtime", "library", `${baseId}-render.jpg`);
|
||||
|
||||
imported.push(
|
||||
toImportedRecord({
|
||||
id: baseId,
|
||||
title: displayTitle,
|
||||
originalKey: `/uploads/${originalRelativePath}`,
|
||||
thumbKey: `/uploads/${thumbRelativePath}`,
|
||||
previewKey: `/uploads/${previewRelativePath}`,
|
||||
renderKey: `/uploads/${renderRelativePath}`,
|
||||
mimeType: "image/jpeg",
|
||||
width,
|
||||
height,
|
||||
dominantColor
|
||||
})
|
||||
);
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({ width: 320, height: 320, fit: "cover", position: "attention" })
|
||||
.jpeg({ quality: 84 })
|
||||
.toFile(path.join(config.storageDir, thumbRelativePath));
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({
|
||||
width: 1280,
|
||||
height: 1280,
|
||||
fit: "inside",
|
||||
withoutEnlargement: true
|
||||
})
|
||||
.jpeg({ quality: 86 })
|
||||
.toFile(path.join(config.storageDir, previewRelativePath));
|
||||
await sourceImage
|
||||
.clone()
|
||||
.resize({
|
||||
width: 1920,
|
||||
height: 1920,
|
||||
fit: "inside",
|
||||
withoutEnlargement: true
|
||||
})
|
||||
.jpeg({ quality: 88 })
|
||||
.toFile(path.join(config.storageDir, renderRelativePath));
|
||||
|
||||
imported.push(
|
||||
toImportedRecord({
|
||||
id: baseId,
|
||||
title: displayTitle,
|
||||
originalKey: `/uploads/${originalRelativePath}`,
|
||||
thumbKey: `/uploads/${thumbRelativePath}`,
|
||||
previewKey: `/uploads/${previewRelativePath}`,
|
||||
renderKey: `/uploads/${renderRelativePath}`,
|
||||
mimeType: "image/jpeg",
|
||||
width,
|
||||
height,
|
||||
dominantColor
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
await decoded?.cleanup();
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`[library-import] Skipping ${file.relativeName}:`, error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user