fix(icons): local icon repository does not work with medias (#1765)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { InferInsertModel } from "@homarr/db";
|
||||
import { and, createId, desc, eq, like } from "@homarr/db";
|
||||
import { medias } from "@homarr/db/schema";
|
||||
import { iconRepositories, icons, medias } from "@homarr/db/schema";
|
||||
import { createLocalImageUrl, LOCAL_ICON_REPOSITORY_SLUG, mapMediaToIcon } from "@homarr/icons/local";
|
||||
import { validation, z } from "@homarr/validation";
|
||||
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure } from "../../trpc";
|
||||
@@ -52,13 +54,29 @@ export const mediaRouter = createTRPCRouter({
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const content = Buffer.from(await input.file.arrayBuffer());
|
||||
const id = createId();
|
||||
await ctx.db.insert(medias).values({
|
||||
const media = {
|
||||
id,
|
||||
creatorId: ctx.session.user.id,
|
||||
content,
|
||||
size: input.file.size,
|
||||
contentType: input.file.type,
|
||||
name: input.file.name,
|
||||
} satisfies InferInsertModel<typeof medias>;
|
||||
await ctx.db.insert(medias).values(media);
|
||||
|
||||
const localIconRepository = await ctx.db.query.iconRepositories.findFirst({
|
||||
where: eq(iconRepositories.slug, LOCAL_ICON_REPOSITORY_SLUG),
|
||||
});
|
||||
|
||||
if (!localIconRepository) return id;
|
||||
|
||||
const icon = mapMediaToIcon(media);
|
||||
await ctx.db.insert(icons).values({
|
||||
id: createId(),
|
||||
checksum: icon.checksum,
|
||||
name: icon.fileNameWithExtension,
|
||||
url: icon.imageUrl,
|
||||
iconRepositoryId: localIconRepository.id,
|
||||
});
|
||||
|
||||
return id;
|
||||
@@ -67,6 +85,7 @@ export const mediaRouter = createTRPCRouter({
|
||||
const dbMedia = await ctx.db.query.medias.findFirst({
|
||||
where: eq(medias.id, input.id),
|
||||
columns: {
|
||||
id: true,
|
||||
creatorId: true,
|
||||
},
|
||||
});
|
||||
@@ -87,5 +106,6 @@ export const mediaRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
await ctx.db.delete(medias).where(eq(medias.id, input.id));
|
||||
await ctx.db.delete(icons).where(eq(icons.url, createLocalImageUrl(input.id)));
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user