fix(icons): local icon repository does not work with medias (#1765)
This commit is contained in:
1
packages/icons/src/local.ts
Normal file
1
packages/icons/src/local.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { createLocalImageUrl, mapMediaToIcon, LOCAL_ICON_REPOSITORY_SLUG } from "./repositories/local.icon-repository";
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user