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