utils.ts
⎇
Raw
1import { StatusMap } from "elysia";
2import { fileTypeFromFile } from "file-type";
3import type { FileListingWithStatus, Metadata } from "music-server-shared/types";
4import { readdir, stat } from "node:fs/promises";
5import path from "node:path";
6import {
7 args,
8 coverRegex,
9 excludeExtension,
10 fileTypeCache,
11 mediaTypes,
12 musicRoot,
13 PathInfo,
14 probeCache,
15 ServerError,
16} from "./shared";
17import { parseFile } from "music-metadata";
18import tmp from "tmp";
19import { readFileSync, writeFileSync } from "node:fs";
20import MediaInfo from "mediainfo.js";
21
22export async function findAsync<T>(arr: Array<T>, asyncCallback: (value: T) => Promise<boolean>) {
23 const promises = arr.map(asyncCallback);
24 const results = await Promise.all(promises);
25 const index = results.findIndex((result) => result);
26 return arr[index];
27}
28
29type CoverResult = { info: PathInfo; path: string } | { info: PathInfo; content: Uint8Array } | { info: ServerError };
30
31const mediaInfo = await MediaInfo({
32 locateFile: () => `${args.serve}/MediaInfoModule.wasm`,
33});
34
35export async function findCover(targetPath: string): Promise<CoverResult> {
36 try {
37 if ((await stat(targetPath)).isFile()) {
38 //TODO
39 const parsed = await parseFile(targetPath, { duration: true, skipCovers: false });
40 if (!parsed.common.picture || parsed.common.picture.length === 0) return findCover(path.dirname(targetPath));
41
42 return { info: new PathInfo(parsed.common.picture[0].format), content: parsed.common.picture[0].data };
43 }
44 } catch (e) {
45 return { info: new ServerError(StatusMap["Internal Server Error"], `error getting cover ${e}`) };
46 }
47
48 if (!coverRegex) return { info: new ServerError(StatusMap["Not Found"], "Cover not found") };
49 let coverFile: string | undefined;
50 try {
51 coverFile = await findAsync(await readdir(targetPath), async (file) => {
52 return file.match(coverRegex) != null && (await stat(path.join(targetPath, file))).isFile();
53 });
54 const above = path.dirname(targetPath);
55 if (!coverFile && isBelow(musicRoot, above)) {
56 return findCover(above);
57 }
58 // biome-ignore lint/suspicious/noExplicitAny:
59 } catch (error: any) {
60 if (error?.code !== "ENOENT") {
61 console.error("error getting cover: readdirSync failed:", error);
62 return { info: new ServerError(StatusMap["Internal Server Error"], "error getting cover: readdirSync failed") };
63 }
64 return { info: new ServerError(StatusMap["Not Found"], "Directory not found") };
65 }
66 if (coverFile) {
67 const info = (await getPathInfo(path.join(targetPath, coverFile))) as PathInfo | ServerError;
68 if (info instanceof ServerError) {
69 return { info: info };
70 }
71 return { path: path.join(targetPath, coverFile), info };
72 }
73 return { info: new ServerError(StatusMap["Not Found"], "Cover not found") };
74}
75
76function decodeString(str?: string) {
77 if (!str) return undefined;
78 if (typeof str === "object" && "@dt" in str && "#value" in str && str["@dt"] === "binary.base64") {
79 return Buffer.from(str["#value"], "base64").toString("utf-8");
80 }
81 return str;
82 // return Buffer.from(str, "utf8").toString("utf-8");
83}
84
85export async function probeFile(filePath: string): Promise<Metadata> {
86 const cached = probeCache.get(filePath);
87 if (cached) return cached;
88
89 const parsed = await parseFile(filePath, { duration: true, skipCovers: false, mkvUseIndex: true });
90
91 const probeData = {
92 title: parsed.common.title,
93 artist: parsed.common.artist,
94 album: parsed.common.album,
95 genre: parsed.common.genre,
96 trackNumber: parsed.common.track.no || undefined,
97 };
98
99 const file = Bun.file(filePath);
100 const fileSize = (await file.stat()).size;
101 const chunkReader = async (size: number, offset: number) => {
102 return await file.slice(offset, offset + size).bytes();
103 };
104 const info = await mediaInfo.analyzeData(fileSize, chunkReader);
105 const generalTrack = info.media?.track.find((track) => track["@type"] === "General");
106 const videoTrack = info.media?.track.find((track) => track["@type"] === "Video");
107 const audioTrack = info.media?.track.find((track) => track["@type"] === "Audio");
108 const probeResult = {
109 ...probeData,
110 duration: generalTrack?.Duration || 0,
111 // title: decodeString(generalTrack?.Title),
112 // artist: generalTrack?.Album_Performer,
113 // album: generalTrack?.Album,
114 // genre: [generalTrack?.Genre || ""],
115 // trackNumber: generalTrack?.Track_Position,
116
117 container: generalTrack?.Format,
118 audioCodec: audioTrack?.Format,
119 audioBitrate: audioTrack?.BitRate,
120 videoCodec: videoTrack?.Format,
121 videoBitrate: videoTrack?.BitRate,
122 };
123
124 probeCache.set(filePath, probeResult);
125 return probeResult;
126}
127
128export async function getPathInfo(filePath: string): Promise<PathInfo | ServerError | undefined> {
129 try {
130 if (excludeExtension.includes(path.extname(filePath).slice(1))) return new ServerError(404, "Excluded extension");
131 const stats = await stat(filePath);
132 if (stats.isDirectory()) {
133 return undefined;
134 }
135 let mimeType = fileTypeCache.get(filePath);
136 if (!mimeType) {
137 const type = await fileTypeFromFile(filePath);
138 mimeType = type?.mime;
139 if (mimeType) {
140 fileTypeCache.set(filePath, mimeType);
141 } else {
142 console.error("Failed to determine MIME type for:", filePath);
143 return new ServerError(StatusMap["Internal Server Error"], "Filetype scan failed");
144 }
145 }
146 return new PathInfo(mimeType);
147 // biome-ignore lint/suspicious/noExplicitAny:
148 } catch (error: any) {
149 if (error?.code === "ENOENT") {
150 return new ServerError(StatusMap["Not Found"], "File not found");
151 }
152 console.error("Error scanning file:", filePath, error.message);
153 return new ServerError(StatusMap["Internal Server Error"], "Internal Server Error");
154 }
155}
156
157export function matchesType(mimeType: string, types: string[]): boolean {
158 return types.includes(mimeType.split("/", 1)[0]);
159}
160
161export function isBelow(basePath: string, targetPath: string) {
162 const relativePath = path.relative(basePath, targetPath);
163 return relativePath && !relativePath.startsWith("..");
164}
165
166export async function listFiles(subPath: string, recursive: boolean): Promise<FileListingWithStatus | ServerError> {
167 const files: FileListingWithStatus = new Map();
168
169 try {
170 for (const fileName of await readdir(subPath)) {
171 const itemPath = path.join(subPath, fileName);
172 if (!isBelow(musicRoot, itemPath)) {
173 console.log(`Skipping ${itemPath} as it is outside the base path.`);
174 continue;
175 }
176
177 const pathInfoResult = await getPathInfo(itemPath);
178 if (!pathInfoResult) {
179 if (recursive) {
180 const subListing = await listFiles(itemPath, true);
181 if (subListing instanceof ServerError) throw subListing;
182 files.set(fileName, { files: subListing, status: "Scanned" });
183 } else {
184 files.set(fileName, { files: new Map(), status: "Unknown" });
185 }
186 } else if (pathInfoResult instanceof PathInfo && matchesType(pathInfoResult.mimeType, mediaTypes)) {
187 try {
188 const probe = await probeFile(itemPath);
189 files.set(fileName, { metadata: probe });
190 } catch (probeError) {
191 console.error("probing error:", probeError);
192 }
193 }
194 }
195 return files;
196 // biome-ignore lint/suspicious/noExplicitAny: <explanation>
197 } catch (error: any) {
198 if (error?.code === "ENOENT") {
199 return new ServerError(StatusMap["Not Found"], "Directory not found");
200 }
201 console.error("Error listing path:", subPath, error.message);
202 return new ServerError(StatusMap["Internal Server Error"], "Internal Server Error");
203 }
204}
205
206export async function readStream(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
207 const reader = stream.getReader();
208 const chunks: Uint8Array[] = [];
209
210 try {
211 while (true) {
212 const { done, value } = await reader.read();
213 if (done) break;
214 if (value) chunks.push(value);
215 }
216
217 const combined = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0));
218 let offset = 0;
219 for (const chunk of chunks) {
220 combined.set(chunk, offset);
221 offset += chunk.length;
222 }
223
224 return combined;
225 } finally {
226 reader.releaseLock();
227 }
228}
229
230export function packWithTar(files: string[]) {
231 // Ensure all files are within musicRoot
232 const absoluteFiles = files.map((f) => path.join(musicRoot, f));
233 for (const f of absoluteFiles) {
234 if (!isBelow(musicRoot, f)) {
235 throw new ServerError(StatusMap.Forbidden, "One or more files are outside the music root");
236 }
237 }
238
239 const relFiles = absoluteFiles.map((f) => path.relative(musicRoot, f));
240 const tarArgs = [
241 "--transform=s|.*/||", //only supported in gnu tar TODO: add fallback
242 "-C",
243 musicRoot,
244 "-c", // create archive
245 ...relFiles,
246 ];
247 return Bun.spawn(["tar", ...tarArgs], {
248 stdin: "ignore",
249 stdout: "pipe",
250 stderr: "pipe",
251 onExit: async (subprocess: Bun.Subprocess<"ignore", "pipe", "pipe">, exitCode: number | null) => {
252 if (exitCode !== 0 && exitCode !== null) {
253 console.error("tar failed with status code", exitCode);
254 console.error(new TextDecoder().decode(await readStream(subprocess.stderr as ReadableStream)));
255 }
256 },
257 });
258}
259
260//copy to a temp file and then use avifenc to convert
261export async function toAvif(
262 data: Uint8Array,
263 mimeType: string,
264 setContentType: (mimeType: string) => void,
265): Promise<Uint8Array> {
266 const tmpFile = tmp.fileSync({
267 postfix: mimeType.includes("image/jpeg") ? ".jpg" : mimeType.includes("image/jpeg") ? ".png" : undefined,
268 });
269 const tmpFile2 = tmp.fileSync({ postfix: ".avif" });
270 try {
271 writeFileSync(tmpFile.fd, data);
272 const cmd = Bun.spawn(["avifenc", "-q", "50", tmpFile.name, tmpFile2.name], {
273 stdin: "ignore",
274 stdout: "pipe",
275 stderr: "pipe",
276 onExit: async (subprocess: Bun.Subprocess<"ignore", "pipe", "pipe">, exitCode: number | null) => {
277 if (exitCode !== 0 && exitCode !== null) {
278 console.error("avifenc failed with status code", exitCode);
279 console.error(new TextDecoder().decode(await readStream(subprocess.stderr as ReadableStream)));
280 }
281 },
282 });
283 await cmd.exited;
284 setContentType("image/avif");
285 return readFileSync(tmpFile2.fd);
286 } catch (e) {
287 setContentType(mimeType);
288 return data;
289 } finally {
290 tmpFile.removeCallback();
291 tmpFile2.removeCallback();
292 }
293}
294