import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { t as enrichImageMetadata } from "../../../../../enrich-BQ0mxJRs.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../../error-CkAseSqZ.mjs";
import { r as parseOptionalBody, t as isParseError } from "../../../../../parse-GmjecGct.mjs";
import { Ht as mediaConfirmBody } from "../../../../../redirects-DXEXBAII.mjs";
import "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { n as requirePerm, t as requireOwnerPerm } from "../../../../../authorize-Dz88F_Fw.mjs";
import { MediaRepository } from "emdash";

//#region src/astro/routes/api/media/[id]/confirm.ts
const prerender = false;
/**
* Max raw bytes to buffer for server-side LQIP generation at confirm time. The
* signed-URL upload flow exists so large files bypass server buffering — re-reading
* the whole object into a Worker's 128 MB heap to compute a blurhash would OOM
* on the very uploads that flow was designed for. LQIP is progressive
* enhancement: large images simply ship without a server-generated placeholder.
*/
const MAX_PLACEHOLDER_DOWNLOAD_BYTES = 8 * 1024 * 1024;
/**
* Add URL to media item (relative URL for portability)
*/
function addUrlToMedia(item) {
	return {
		...item,
		url: `/_emdash/api/media/file/${item.storageKey}`
	};
}
/**
* Confirm upload completion
*/
const POST = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { id } = params;
	const denied = requirePerm(user, "media:upload");
	if (denied) return denied;
	if (!id) return apiError("INVALID_REQUEST", "Media ID is required", 400);
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		const body = await parseOptionalBody(request, mediaConfirmBody, {});
		if (isParseError(body)) return body;
		const repo = new MediaRepository(emdash.db);
		const existing = await repo.findById(id);
		if (!existing) return apiError("NOT_FOUND", `Media item not found: ${id}`, 404);
		if (existing.status !== "pending") return apiError("INVALID_STATE", `Media item is not pending: ${existing.status}`, 400);
		const ownerDenied = requireOwnerPerm(user, existing.authorId ?? "", "media:edit_own", "media:edit_any");
		if (ownerDenied) return ownerDenied;
		if (emdash.storage) {
			if (!await emdash.storage.exists(existing.storageKey)) {
				await repo.markFailed(id);
				return apiError("FILE_NOT_FOUND", "File was not uploaded to storage", 400);
			}
		}
		let blurhash;
		let dominantColor;
		let width = body.width;
		let height = body.height;
		if (emdash.storage && existing.mimeType.startsWith("image/")) {
			const knownSize = body.size ?? existing.size ?? void 0;
			if (!(knownSize != null && knownSize > MAX_PLACEHOLDER_DOWNLOAD_BYTES)) try {
				const { body: stream } = await emdash.storage.download(existing.storageKey);
				const bytes = new Uint8Array(await new Response(stream).arrayBuffer());
				if (bytes.byteLength > MAX_PLACEHOLDER_DOWNLOAD_BYTES) console.warn(`[media] confirm skipping placeholder: object ${existing.storageKey} is ${bytes.byteLength} bytes (> ${MAX_PLACEHOLDER_DOWNLOAD_BYTES})`);
				else {
					const enriched = await enrichImageMetadata(bytes, existing.mimeType, { knownDimensions: body.width != null && body.height != null ? {
						width: body.width,
						height: body.height
					} : void 0 });
					blurhash = enriched.blurhash;
					dominantColor = enriched.dominantColor;
					width = width ?? enriched.width;
					height = height ?? enriched.height;
				}
			} catch (error) {
				console.error("[media] confirm placeholder generation failed:", error);
			}
			else console.warn(`[media] confirm skipping placeholder: object ${existing.storageKey} reported size ${knownSize} bytes (> ${MAX_PLACEHOLDER_DOWNLOAD_BYTES})`);
		}
		const item = await repo.confirmUpload(id, {
			size: body.size,
			width,
			height,
			blurhash,
			dominantColor
		});
		if (!item) return apiError("CONFIRM_FAILED", "Failed to confirm upload", 500);
		return apiSuccess({ item: addUrlToMedia(item) });
	} catch (error) {
		return handleError(error, "Failed to confirm upload", "CONFIRM_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=confirm.mjs.map