105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { NextResponse } from "next/server";
|
|
import { apiRoute } from "../../../../lib/server/apiRoute";
|
|
import { prisma } from "../../../../lib/server/db";
|
|
import { isDatabaseConfigured } from "../../../../lib/server/env";
|
|
import {
|
|
dbUnavailable,
|
|
notFound,
|
|
serverMisconfigured,
|
|
} from "../../../../lib/server/responses";
|
|
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
|
|
import { getSessionUser } from "../../../../lib/server/session";
|
|
import { isValidUploadFileId } from "../../../../lib/server/uploads/uploadConstants";
|
|
import { resolveUploadedFileById } from "../../../../lib/server/uploads/resolveUploadedFile";
|
|
import { getUploadRootFromEnv } from "../../../../lib/server/uploads/uploadRoot";
|
|
import { browserHoldsUploadGrant } from "../../../../lib/server/uploads/uploadGrantCookie";
|
|
import { RULE_VISIBILITY } from "../../../../lib/ruleVisibility";
|
|
|
|
type RouteContext = { params: Promise<{ id: string }> };
|
|
|
|
function cacheControlForVisibility(
|
|
visibility: "private" | "shared_by_link" | "public_and_featured" | null,
|
|
): string {
|
|
if (visibility === RULE_VISIBILITY.public_and_featured) {
|
|
return "public, max-age=3600";
|
|
}
|
|
return "private, no-store";
|
|
}
|
|
|
|
export const GET = apiRoute<RouteContext>(
|
|
"uploads.byId",
|
|
async (request, context) => {
|
|
if (!getUploadRootFromEnv()) {
|
|
return serverMisconfigured(
|
|
"File uploads are not configured (UPLOAD_ROOT is unset).",
|
|
);
|
|
}
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const { id } = await context.params;
|
|
if (!isValidUploadFileId(id)) {
|
|
return notFound("Upload not found");
|
|
}
|
|
|
|
const row = await prisma.upload.findUnique({
|
|
where: { id: id.toLowerCase() },
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
draftId: true,
|
|
ruleId: true,
|
|
rule: { select: { visibility: true } },
|
|
draft: { select: { userId: true } },
|
|
},
|
|
});
|
|
if (!row) {
|
|
return notFound("Upload not found");
|
|
}
|
|
|
|
const user = await getSessionUser();
|
|
let allowed = false;
|
|
let visibility: "private" | "shared_by_link" | "public_and_featured" | null =
|
|
row.rule?.visibility ?? null;
|
|
|
|
if (row.ruleId) {
|
|
const resolved = await getReadablePublishedRule({
|
|
idOrSlug: row.ruleId,
|
|
user,
|
|
viaShareSlug: visibility === RULE_VISIBILITY.shared_by_link,
|
|
lookup: "id",
|
|
requestPath: request.nextUrl.pathname,
|
|
});
|
|
allowed = resolved != null;
|
|
visibility = resolved?.visibility ?? visibility;
|
|
} else if (row.draftId) {
|
|
allowed = Boolean(user && row.draft?.userId === user.id);
|
|
} else if (user && row.userId === user.id) {
|
|
allowed = true;
|
|
} else if (!user) {
|
|
allowed = await browserHoldsUploadGrant(row.id);
|
|
}
|
|
|
|
if (!allowed) {
|
|
return notFound("Upload not found");
|
|
}
|
|
|
|
const resolved = await resolveUploadedFileById(row.id);
|
|
if (!resolved) {
|
|
return notFound("Upload not found");
|
|
}
|
|
|
|
const body = await readFile(resolved.absolutePath);
|
|
return new NextResponse(new Uint8Array(body), {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": resolved.contentType,
|
|
"Cache-Control": cacheControlForVisibility(visibility),
|
|
"X-Content-Type-Options": "nosniff",
|
|
},
|
|
});
|
|
},
|
|
);
|