Add HEIF decode fallback for uploads
This commit is contained in:
parent
d90f8bd645
commit
d51eef4d42
@ -23,6 +23,7 @@ RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
libheif1 \
|
||||
libheif-plugin-libde265 \
|
||||
libheif-examples \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
FROM workspace-source AS admin-build
|
||||
|
||||
38
services/api/src/heif.ts
Normal file
38
services/api/src/heif.ts
Normal file
@ -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}`);
|
||||
}
|
||||
};
|
||||
@ -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,7 +143,12 @@ 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 decoded = isHeifSource(file.absolutePath)
|
||||
? await decodeHeifToJpeg(file.absolutePath, `goodgrief-library-${baseId}`)
|
||||
: null;
|
||||
|
||||
try {
|
||||
const sourceImage = sharp(decoded?.outputPath ?? file.absolutePath).rotate();
|
||||
const metadata = await sourceImage.metadata();
|
||||
const width = metadata.width ?? 1600;
|
||||
const height = metadata.height ?? 900;
|
||||
@ -193,6 +199,9 @@ export const createLibraryAssets = async () => {
|
||||
dominantColor
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
await decoded?.cleanup();
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`[library-import] Skipping ${file.relativeName}:`, error);
|
||||
}
|
||||
|
||||
40
services/worker/src/heif.ts
Normal file
40
services/worker/src/heif.ts
Normal 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}`);
|
||||
}
|
||||
};
|
||||
@ -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,7 +58,12 @@ 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 decoded = isHeifSource(sourcePath, asset.mimeType)
|
||||
? await decodeHeifToJpeg(sourcePath, `goodgrief-worker-${asset.id}`)
|
||||
: null;
|
||||
|
||||
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;
|
||||
@ -92,6 +98,9 @@ export const processAsset = async (asset: PhotoAsset) => {
|
||||
lowContrast: stats.channels[0]?.stdev ? stats.channels[0].stdev < 12 : false
|
||||
}
|
||||
};
|
||||
} finally {
|
||||
await decoded?.cleanup();
|
||||
}
|
||||
};
|
||||
|
||||
export const runWorkerOnce = async () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user