mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Make user avatar and header into a Media instead of plaintext
This commit is contained in:
parent
bc961b70bb
commit
ba431e2b11
11 changed files with 2480 additions and 89 deletions
|
|
@ -1,16 +1,14 @@
|
|||
import { apiRoute, auth, jsonOrForm } from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { mergeAndDeduplicate } from "@/lib";
|
||||
import { sanitizedHtmlStrip } from "@/sanitization";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { Emoji, Media, User } from "@versia/kit/db";
|
||||
import { Emoji, User } from "@versia/kit/db";
|
||||
import { RolePermissions, Users } from "@versia/kit/tables";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import ISO6391 from "iso-639-1";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { contentToHtml } from "~/classes/functions/status";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -206,8 +204,6 @@ export default apiRoute((app) =>
|
|||
display_name ?? "",
|
||||
);
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
if (display_name) {
|
||||
self.displayName = sanitizedDisplayName;
|
||||
}
|
||||
|
|
@ -249,37 +245,17 @@ export default apiRoute((app) =>
|
|||
|
||||
if (avatar) {
|
||||
if (avatar instanceof File) {
|
||||
const { path, uploadedFile } =
|
||||
await mediaManager.addFile(avatar);
|
||||
const contentType = uploadedFile.type;
|
||||
|
||||
self.avatar = Media.getUrl(path);
|
||||
self.source.avatar = {
|
||||
content_type: contentType,
|
||||
};
|
||||
await user.avatar?.updateFromFile(avatar);
|
||||
} else {
|
||||
self.avatar = avatar.toString();
|
||||
self.source.avatar = {
|
||||
content_type: await mimeLookup(avatar),
|
||||
};
|
||||
await user.avatar?.updateFromUrl(avatar);
|
||||
}
|
||||
}
|
||||
|
||||
if (header) {
|
||||
if (header instanceof File) {
|
||||
const { path, uploadedFile } =
|
||||
await mediaManager.addFile(header);
|
||||
const contentType = uploadedFile.type;
|
||||
|
||||
self.header = Media.getUrl(path);
|
||||
self.source.header = {
|
||||
content_type: contentType,
|
||||
};
|
||||
await user.header?.updateFromFile(header);
|
||||
} else {
|
||||
self.header = header.toString();
|
||||
self.source.header = {
|
||||
content_type: await mimeLookup(header),
|
||||
};
|
||||
await user.header?.updateFromUrl(header);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,9 +30,7 @@ export default apiRoute((app) =>
|
|||
app.openapi(route, async (context) => {
|
||||
const { user } = context.get("auth");
|
||||
|
||||
await user.update({
|
||||
avatar: "",
|
||||
});
|
||||
await user.header?.delete();
|
||||
|
||||
return context.json(user.toApi(true), 200);
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -30,9 +30,7 @@ export default apiRoute((app) =>
|
|||
app.openapi(route, async (context) => {
|
||||
const { user } = context.get("auth");
|
||||
|
||||
await user.update({
|
||||
header: "",
|
||||
});
|
||||
await user.header?.delete();
|
||||
|
||||
return context.json(user.toApi(true), 200);
|
||||
}),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue