Files
community-rule/lib/server/uploads/deleteUploadedFiles.ts
T
adilallo 5cbda35e32 Start every Rule private, with a rotating link and files that follow its readers.
New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
2026-11-09 19:54:04 -07:00

78 lines
2.2 KiB
TypeScript

import { unlink } from "node:fs/promises";
import { logger } from "../../logger";
import { prisma } from "../db";
import { resolveUploadedFileById } from "./resolveUploadedFile";
async function unlinkResolved(id: string): Promise<void> {
const resolved = await resolveUploadedFileById(id);
if (!resolved) return;
try {
await unlink(resolved.absolutePath);
} catch (err) {
logger.warn("[uploads] failed to unlink file", id, err);
}
}
export async function deleteDiskFilesForUploadIds(
ids: string[],
): Promise<void> {
const unique = [...new Set(ids)];
for (const id of unique) {
await unlinkResolved(id);
}
}
export async function deleteUploadsForRule(ruleId: string): Promise<void> {
const rows = await prisma.upload.findMany({
where: { ruleId },
select: { id: true },
});
await deleteDiskFilesForUploadIds(rows.map((r) => r.id));
}
export async function deleteUploadsForDraft(draftId: string): Promise<void> {
const rows = await prisma.upload.findMany({
where: { draftId },
select: { id: true },
});
await deleteDiskFilesForUploadIds(rows.map((r) => r.id));
}
export async function deleteUploadsForUser(userId: string): Promise<void> {
const [ownedRules, draft, ownedUploads] = await Promise.all([
prisma.publishedRule.findMany({
where: { userId },
select: { id: true },
}),
prisma.ruleDraft.findUnique({
where: { userId },
select: { id: true },
}),
prisma.upload.findMany({
where: { userId },
select: { id: true },
}),
]);
const ruleIds = ownedRules.map((r) => r.id);
const [ruleUploads, draftUploads] = await Promise.all([
ruleIds.length === 0
? Promise.resolve([] as Array<{ id: string }>)
: prisma.upload.findMany({
where: { ruleId: { in: ruleIds } },
select: { id: true },
}),
draft
? prisma.upload.findMany({
where: { draftId: draft.id },
select: { id: true },
})
: Promise.resolve([] as Array<{ id: string }>),
]);
const ids = [
...ownedUploads.map((r) => r.id),
...ruleUploads.map((r) => r.id),
...draftUploads.map((r) => r.id),
];
await deleteDiskFilesForUploadIds(ids);
}