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