Files
gerbeur/api/services/providers/self.ts
khannurien 856511777c
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 46s
chore: smaller docker image, simplification pass on environment variables, fix direct navigation without vite
2026-04-08 19:43:19 +00:00

84 lines
2.4 KiB
TypeScript

import type { RichContent } from "../../model/interfaces.ts";
import type { RichContentProvider } from "../rich-content-service.ts";
import { getDump } from "../dump-service.ts";
import { getUserByUsername } from "../user-service.ts";
import { getPlaylistById } from "../playlist-service.ts";
import { PUBLIC_URL } from "../../config.ts";
const DUMP_RE = /^\/dumps\/([^/]+)$/;
const USER_RE = /^\/users\/([^/]+)$/;
const PLAYLIST_RE = /^\/playlists\/([^/]+)$/;
export const selfProvider: RichContentProvider = {
name: "self",
matches(url: string): boolean {
try {
const { pathname } = new URL(url);
return DUMP_RE.test(pathname) ||
USER_RE.test(pathname) ||
PLAYLIST_RE.test(pathname);
} catch {
return false;
}
},
fetch(url: string): Promise<RichContent> {
const { pathname } = new URL(url);
const dumpMatch = pathname.match(DUMP_RE);
if (dumpMatch) {
const dump = getDump(dumpMatch[1]);
let thumbnailUrl: string | undefined;
if (dump.kind === "file" && dump.fileMime?.startsWith("image/")) {
thumbnailUrl = `${PUBLIC_URL}/api/files/${dump.id}`;
} else if (dump.richContent?.thumbnailUrl) {
thumbnailUrl = dump.richContent.thumbnailUrl;
}
return Promise.resolve({
type: "generic",
url,
siteName: "gerbeur",
title: dump.title,
description: dump.comment,
thumbnailUrl,
});
}
const userMatch = pathname.match(USER_RE);
if (userMatch) {
const user = getUserByUsername(userMatch[1]);
const thumbnailUrl = user.avatarMime
? `${PUBLIC_URL}/api/avatars/${user.id}`
: undefined;
return Promise.resolve({
type: "generic",
url,
siteName: "gerbeur",
title: user.username,
description: user.description,
thumbnailUrl,
});
}
const playlistMatch = pathname.match(PLAYLIST_RE);
if (playlistMatch) {
const playlist = getPlaylistById(playlistMatch[1]);
const thumbnailUrl = playlist.imageMime
? `${PUBLIC_URL}/api/playlists/${playlist.id}/image`
: undefined;
return Promise.resolve({
type: "generic",
url,
siteName: "gerbeur",
title: playlist.title,
description: playlist.description,
thumbnailUrl,
});
}
// Should not reach here if matches() is correct
return Promise.resolve({ type: "generic", url });
},
};