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