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