feat: compression 13
This commit is contained in:
1 parent
cf6b9c35c2
commit
74e4b51e4e
1 file changed
+24
-3
@@ -5,7 +5,7 @@ import { getUserHeaders } from "@/helpers/user.helper";
|
||||
import { clearCompressionDirectory, getCompressionOutputPath } from "@/helpers/compression.server";
|
||||
import { assets } from "@/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { openAsBlob } from "node:fs";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
@@ -15,6 +15,24 @@ const readError = async (response: Response) => {
|
||||
return body || response.statusText || `HTTP ${response.status}`;
|
||||
};
|
||||
|
||||
class UploadFile extends File {
|
||||
constructor(
|
||||
private readonly filePath: string,
|
||||
private readonly fileSize: number,
|
||||
fileName: string,
|
||||
) {
|
||||
super([], fileName);
|
||||
}
|
||||
|
||||
override get size() {
|
||||
return this.fileSize;
|
||||
}
|
||||
|
||||
override stream() {
|
||||
return createReadStream(this.filePath) as any;
|
||||
}
|
||||
}
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method !== "POST") return res.status(405).json({ message: "Method not allowed" });
|
||||
|
||||
@@ -67,7 +85,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const replacementFileName = path.basename(source.originalFileName || dbAsset.originalFileName || `${dbAsset.id}.mp4`);
|
||||
const outputPath = getCompressionOutputPath(dbAsset.id, dbAsset.originalFileName);
|
||||
const outputStats = await stat(outputPath);
|
||||
const uploadFile = await openAsBlob(outputPath);
|
||||
const uploadFile = new UploadFile(outputPath, outputStats.size, replacementFileName);
|
||||
|
||||
// Keep the upload payload aligned with Immich's own CLI uploader.
|
||||
// Restore Power Tools/custom metadata only after Immich has created the asset.
|
||||
@@ -77,7 +95,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
formData.set("fileSize", String(outputStats.size));
|
||||
formData.set("filename", replacementFileName);
|
||||
formData.set("isFavorite", source.isFavorite ? "true" : "false");
|
||||
formData.set("assetData", uploadFile, replacementFileName);
|
||||
// Important: do not pass the optional filename argument here. Node/Undici
|
||||
// clones custom File instances when that argument is provided, which loses
|
||||
// the overridden stream() implementation and causes a content-length mismatch.
|
||||
formData.set("assetData", uploadFile);
|
||||
if (source.visibility) formData.set("visibility", source.visibility);
|
||||
|
||||
const { ["Content-Type"]: _contentType, ...uploadHeaders } = jsonHeaders;
|
||||
|
||||
Reference in new issue
Block a user