feat: compression 15
This commit is contained in:
1 parent
a182777ec4
commit
4858392c1d
7 files changed
+236
-53
No files matched your search
@@ -168,6 +168,10 @@ export interface ICompressedVideo extends ICompressionResult {
|
||||
originalFileName: string;
|
||||
localDateTime: string;
|
||||
compressedAt: string;
|
||||
status: "compressed" | "replacing" | "replaced";
|
||||
replacementAssetId?: string;
|
||||
replacementStartedAt?: string;
|
||||
replacedAt?: string;
|
||||
}
|
||||
|
||||
export const listCompressedVideos = async (): Promise<ICompressedVideo[]> => {
|
||||
|
||||
@@ -2,16 +2,39 @@ import { appDb } from "@/db";
|
||||
import { settings } from "@/db/schema/settings.schema";
|
||||
import { DEFAULT_FFMPEG_COMMAND, FFMPEG_COMMAND_SETTING } from "@/config/compression";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const COMPRESSION_ROOT = "/app/data/compression";
|
||||
const ORIGINALS_ROOT = "/originals";
|
||||
const COMPRESSION_JOB_FILE = "job.json";
|
||||
|
||||
export type CompressionJobStatus = "compressed" | "replacing" | "replaced";
|
||||
|
||||
export interface CompressionJob {
|
||||
version: 1;
|
||||
ownerId: string;
|
||||
sourceAssetId: string;
|
||||
originalFileName: string;
|
||||
originalPath: string;
|
||||
localDateTime: string;
|
||||
originalSize: number;
|
||||
outputFileName: string;
|
||||
compressedSize: number;
|
||||
compressedAt: string;
|
||||
status: CompressionJobStatus;
|
||||
replacementAssetId?: string;
|
||||
replacementStartedAt?: string;
|
||||
replacedAt?: string;
|
||||
}
|
||||
|
||||
export const getCompressionDirectory = (assetId: string) =>
|
||||
path.join(COMPRESSION_ROOT, assetId);
|
||||
|
||||
export const getCompressionJobPath = (assetId: string) =>
|
||||
path.join(getCompressionDirectory(assetId), COMPRESSION_JOB_FILE);
|
||||
|
||||
const getSafeOriginalFileName = (assetId: string, originalFileName: string) => {
|
||||
const fileName = path.basename(originalFileName || "");
|
||||
return fileName || `${assetId}.mp4`;
|
||||
@@ -56,6 +79,26 @@ export const clearCompressionDirectory = async (assetId: string) => {
|
||||
await rm(getCompressionDirectory(assetId), { recursive: true, force: true });
|
||||
};
|
||||
|
||||
export const writeCompressionJob = async (job: CompressionJob) => {
|
||||
await ensureCompressionDirectory(job.sourceAssetId);
|
||||
const jobPath = getCompressionJobPath(job.sourceAssetId);
|
||||
const tempPath = `${jobPath}.tmp`;
|
||||
await writeFile(tempPath, JSON.stringify(job, null, 2), "utf8");
|
||||
await rename(tempPath, jobPath);
|
||||
};
|
||||
|
||||
export const readCompressionJob = async (assetId: string): Promise<CompressionJob | null> => {
|
||||
try {
|
||||
const raw = await readFile(getCompressionJobPath(assetId), "utf8");
|
||||
const job = JSON.parse(raw) as CompressionJob;
|
||||
if (job.version !== 1 || job.sourceAssetId !== assetId) return null;
|
||||
return job;
|
||||
} catch (error: any) {
|
||||
if (error?.code === "ENOENT" || error instanceof SyntaxError) return null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getFfmpegCommand = async (ownerId: string) => {
|
||||
const [row] = await appDb
|
||||
.select({ value: settings.value })
|
||||
|
||||
@@ -2,7 +2,12 @@ import { ENV } from "@/config/environment";
|
||||
import { db } from "@/config/db";
|
||||
import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
|
||||
import { getUserHeaders } from "@/helpers/user.helper";
|
||||
import { getCompressionOutputPath } from "@/helpers/compression.server";
|
||||
import {
|
||||
getCompressionDirectory,
|
||||
getCompressionOutputPath,
|
||||
readCompressionJob,
|
||||
writeCompressionJob,
|
||||
} from "@/helpers/compression.server";
|
||||
import { assets } from "@/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { createReadStream } from "node:fs";
|
||||
@@ -40,10 +45,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
if (!currentUser) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const rawAssetId = req.body?.assetId as string;
|
||||
const job = await readCompressionJob(rawAssetId);
|
||||
if (job && job.ownerId !== currentUser.id) {
|
||||
return res.status(404).json({ message: "Video not found" });
|
||||
}
|
||||
if (job?.status === "replaced") {
|
||||
return res.status(409).json({ message: "This compressed file has already replaced its source asset" });
|
||||
}
|
||||
if (job?.status === "replacing") {
|
||||
return res.status(409).json({
|
||||
message: "A previous replacement did not finish cleanly. Check Immich before retrying.",
|
||||
replacementAssetId: job.replacementAssetId,
|
||||
});
|
||||
}
|
||||
|
||||
const sourceAssetId = job?.sourceAssetId ?? rawAssetId;
|
||||
const [dbAsset] = await db
|
||||
.select({ id: assets.id, type: assets.type, originalFileName: assets.originalFileName })
|
||||
.from(assets)
|
||||
.where(and(eq(assets.id, rawAssetId), eq(assets.ownerId, currentUser.id)));
|
||||
.where(and(eq(assets.id, sourceAssetId), eq(assets.ownerId, currentUser.id)));
|
||||
|
||||
if (!dbAsset || dbAsset.type !== "VIDEO") {
|
||||
return res.status(404).json({ message: "Video not found" });
|
||||
@@ -83,7 +103,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const customMetadata = metadataResponse.ok ? await metadataResponse.json() : [];
|
||||
|
||||
const replacementFileName = path.basename(source.originalFileName || dbAsset.originalFileName || `${dbAsset.id}.mp4`);
|
||||
const outputPath = getCompressionOutputPath(dbAsset.id, dbAsset.originalFileName);
|
||||
const outputPath = job
|
||||
? path.join(getCompressionDirectory(job.sourceAssetId), job.outputFileName)
|
||||
: getCompressionOutputPath(dbAsset.id, dbAsset.originalFileName);
|
||||
const outputStats = await stat(outputPath);
|
||||
const uploadFile = new UploadFile(outputPath, outputStats.size, replacementFileName);
|
||||
|
||||
@@ -124,6 +146,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
newAssetId = uploaded.id;
|
||||
createdNewAsset = true;
|
||||
|
||||
if (job) {
|
||||
await writeCompressionJob({
|
||||
...job,
|
||||
status: "replacing",
|
||||
replacementAssetId: newAssetId,
|
||||
replacementStartedAt: new Date().toISOString(),
|
||||
replacedAt: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(customMetadata) && customMetadata.length > 0) {
|
||||
const metadataItems = customMetadata
|
||||
.filter((item: any) => item?.key && item?.value && typeof item.value === "object")
|
||||
@@ -225,6 +257,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
}
|
||||
if (!deleteResponse.ok) throw new Error(`Failed to move original asset to Trash: ${await readError(deleteResponse)}`);
|
||||
|
||||
// From this point on the original may already be in Trash, so never roll back by deleting the replacement.
|
||||
rollbackSafe = false;
|
||||
if (job) {
|
||||
await writeCompressionJob({
|
||||
...job,
|
||||
status: "replaced",
|
||||
replacementAssetId: newAssetId,
|
||||
replacementStartedAt: job.replacementStartedAt ?? new Date().toISOString(),
|
||||
replacedAt: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
oldAssetId: dbAsset.id,
|
||||
newAssetId,
|
||||
@@ -261,6 +305,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
headers: jsonHeaders,
|
||||
body: JSON.stringify({ ids: [newAssetId], force: true }),
|
||||
}).catch(() => undefined);
|
||||
|
||||
if (job) {
|
||||
await writeCompressionJob({
|
||||
...job,
|
||||
status: "compressed",
|
||||
replacementAssetId: undefined,
|
||||
replacementStartedAt: undefined,
|
||||
replacedAt: undefined,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
return res.status(500).json({ message: error?.message ?? "Failed to replace asset" });
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { db } from "@/config/db";
|
||||
import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
|
||||
import { clearCompressionDirectory, getCompressionOutputPath, getVideoContentType } from "@/helpers/compression.server";
|
||||
import {
|
||||
clearCompressionDirectory,
|
||||
getCompressionDirectory,
|
||||
getCompressionOutputPath,
|
||||
getVideoContentType,
|
||||
readCompressionJob,
|
||||
} from "@/helpers/compression.server";
|
||||
import { assets } from "@/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -12,22 +19,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
if (!currentUser) return res.status(401).json({ message: "Unauthorized" });
|
||||
|
||||
const rawAssetId = req.query.assetId as string;
|
||||
const [asset] = await db
|
||||
.select({ id: assets.id, originalFileName: assets.originalFileName })
|
||||
.from(assets)
|
||||
.where(and(eq(assets.id, rawAssetId), eq(assets.ownerId, currentUser.id)));
|
||||
const job = await readCompressionJob(rawAssetId);
|
||||
|
||||
if (!asset) return res.status(404).json({ message: "Asset not found" });
|
||||
let filePath: string;
|
||||
if (job) {
|
||||
if (job.ownerId !== currentUser.id) return res.status(404).json({ message: "Asset not found" });
|
||||
filePath = path.join(getCompressionDirectory(job.sourceAssetId), job.outputFileName);
|
||||
} else {
|
||||
const [asset] = await db
|
||||
.select({ id: assets.id, originalFileName: assets.originalFileName })
|
||||
.from(assets)
|
||||
.where(and(eq(assets.id, rawAssetId), eq(assets.ownerId, currentUser.id)));
|
||||
|
||||
if (!asset) return res.status(404).json({ message: "Asset not found" });
|
||||
filePath = getCompressionOutputPath(asset.id, asset.originalFileName);
|
||||
}
|
||||
|
||||
if (req.method === "DELETE") {
|
||||
await clearCompressionDirectory(asset.id);
|
||||
await clearCompressionDirectory(rawAssetId);
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
if (req.method !== "GET") return res.status(405).json({ message: "Method not allowed" });
|
||||
|
||||
const filePath = getCompressionOutputPath(asset.id, asset.originalFileName);
|
||||
|
||||
try {
|
||||
const fileStat = await stat(filePath);
|
||||
const range = req.headers.range;
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { db } from "@/config/db";
|
||||
import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
|
||||
import { getCompressionOutputFileName, getCompressionOutputPath } from "@/helpers/compression.server";
|
||||
import {
|
||||
getCompressionOutputFileName,
|
||||
getCompressionOutputPath,
|
||||
getCompressionDirectory,
|
||||
readCompressionJob,
|
||||
} from "@/helpers/compression.server";
|
||||
import { assets, exif } from "@/schema";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { readdir, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
const COMPRESSION_ROOT = "/app/data/compression";
|
||||
@@ -24,41 +30,78 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
|
||||
if (assetIds.length === 0) return res.status(200).json([]);
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: assets.id,
|
||||
originalFileName: assets.originalFileName,
|
||||
localDateTime: assets.localDateTime,
|
||||
originalSize: exif.fileSizeInByte,
|
||||
})
|
||||
.from(assets)
|
||||
.leftJoin(exif, eq(assets.id, exif.assetId))
|
||||
.where(and(eq(assets.ownerId, currentUser.id), inArray(assets.id, assetIds)));
|
||||
const results: any[] = [];
|
||||
const legacyAssetIds: string[] = [];
|
||||
|
||||
const results = await Promise.all(rows.map(async (asset) => {
|
||||
try {
|
||||
const outputPath = getCompressionOutputPath(asset.id, asset.originalFileName);
|
||||
const outputStat = await stat(outputPath);
|
||||
if (!outputStat.isFile()) return null;
|
||||
|
||||
return {
|
||||
assetId: asset.id,
|
||||
originalFileName: asset.originalFileName,
|
||||
outputFileName: getCompressionOutputFileName(asset.id, asset.originalFileName),
|
||||
localDateTime: asset.localDateTime,
|
||||
originalSize: Number(asset.originalSize ?? 0),
|
||||
compressedSize: outputStat.size,
|
||||
compressedAt: outputStat.mtime.toISOString(),
|
||||
resultUrl: `/api/assets/compression/result/${asset.id}`,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
for (const assetId of assetIds) {
|
||||
const job = await readCompressionJob(assetId);
|
||||
if (!job) {
|
||||
legacyAssetIds.push(assetId);
|
||||
continue;
|
||||
}
|
||||
}));
|
||||
if (job.ownerId !== currentUser.id) continue;
|
||||
|
||||
try {
|
||||
const outputPath = path.join(getCompressionDirectory(job.sourceAssetId), job.outputFileName);
|
||||
const outputStat = await stat(outputPath);
|
||||
if (!outputStat.isFile()) continue;
|
||||
|
||||
results.push({
|
||||
assetId: job.sourceAssetId,
|
||||
originalFileName: job.originalFileName,
|
||||
outputFileName: job.outputFileName,
|
||||
localDateTime: job.localDateTime,
|
||||
originalSize: job.originalSize,
|
||||
compressedSize: outputStat.size,
|
||||
compressedAt: job.compressedAt,
|
||||
resultUrl: `/api/assets/compression/result/${job.sourceAssetId}`,
|
||||
status: job.status,
|
||||
replacementAssetId: job.replacementAssetId,
|
||||
replacementStartedAt: job.replacementStartedAt,
|
||||
replacedAt: job.replacedAt,
|
||||
});
|
||||
} catch {
|
||||
// Ignore stale job directories whose compressed output is gone.
|
||||
}
|
||||
}
|
||||
|
||||
// Backwards compatibility for compressed files created before job.json existed.
|
||||
if (legacyAssetIds.length > 0) {
|
||||
const rows = await db
|
||||
.select({
|
||||
id: assets.id,
|
||||
originalFileName: assets.originalFileName,
|
||||
localDateTime: assets.localDateTime,
|
||||
originalSize: exif.fileSizeInByte,
|
||||
})
|
||||
.from(assets)
|
||||
.leftJoin(exif, eq(assets.id, exif.assetId))
|
||||
.where(and(eq(assets.ownerId, currentUser.id), inArray(assets.id, legacyAssetIds)));
|
||||
|
||||
for (const asset of rows) {
|
||||
try {
|
||||
const outputPath = getCompressionOutputPath(asset.id, asset.originalFileName);
|
||||
const outputStat = await stat(outputPath);
|
||||
if (!outputStat.isFile()) continue;
|
||||
|
||||
results.push({
|
||||
assetId: asset.id,
|
||||
originalFileName: asset.originalFileName,
|
||||
outputFileName: getCompressionOutputFileName(asset.id, asset.originalFileName),
|
||||
localDateTime: asset.localDateTime,
|
||||
originalSize: Number(asset.originalSize ?? 0),
|
||||
compressedSize: outputStat.size,
|
||||
compressedAt: outputStat.mtime.toISOString(),
|
||||
resultUrl: `/api/assets/compression/result/${asset.id}`,
|
||||
status: "compressed",
|
||||
});
|
||||
} catch {
|
||||
// Ignore incomplete legacy result directories.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(200).json(
|
||||
results
|
||||
.filter((result): result is NonNullable<typeof result> => result !== null)
|
||||
.sort((a, b) => b.compressedAt.localeCompare(a.compressedAt))
|
||||
results.sort((a, b) => b.compressedAt.localeCompare(a.compressedAt))
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
getCompressionOutputPath,
|
||||
getFfmpegCommand,
|
||||
runFfmpegCommand,
|
||||
writeCompressionJob,
|
||||
} from "@/helpers/compression.server";
|
||||
import { assets } from "@/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
@@ -26,6 +27,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
type: assets.type,
|
||||
originalFileName: assets.originalFileName,
|
||||
originalPath: assets.originalPath,
|
||||
localDateTime: assets.localDateTime,
|
||||
})
|
||||
.from(assets)
|
||||
.where(and(eq(assets.id, assetId), eq(assets.ownerId, currentUser.id)));
|
||||
@@ -45,12 +47,27 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const command = await getFfmpegCommand(currentUser.id);
|
||||
await runFfmpegCommand(command, inputPath, outputPath);
|
||||
const outputStat = await stat(outputPath);
|
||||
const outputFileName = outputPath.split("/").pop() || `${asset.id}-compressed.mp4`;
|
||||
|
||||
await writeCompressionJob({
|
||||
version: 1,
|
||||
ownerId: currentUser.id,
|
||||
sourceAssetId: asset.id,
|
||||
originalFileName: asset.originalFileName,
|
||||
originalPath: asset.originalPath,
|
||||
localDateTime: asset.localDateTime.toISOString(),
|
||||
originalSize: originalStat.size,
|
||||
outputFileName,
|
||||
compressedSize: outputStat.size,
|
||||
compressedAt: outputStat.mtime.toISOString(),
|
||||
status: "compressed",
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
assetId: asset.id,
|
||||
originalSize: originalStat.size,
|
||||
compressedSize: outputStat.size,
|
||||
outputFileName: outputPath.split("/").pop(),
|
||||
outputFileName,
|
||||
resultUrl: `/api/assets/compression/result/${asset.id}`,
|
||||
});
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -299,7 +299,7 @@ export default function CompressVideosPage() {
|
||||
assets: remainingAssets,
|
||||
selectedIds: contextState.selectedIds.filter((id) => id !== assetId),
|
||||
})
|
||||
setCompressedVideos((previous) => previous.filter((item) => item.assetId !== assetId))
|
||||
await loadCompressed()
|
||||
setQueue((previous) => previous.filter((item) => item.assetId !== assetId))
|
||||
toast.success('Asset replaced in Immich')
|
||||
} catch (error: any) {
|
||||
@@ -452,7 +452,7 @@ export default function CompressVideosPage() {
|
||||
<section className="rounded-lg border bg-card">
|
||||
<div className="border-b p-3">
|
||||
<div className="font-medium">Compressed files</div>
|
||||
<div className="text-xs text-muted-foreground">{compressedVideos.length} ready</div>
|
||||
<div className="text-xs text-muted-foreground">{compressedVideos.length} stored</div>
|
||||
</div>
|
||||
|
||||
<div className="max-h-64 overflow-auto">
|
||||
@@ -467,6 +467,8 @@ export default function CompressVideosPage() {
|
||||
: 0
|
||||
const replacing = replacingAssetId === item.assetId
|
||||
const discarding = discardingAssetId === item.assetId
|
||||
const alreadyReplaced = item.status === 'replaced'
|
||||
const replacementIncomplete = item.status === 'replacing'
|
||||
|
||||
return (
|
||||
<div key={item.assetId} className="border-b last:border-b-0 p-3">
|
||||
@@ -476,6 +478,12 @@ export default function CompressVideosPage() {
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{formatMB(item.originalSize)} → {formatMB(item.compressedSize)} · saved {formatMB(savedBytes)} ({savedPercent}%)
|
||||
</div>
|
||||
{alreadyReplaced && (
|
||||
<div className="text-xs text-muted-foreground">Replaced in Immich · replacement {item.replacementAssetId}</div>
|
||||
)}
|
||||
{replacementIncomplete && (
|
||||
<div className="text-xs text-destructive">Replacement incomplete · check Immich before retrying</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex shrink-0 gap-1">
|
||||
<Button
|
||||
@@ -495,14 +503,14 @@ export default function CompressVideosPage() {
|
||||
</Button>
|
||||
<AlertDialog
|
||||
title="Replace this asset in Immich?"
|
||||
description="The compressed video will be uploaded as a new Immich asset. Its associations and metadata will be copied, then the original asset will be permanently deleted."
|
||||
description="The compressed video will be uploaded as a new Immich asset. Its associations and metadata will be copied, then the original asset will be moved to Immich Trash."
|
||||
onConfirm={() => handleReplace(item.assetId)}
|
||||
variant="destructive"
|
||||
disabled={replacing || discarding}
|
||||
disabled={replacing || discarding || alreadyReplaced || replacementIncomplete}
|
||||
asChild
|
||||
>
|
||||
<Button variant="destructive" size="sm" disabled={replacing || discarding}>
|
||||
{replacing ? 'Replacing...' : 'Replace'}
|
||||
<Button variant="destructive" size="sm" disabled={replacing || discarding || alreadyReplaced || replacementIncomplete}>
|
||||
{replacing ? 'Replacing...' : alreadyReplaced ? 'Replaced' : replacementIncomplete ? 'Check Immich' : 'Replace'}
|
||||
</Button>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user