fix(icons): local icon repository does not work with medias (#1765)

This commit is contained in:
Meier Lukas
2024-12-24 14:15:18 +01:00
committed by GitHub
parent f127c9325c
commit e220087e96
6 changed files with 49 additions and 13 deletions

View File

@@ -0,0 +1 @@
export { createLocalImageUrl, mapMediaToIcon, LOCAL_ICON_REPOSITORY_SLUG } from "./repositories/local.icon-repository";

View File

@@ -1,26 +1,36 @@
import { createHash } from "crypto";
import type { InferSelectModel } from "@homarr/db";
import { db } from "@homarr/db";
import type { medias } from "@homarr/db/schema";
import type { RepositoryIconGroup } from "../types";
import type { RepositoryIcon, RepositoryIconGroup } from "../types";
import { IconRepository } from "./icon-repository";
export const LOCAL_ICON_REPOSITORY_SLUG = "local";
export class LocalIconRepository extends IconRepository {
constructor() {
super("Local", "local", undefined, undefined, undefined, undefined);
super("Local", LOCAL_ICON_REPOSITORY_SLUG, undefined, undefined, undefined, undefined);
}
protected async getAllIconsInternalAsync(): Promise<RepositoryIconGroup> {
const medias = await db.query.medias.findMany();
return {
success: true,
icons: medias.map((media) => ({
local: true,
fileNameWithExtension: media.name,
imageUrl: `/api/user-medias/${media.id}`,
checksum: createHash("md5").update(media.content).digest("hex"),
sizeInBytes: media.size,
})),
slug: "local",
icons: medias.map(mapMediaToIcon),
slug: LOCAL_ICON_REPOSITORY_SLUG,
};
}
}
export const createLocalImageUrl = (id: string) => `/api/user-medias/${id}`;
export const mapMediaToIcon = (
media: Pick<InferSelectModel<typeof medias>, "name" | "id" | "content" | "size">,
): RepositoryIcon => ({
local: true,
fileNameWithExtension: media.name,
imageUrl: createLocalImageUrl(media.id),
checksum: createHash("md5").update(media.content).digest("hex"),
sizeInBytes: media.size,
});