shared.ts
⎇
Raw
1import path from "node:path";
2import type { ProbeData } from "./utils";
3import commandExists from "command-exists";
4
5export const coverRegex = process.env.COVER_REGEX === undefined ? /(cover)|(folder).(png|jpg)$/i : "";
6export const musicRoot = process.env.MUSIC_ROOT || "";
7if (musicRoot === "") {
8 console.error("Please define the environment variable MUSIC_ROOT");
9 process.exit(1);
10}
11
12export const [username, password] = process.env.AUTH
13 ? Buffer.from(process.env.AUTH, "base64").toString().split(":", 2)
14 : [undefined, undefined];
15
16export const authTokens = new Map<string, Date>();
17
18//clear outdated tokens once a day
19setInterval(
20 () => {
21 for (const [token, endDate] of authTokens.entries()) {
22 if (endDate.getTime() < Date.now()) authTokens.delete(token);
23 }
24 },
25 1000 * 60 * 60 * 24,
26);
27
28if (!path.isAbsolute(musicRoot)) {
29 console.error(`Music root ${musicRoot} is not absolute`);
30 process.exit(1);
31}
32
33export const excludeExtension =
34 process.env.EXCLUDE_EXTENSION === undefined
35 ? ["txt", "log", "nfo", "m3u", "htm", "html"]
36 : process.env.EXCLUDE_EXTENSION.split(",");
37
38if (!commandExists.sync("ffmpeg")) {
39 console.error("Required command ffmpeg not found in PATH");
40 process.exit(1);
41}
42if (!commandExists.sync("tar")) {
43 console.error("Required command tar not found in PATH");
44 process.exit(1);
45}
46if (!commandExists.sync("avifenc")) {
47 console.error("Required command avifenc not found in PATH");
48 process.exit(1);
49}
50
51export const mediaTypes = ["audio", "video"];
52export const allowedTypes = [...mediaTypes, "image"];
53
54export const fileTypeCache = new Map<string, string>();
55export const probeCache = new Map<string, ProbeData>();
56export const generatedPlaylistIds = new Map<string, string[]>();
57
58export class PathInfo {
59 constructor(public mimeType: string) {}
60}
61
62export class ServerError {
63 constructor(
64 public status: number,
65 public error: string,
66 ) {}
67}
68