Add HEIF decode fallback for uploads

This commit is contained in:
2026-04-09 23:30:06 -07:00
parent d90f8bd645
commit d51eef4d42
5 changed files with 177 additions and 80 deletions
+40
View File
@@ -0,0 +1,40 @@
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 heifMimeTypes = new Set(["image/heic", "image/heif", "image/heic-sequence", "image/heif-sequence"]);
const decoderCommands = ["heif-dec", "heif-convert"];
export const isHeifSource = (sourcePath: string, mimeType?: string) =>
heifExtensions.has(path.extname(sourcePath).toLowerCase()) || (mimeType ? heifMimeTypes.has(mimeType) : false);
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}`);
}
};
+41 -32
View File
@@ -4,6 +4,7 @@ import path from "node:path";
import sharp from "sharp";
import type { PhotoAsset, RepositoryState } from "@goodgrief/shared-types";
import { config } from "./config.ts";
import { decodeHeifToJpeg, isHeifSource } from "./heif.ts";
const toStoragePath = (publicUrl: string) => path.join(config.storageDir, publicUrl.replace(/^\/uploads\//, ""));
@@ -57,41 +58,49 @@ export const processAsset = async (asset: PhotoAsset) => {
await mkdir(path.join(config.storageDir, "runtime", "previews"), { recursive: true });
await mkdir(path.join(config.storageDir, "runtime", "renders"), { recursive: true });
const image = sharp(inputBuffer, { failOn: "none" }).rotate();
const metadata = await image.metadata();
const stats = await image.stats();
const width = metadata.width ?? 0;
const height = metadata.height ?? 0;
const decoded = isHeifSource(sourcePath, asset.mimeType)
? await decodeHeifToJpeg(sourcePath, `goodgrief-worker-${asset.id}`)
: null;
await image.clone().resize({ width: 320, height: 320, fit: "inside" }).jpeg({ quality: 78 }).toFile(
createDerivativePath(asset.id, "thumbs")
);
await image.clone().resize({ width: 960, height: 960, fit: "inside" }).jpeg({ quality: 84 }).toFile(
createDerivativePath(asset.id, "previews")
);
await image.clone().resize({ width: 1920, height: 1920, fit: "inside" }).jpeg({ quality: 88 }).toFile(
createDerivativePath(asset.id, "renders")
);
try {
const image = sharp(decoded?.outputPath ?? sourcePath, { failOn: "none" }).rotate();
const metadata = await image.metadata();
const stats = await image.stats();
const width = metadata.width ?? 0;
const height = metadata.height ?? 0;
const dominant = stats.dominant;
const dominantColor = `#${[dominant.r, dominant.g, dominant.b]
.map((value) => value.toString(16).padStart(2, "0"))
.join("")}`;
await image.clone().resize({ width: 320, height: 320, fit: "inside" }).jpeg({ quality: 78 }).toFile(
createDerivativePath(asset.id, "thumbs")
);
await image.clone().resize({ width: 960, height: 960, fit: "inside" }).jpeg({ quality: 84 }).toFile(
createDerivativePath(asset.id, "previews")
);
await image.clone().resize({ width: 1920, height: 1920, fit: "inside" }).jpeg({ quality: 88 }).toFile(
createDerivativePath(asset.id, "renders")
);
return {
thumbKey: publicKeyFor(asset.id, "thumbs"),
previewKey: publicKeyFor(asset.id, "previews"),
renderKey: publicKeyFor(asset.id, "renders"),
width,
height,
orientation: computeOrientation(width, height),
sha256,
dominantColor,
qualityFlags: {
tooSmall: width < 800 || height < 800,
lowContrast: stats.channels[0]?.stdev ? stats.channels[0].stdev < 12 : false
}
};
const dominant = stats.dominant;
const dominantColor = `#${[dominant.r, dominant.g, dominant.b]
.map((value) => value.toString(16).padStart(2, "0"))
.join("")}`;
return {
thumbKey: publicKeyFor(asset.id, "thumbs"),
previewKey: publicKeyFor(asset.id, "previews"),
renderKey: publicKeyFor(asset.id, "renders"),
width,
height,
orientation: computeOrientation(width, height),
sha256,
dominantColor,
qualityFlags: {
tooSmall: width < 800 || height < 800,
lowContrast: stats.channels[0]?.stdev ? stats.channels[0].stdev < 12 : false
}
};
} finally {
await decoded?.cleanup();
}
};
export const runWorkerOnce = async () => {