This commit is contained in:
2026-03-14 17:32:00 -05:00
parent b776ec3381
commit f1fd2ed979
7 changed files with 197 additions and 10 deletions

View File

@@ -5,7 +5,7 @@ import { z } from "zod";
import { fail, ok } from "../../lib/http.js";
import { requirePermissions } from "../../lib/rbac.js";
import { createAttachment, getAttachmentContent, getAttachmentMetadata } from "./service.js";
import { createAttachment, getAttachmentContent, getAttachmentMetadata, listAttachmentsByOwner } from "./service.js";
const upload = multer({
storage: multer.memoryStorage(),
@@ -19,8 +19,22 @@ const uploadSchema = z.object({
ownerId: z.string().min(1),
});
const listSchema = z.object({
ownerType: z.string().min(1),
ownerId: z.string().min(1),
});
export const filesRouter = Router();
filesRouter.get("/", requirePermissions([permissions.filesRead]), async (request, response) => {
const parsed = listSchema.safeParse(request.query);
if (!parsed.success) {
return fail(response, 400, "INVALID_INPUT", "ownerType and ownerId are required.");
}
return ok(response, await listAttachmentsByOwner(parsed.data.ownerType, parsed.data.ownerId));
});
filesRouter.post(
"/upload",
requirePermissions([permissions.filesWrite]),

View File

@@ -56,6 +56,18 @@ export async function getAttachmentMetadata(id: string) {
);
}
export async function listAttachmentsByOwner(ownerType: string, ownerId: string) {
const files = await prisma.fileAttachment.findMany({
where: {
ownerType,
ownerId,
},
orderBy: [{ createdAt: "desc" }, { originalName: "asc" }],
});
return files.map(mapFile);
}
export async function getAttachmentContent(id: string) {
const file = await prisma.fileAttachment.findUniqueOrThrow({
where: { id },
@@ -66,4 +78,3 @@ export async function getAttachmentContent(id: string) {
content: await fs.readFile(path.join(paths.dataDir, file.relativePath)),
};
}