2024-08-19 20:06:38 +02:00
|
|
|
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
2024-04-07 07:30:49 +02:00
|
|
|
import sharp from "sharp";
|
2024-04-14 12:36:25 +02:00
|
|
|
import { z } from "zod";
|
2024-06-29 08:10:02 +02:00
|
|
|
import { MediaManager } from "~/classes/media/media-manager";
|
2024-06-13 03:03:57 +02:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
2024-08-19 14:43:54 +02:00
|
|
|
import { config } from "~/packages/config-manager/index";
|
2024-06-13 03:03:57 +02:00
|
|
|
import { Attachment } from "~/packages/database-interface/attachment";
|
2023-11-29 00:54:39 +01:00
|
|
|
|
2024-03-11 00:57:26 +01:00
|
|
|
export const meta = applyConfig({
|
2024-04-07 07:30:49 +02:00
|
|
|
allowedMethods: ["POST"],
|
|
|
|
|
ratelimits: {
|
|
|
|
|
max: 10,
|
|
|
|
|
duration: 60,
|
|
|
|
|
},
|
|
|
|
|
route: "/api/v1/media",
|
|
|
|
|
auth: {
|
|
|
|
|
required: true,
|
|
|
|
|
oauthPermissions: ["write:media"],
|
|
|
|
|
},
|
2024-06-08 06:57:29 +02:00
|
|
|
permissions: {
|
2024-06-13 04:26:43 +02:00
|
|
|
required: [RolePermissions.ManageOwnMedia],
|
2024-06-08 06:57:29 +02:00
|
|
|
},
|
2023-11-29 00:54:39 +01:00
|
|
|
});
|
|
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
export const schemas = {
|
|
|
|
|
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-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-05-06 09:16:33 +02:00
|
|
|
app.on(
|
|
|
|
|
meta.allowedMethods,
|
|
|
|
|
meta.route,
|
|
|
|
|
zValidator("form", schemas.form, handleZodError),
|
2024-06-08 06:57:29 +02:00
|
|
|
auth(meta.auth, meta.permissions),
|
2024-05-06 09:16:33 +02:00
|
|
|
async (context) => {
|
2024-05-29 03:14:24 +02:00
|
|
|
const { file, thumbnail, description } = context.req.valid("form");
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
if (file.size > config.validation.max_media_size) {
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json(
|
|
|
|
|
{
|
|
|
|
|
error: `File too large, max size is ${config.validation.max_media_size} bytes`,
|
|
|
|
|
},
|
2024-05-06 09:16:33 +02:00
|
|
|
413,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
config.validation.enforce_mime_types &&
|
|
|
|
|
!config.validation.allowed_mime_types.includes(file.type)
|
|
|
|
|
) {
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json({ error: "Invalid file type" }, 415);
|
2024-05-06 09:16:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sha256 = new Bun.SHA256();
|
|
|
|
|
|
|
|
|
|
const isImage = file.type.startsWith("image/");
|
|
|
|
|
|
|
|
|
|
const metadata = isImage
|
|
|
|
|
? await sharp(await file.arrayBuffer()).metadata()
|
|
|
|
|
: null;
|
|
|
|
|
|
2024-06-29 08:10:02 +02:00
|
|
|
const mediaManager = new MediaManager(config);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-06-29 08:10:02 +02:00
|
|
|
const { path, blurhash } = await mediaManager.addFile(file);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
2024-06-29 08:10:02 +02:00
|
|
|
const url = Attachment.getUrl(path);
|
2024-05-06 09:16:33 +02:00
|
|
|
|
|
|
|
|
let thumbnailUrl = "";
|
|
|
|
|
|
|
|
|
|
if (thumbnail) {
|
|
|
|
|
const { path } = await mediaManager.addFile(thumbnail);
|
|
|
|
|
|
2024-06-29 05:50:56 +02:00
|
|
|
thumbnailUrl = Attachment.getUrl(path);
|
2024-05-06 09:16:33 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 03:03:57 +02:00
|
|
|
const newAttachment = await Attachment.insert({
|
|
|
|
|
url,
|
|
|
|
|
thumbnailUrl,
|
|
|
|
|
sha256: sha256.update(await file.arrayBuffer()).digest("hex"),
|
|
|
|
|
mimeType: file.type,
|
|
|
|
|
description: description ?? "",
|
|
|
|
|
size: file.size,
|
|
|
|
|
blurhash: blurhash ?? undefined,
|
|
|
|
|
width: metadata?.width ?? undefined,
|
|
|
|
|
height: metadata?.height ?? undefined,
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
// TODO: Add job to process videos and other media
|
|
|
|
|
|
2024-08-19 21:03:59 +02:00
|
|
|
return context.json(newAttachment.toApi());
|
2024-05-06 09:16:33 +02:00
|
|
|
},
|
2024-08-19 20:06:38 +02:00
|
|
|
),
|
|
|
|
|
);
|