mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
refactor(database): 🔥 Simplify media management code
This commit is contained in:
parent
cf1104d762
commit
bc961b70bb
7 changed files with 173 additions and 109 deletions
|
|
@ -61,6 +61,7 @@ const schemas = {
|
|||
.min(1)
|
||||
.max(2000)
|
||||
.url()
|
||||
.transform((a) => new URL(a))
|
||||
.or(
|
||||
z
|
||||
.instanceof(File)
|
||||
|
|
@ -76,6 +77,7 @@ const schemas = {
|
|||
.min(1)
|
||||
.max(2000)
|
||||
.url()
|
||||
.transform((v) => new URL(v))
|
||||
.or(
|
||||
z
|
||||
.instanceof(File)
|
||||
|
|
@ -256,7 +258,7 @@ export default apiRoute((app) =>
|
|||
content_type: contentType,
|
||||
};
|
||||
} else {
|
||||
self.avatar = avatar;
|
||||
self.avatar = avatar.toString();
|
||||
self.source.avatar = {
|
||||
content_type: await mimeLookup(avatar),
|
||||
};
|
||||
|
|
@ -274,7 +276,7 @@ export default apiRoute((app) =>
|
|||
content_type: contentType,
|
||||
};
|
||||
} else {
|
||||
self.header = header;
|
||||
self.header = header.toString();
|
||||
self.source.header = {
|
||||
content_type: await mimeLookup(header),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
import { apiRoute, auth, emojiValidator, jsonOrForm } from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import type { ContentFormat } from "@versia/federation/types";
|
||||
import { Emoji, Media, db } from "@versia/kit/db";
|
||||
import { Emoji, db } from "@versia/kit/db";
|
||||
import { Emojis, RolePermissions } from "@versia/kit/tables";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -32,6 +31,7 @@ const schemas = {
|
|||
.min(1)
|
||||
.max(2000)
|
||||
.url()
|
||||
.transform((a) => new URL(a))
|
||||
.or(
|
||||
z
|
||||
.instanceof(File)
|
||||
|
|
@ -247,9 +247,6 @@ export default apiRoute((app) => {
|
|||
);
|
||||
}
|
||||
|
||||
const modifiedMedia = structuredClone(emoji.data.media);
|
||||
const modified = structuredClone(emoji.data);
|
||||
|
||||
if (element) {
|
||||
// Check of emoji is an image
|
||||
const contentType =
|
||||
|
|
@ -265,40 +262,24 @@ export default apiRoute((app) => {
|
|||
);
|
||||
}
|
||||
|
||||
let contentFormat: ContentFormat | undefined;
|
||||
|
||||
if (element instanceof File) {
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const { uploadedFile, path } =
|
||||
await mediaManager.addFile(element);
|
||||
|
||||
contentFormat = await Media.fileToContentFormat(
|
||||
uploadedFile,
|
||||
Media.getUrl(path),
|
||||
{ description: alt },
|
||||
);
|
||||
await emoji.media.updateFromFile(element);
|
||||
} else {
|
||||
contentFormat = {
|
||||
[contentType]: {
|
||||
content: element,
|
||||
remote: true,
|
||||
description: alt,
|
||||
},
|
||||
};
|
||||
await emoji.media.updateFromUrl(element);
|
||||
}
|
||||
|
||||
modifiedMedia.content = contentFormat;
|
||||
}
|
||||
|
||||
modified.shortcode = shortcode ?? modified.shortcode;
|
||||
modified.category = category ?? modified.category;
|
||||
|
||||
if (emojiGlobal !== undefined) {
|
||||
modified.ownerId = emojiGlobal ? null : user.data.id;
|
||||
if (alt) {
|
||||
await emoji.media.updateMetadata({
|
||||
description: alt,
|
||||
});
|
||||
}
|
||||
|
||||
await emoji.update(modified);
|
||||
await emoji.update({
|
||||
shortcode,
|
||||
ownerId: emojiGlobal ? null : user.data.id,
|
||||
category,
|
||||
});
|
||||
|
||||
return context.json(emoji.toApi(), 200);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
import { apiRoute, auth, emojiValidator, jsonOrForm } from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import type { ContentFormat } from "@versia/federation/types";
|
||||
import { Emoji, Media } from "@versia/kit/db";
|
||||
import { Emojis, RolePermissions } from "@versia/kit/tables";
|
||||
import { and, eq, isNull, or } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -28,6 +26,7 @@ const schemas = {
|
|||
.min(1)
|
||||
.max(2000)
|
||||
.url()
|
||||
.transform((a) => new URL(a))
|
||||
.or(
|
||||
z
|
||||
.instanceof(File)
|
||||
|
|
@ -143,31 +142,14 @@ export default apiRoute((app) =>
|
|||
);
|
||||
}
|
||||
|
||||
let contentFormat: ContentFormat | undefined;
|
||||
|
||||
if (element instanceof File) {
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const { uploadedFile, path } = await mediaManager.addFile(element);
|
||||
|
||||
contentFormat = await Media.fileToContentFormat(
|
||||
uploadedFile,
|
||||
Media.getUrl(path),
|
||||
{ description: alt },
|
||||
);
|
||||
} else {
|
||||
contentFormat = {
|
||||
[contentType]: {
|
||||
content: element,
|
||||
remote: true,
|
||||
description: alt,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const media = await Media.insert({
|
||||
content: contentFormat,
|
||||
});
|
||||
const media =
|
||||
element instanceof File
|
||||
? await Media.fromFile(element, {
|
||||
description: alt,
|
||||
})
|
||||
: await Media.fromUrl(element, {
|
||||
description: alt,
|
||||
});
|
||||
|
||||
const emoji = await Emoji.insert({
|
||||
shortcode,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import { Media } from "@versia/kit/db";
|
|||
import { RolePermissions } from "@versia/kit/tables";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -101,47 +100,26 @@ export default apiRoute((app) => {
|
|||
app.openapi(routePut, async (context) => {
|
||||
const { id } = context.req.valid("param");
|
||||
|
||||
const attachment = await Media.fromId(id);
|
||||
const media = await Media.fromId(id);
|
||||
|
||||
if (!attachment) {
|
||||
if (!media) {
|
||||
throw new ApiError(404, "Media not found");
|
||||
}
|
||||
|
||||
const { description, thumbnail: thumbnailFile } =
|
||||
context.req.valid("form");
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
// TODO: Generate thumbnail if not provided
|
||||
if (thumbnailFile) {
|
||||
const { path } = await mediaManager.addFile(thumbnailFile);
|
||||
const thumbnail = attachment.data.thumbnail;
|
||||
|
||||
// FIXME: Also update thumbnail if it hasn't been set
|
||||
if (thumbnail) {
|
||||
thumbnail[Object.keys(thumbnail)[0]].content =
|
||||
Media.getUrl(path);
|
||||
}
|
||||
|
||||
attachment.data.thumbnail = thumbnail;
|
||||
await media.updateThumbnail(thumbnailFile);
|
||||
}
|
||||
|
||||
if (description) {
|
||||
for (const type of Object.keys(attachment.data.content)) {
|
||||
attachment.data.content[type].description = description;
|
||||
}
|
||||
}
|
||||
|
||||
if (description || thumbnailFile) {
|
||||
await attachment.update({
|
||||
content: attachment.data.content,
|
||||
thumbnail: attachment.data.thumbnail,
|
||||
await media.updateMetadata({
|
||||
description,
|
||||
});
|
||||
|
||||
return context.json(attachment.toApi(), 200);
|
||||
}
|
||||
|
||||
return context.json(attachment.toApi(), 200);
|
||||
return context.json(media.toApi(), 200);
|
||||
});
|
||||
|
||||
app.openapi(routeGet, async (context) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue