globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { l as jsonError } from "./crypto_CY2zBtG3.mjs";
import { a as deleteMedia, n as getFullDb } from "./db_DZJ1iBSP.mjs";
import { n as recordAudit, t as clientIp } from "./audit_CJOh7kpS.mjs";
import { c as assertCsrf, i as requireAdmin } from "./auth_B0DWM0G6.mjs";
import { t as getStorage } from "./storage_CReXjlye.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/admin/media/[...key].ts
var ____key__exports = /* @__PURE__ */ __exportAll({
	DELETE: () => DELETE,
	prerender: () => false
});
var DELETE = async ({ request, locals, params, cookies }) => {
	try {
		const { actor, authMode } = await requireAdmin(request, env, cookies);
		assertCsrf(request, {
			authMode,
			siteOrigin: env.SITE_ORIGIN
		});
		const key = params.key ?? "";
		if (!key.startsWith("media/")) return jsonError(404, "not_found", "media not found");
		const { db } = getFullDb(env);
		if (!await deleteMedia(db, key)) return jsonError(404, "not_found", "media not found");
		await getStorage(env).delete(key);
		await recordAudit(db, {
			actorId: actor.id,
			ip: clientIp(request)
		}, {
			action: "media.delete",
			entityType: "media",
			entityId: key
		});
		return new Response(null, { status: 204 });
	} catch (error) {
		const name = error?.name;
		if (name === "InvalidSessionError") return jsonError(401, "unauthorized", error.message);
		if (name === "RoleDeniedError") return jsonError(403, "forbidden", error.message);
		if (name === "CsrfError") return jsonError(403, "forbidden", error.message);
		if (name === "RateLimitedError") return jsonError(429, "rate_limited", error.message);
		return jsonError(500, "internal_error", "internal error");
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/media/[...key]@_@ts
var page = () => ____key__exports;
//#endregion
export { page };
