2024-12-30 20:18:48 +01:00
|
|
|
import { apiRoute, auth } from "@/api";
|
2025-02-05 21:49:39 +01:00
|
|
|
import { createRoute, z } from "@hono/zod-openapi";
|
2025-01-23 16:08:42 +01:00
|
|
|
import { Media } from "@versia/kit/db";
|
2024-11-01 21:05:54 +01:00
|
|
|
import { RolePermissions } from "@versia/kit/tables";
|
2025-02-12 23:25:22 +01:00
|
|
|
import { Attachment as AttachmentSchema } from "~/classes/schemas/attachment";
|
2024-10-04 15:22:48 +02:00
|
|
|
import { config } from "~/packages/config-manager/index.ts";
|
2024-09-15 14:28:47 +02:00
|
|
|
import { ErrorSchema } from "~/types/api";
|
2023-11-29 00:54:39 +01:00
|
|
|
|
2024-12-30 20:26:56 +01:00
|
|
|
const schemas = {
|
2024-05-06 09:16:33 +02:00
|
|
|
form: z.object({
|
|
|
|
|
file: z.instanceof(File),
|
|
|
|
|
thumbnail: z.instanceof(File).optional(),
|
|
|
|
|
description: z
|
|
|
|
|
.string()
|
|
|
|
|
.max(config.validation.max_media_description_size)
|
|
|
|
|
.optional(),
|
|
|
|
|
focus: z.string().optional(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "post",
|
|
|
|
|
path: "/api/v1/media",
|
|
|
|
|
summary: "Upload media",
|
2024-12-30 19:18:31 +01:00
|
|
|
middleware: [
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
scopes: ["write:media"],
|
|
|
|
|
permissions: [RolePermissions.ManageOwnMedia],
|
|
|
|
|
}),
|
|
|
|
|
] as const,
|
2024-09-15 14:28:47 +02:00
|
|
|
request: {
|
|
|
|
|
body: {
|
|
|
|
|
content: {
|
|
|
|
|
"multipart/form-data": {
|
|
|
|
|
schema: schemas.form,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Attachment",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-02-12 23:25:22 +01:00
|
|
|
schema: AttachmentSchema,
|
2024-09-15 14:28:47 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-30 19:38:41 +01:00
|
|
|
|
2024-09-15 14:28:47 +02:00
|
|
|
413: {
|
|
|
|
|
description: "File too large",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
415: {
|
|
|
|
|
description: "Disallowed file type",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: ErrorSchema,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.openapi(route, async (context) => {
|
|
|
|
|
const { file, thumbnail, description } = context.req.valid("form");
|
|
|
|
|
|
2025-01-23 16:08:42 +01:00
|
|
|
const attachment = await Media.fromFile(file, {
|
2025-01-06 19:21:57 +01:00
|
|
|
thumbnail,
|
|
|
|
|
description,
|
2024-09-15 14:28:47 +02:00
|
|
|
});
|
|
|
|
|
|
2025-01-06 19:21:57 +01:00
|
|
|
return context.json(attachment.toApi(), 200);
|
2024-09-15 14:28:47 +02:00
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|