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.
This commit is contained in:
1 parent
642551064c
commit
5cbda35e32
50 files changed
+2429
-219
No files matched your search
@@ -0,0 +1,77 @@
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user