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 for (const track of info.media?.track || []) {
106 console.log(track);
107 }
108 const generalTrack = info.media?.track.find((track) => track["@type"] === "General");
109 const videoTrack = info.media?.track.find((track) => track["@type"] === "Video");
110 const audioTrack = info.media?.track.find((track) => track["@type"] === "Audio");
111 const probeResult = {
112 ...probeData,
113 duration: generalTrack?.Duration || 0,
114 // title: decodeString(generalTrack?.Title),
115 // artist: generalTrack?.Album_Performer,
116 // album: generalTrack?.Album,
117 // genre: [generalTrack?.Genre || ""],
118 // trackNumber: generalTrack?.Track_Position,
119
120 container: generalTrack?.Format,
121 audioCodec: audioTrack?.Format,
122 audioBitrate: audioTrack?.BitRate,
123 videoCodec: videoTrack?.Format,
124 videoBitrate: videoTrack?.BitRate,
125 };
126
127 probeCache.set(filePath, probeResult);
128 return probeResult;
129}
130
131export async function getPathInfo(filePath: string): Promise<PathInfo | ServerError | undefined> {
132 try {
133 if (excludeExtension.includes(path.extname(filePath).slice(1))) return new ServerError(404, "Excluded extension");
134 const stats = await stat(filePath);
135 if (stats.isDirectory()) {
136 return undefined;
137 }
138 let mimeType = fileTypeCache.get(filePath);
139 if (!mimeType) {
140 const type = await fileTypeFromFile(filePath);
141 mimeType = type?.mime;
142 if (mimeType) {
143 fileTypeCache.set(filePath, mimeType);
144 } else {
145 console.error("Failed to determine MIME type for:", filePath);
146 return new ServerError(StatusMap["Internal Server Error"], "Filetype scan failed");
147 }
148 }
149 return new PathInfo(mimeType);
150 // biome-ignore lint/suspicious/noExplicitAny:
151 } catch (error: any) {
152 if (error?.code === "ENOENT") {
153 return new ServerError(StatusMap["Not Found"], "File not found");
154 }
155 console.error("Error scanning file:", filePath, error.message);
156 return new ServerError(StatusMap["Internal Server Error"], "Internal Server Error");
157 }
158}
159
160export function matchesType(mimeType: string, types: string[]): boolean {
161 return types.includes(mimeType.split("/", 1)[0]);
162}
163
164export function isBelow(basePath: string, targetPath: string) {
165 const relativePath = path.relative(basePath, targetPath);
166 return relativePath && !relativePath.startsWith("..");
167}
168
169export async function listFiles(subPath: string, recursive: boolean): Promise<FileListingWithStatus | ServerError> {
170 const files: FileListingWithStatus = new Map();
171
172 try {
173 for (const fileName of await readdir(subPath)) {
174 const itemPath = path.join(subPath, fileName);
175 if (!isBelow(musicRoot, itemPath)) {
176 console.log(`Skipping ${itemPath} as it is outside the base path.`);
177 continue;
178 }
179
180 const pathInfoResult = await getPathInfo(itemPath);
181 if (!pathInfoResult) {
182 if (recursive) {
183 const subListing = await listFiles(itemPath, true);
184 if (subListing instanceof ServerError) throw subListing;
185 files.set(fileName, { files: subListing, status: "Scanned" });
186 } else {
187 files.set(fileName, { files: new Map(), status: "Unknown" });
188 }
189 } else if (pathInfoResult instanceof PathInfo && matchesType(pathInfoResult.mimeType, mediaTypes)) {
190 try {
191 const probe = await probeFile(itemPath);
192 files.set(fileName, { metadata: probe });
193 } catch (probeError) {
194 console.error("probing error:", probeError);
195 }
196 }
197 }
198 return files;
199 // biome-ignore lint/suspicious/noExplicitAny: <explanation>
200 } catch (error: any) {
201 if (error?.code === "ENOENT") {
202 return new ServerError(StatusMap["Not Found"], "Directory not found");
203 }
204 console.error("Error listing path:", subPath, error.message);
205 return new ServerError(StatusMap["Internal Server Error"], "Internal Server Error");
206 }
207}
208
209export async function readStream(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
210 const reader = stream.getReader();
211 const chunks: Uint8Array[] = [];
212
213 try {
214 while (true) {
215 const { done, value } = await reader.read();
216 if (done) break;
217 if (value) chunks.push(value);
218 }
219
220 const combined = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0));
221 let offset = 0;
222 for (const chunk of chunks) {
223 combined.set(chunk, offset);
224 offset += chunk.length;
225 }
226
227 return combined;
228 } finally {
229 reader.releaseLock();
230 }
231}
232
233export function packWithTar(files: string[]) {
234 // Ensure all files are within musicRoot
235 const absoluteFiles = files.map((f) => path.join(musicRoot, f));
236 for (const f of absoluteFiles) {
237 if (!isBelow(musicRoot, f)) {
238 throw new ServerError(StatusMap.Forbidden, "One or more files are outside the music root");
239 }
240 }
241
242 const relFiles = absoluteFiles.map((f) => path.relative(musicRoot, f));
243 const tarArgs = [
244 "--transform=s|.*/||", //only supported in gnu tar TODO: add fallback
245 "-C",
246 musicRoot,
247 "-c", // create archive
248 ...relFiles,
249 ];
250 return Bun.spawn(["tar", ...tarArgs], {
251 stdin: "ignore",
252 stdout: "pipe",
253 stderr: "pipe",
254 onExit: async (subprocess: Bun.Subprocess<"ignore", "pipe", "pipe">, exitCode: number | null) => {
255 if (exitCode !== 0 && exitCode !== null) {
256 console.error("tar failed with status code", exitCode);
257 console.error(new TextDecoder().decode(await readStream(subprocess.stderr as ReadableStream)));
258 }
259 },
260 });
261}
262
263//copy to a temp file and then use avifenc to convert
264export async function toAvif(
265 data: Uint8Array,
266 mimeType: string,
267 setContentType: (mimeType: string) => void,
268): Promise<Uint8Array> {
269 const tmpFile = tmp.fileSync({
270 postfix: mimeType.includes("image/jpeg") ? ".jpg" : mimeType.includes("image/jpeg") ? ".png" : undefined,
271 });
272 const tmpFile2 = tmp.fileSync({ postfix: ".avif" });
273 try {
274 writeFileSync(tmpFile.fd, data);
275 const cmd = Bun.spawn(["avifenc", "-q", "50", tmpFile.name, tmpFile2.name], {
276 stdin: "ignore",
277 stdout: "pipe",
278 stderr: "pipe",
279 onExit: async (subprocess: Bun.Subprocess<"ignore", "pipe", "pipe">, exitCode: number | null) => {
280 if (exitCode !== 0 && exitCode !== null) {
281 console.error("avifenc failed with status code", exitCode);
282 console.error(new TextDecoder().decode(await readStream(subprocess.stderr as ReadableStream)));
283 }
284 },
285 });
286 await cmd.exited;
287 setContentType("image/avif");
288 return readFileSync(tmpFile2.fd);
289 } catch (e) {
290 setContentType(mimeType);
291 return data;
292 } finally {
293 tmpFile.removeCallback();
294 tmpFile2.removeCallback();
295 }
296}
297