add request logging, always transcode media

AuthorKonata <konata@posteo.jp>
Date
Commit6aaf24f4b9057a020eb077b43dcf6e4596921908
Parent5d984dd
2 files changed, 11 insertions(+), 20 deletions(-)
▾Mserver/src/index.ts
@@ -3,16 +3,7 @@ import { Elysia, ElysiaFile, type InferHandler, t } from "elysia";
import path from "node:path";
import { parseArgs } from "node:util";
import { musicRoot, fileTypeCache, ServerError, allowedTypes, probeCache, mediaTypes } from "./shared";
import {
getPathInfo,
probeFile,
listFiles,
findCover,
matchesType,
needsTranscoding,
convertWithFFmpeg,
packWithTar,
} from "./utils";
import { getPathInfo, listFiles, findCover, matchesType, convertWithFFmpeg, packWithTar, probeFile } from "./utils";
import { stat } from "node:fs/promises";
import parseRange from "range-parser";
@@ -94,6 +85,9 @@ const downloadHandler: DownloadHandler = async ({ headers, params, set, request
const app = setup
.use(staticPlugin({ assets: values.serve, prefix: "/" }))
.onBeforeHandle(({ request, path }) => {
console.info(request.method, path);
})
.post("/reset-cache", () => {
fileTypeCache.clear();
probeCache.clear();
@@ -113,11 +107,13 @@ const app = setup
set.status = "Forbidden";
return "Forbidden file type";
}
const bitrate = query.bitrate || 96000;
if (!matchesType(fileScan.mimeType, mediaTypes) || !needsTranscoding(await probeFile(filePath), bitrate)) {
const probe = await probeFile(filePath);
//don't use higher bitrate than what the file has, use requested bitrate if unknown, default to 96kbit if not provided
const bitrate = query.bitrate && probe.bitrate ? Math.min(query.bitrate, probe.bitrate) : query.bitrate || 96000;
if (!matchesType(fileScan.mimeType, mediaTypes)) {
set.status = "Temporary Redirect";
set.headers.Location = `/download/${params["*"]}`;
return "No transcoding necessary or probe failed, redirecting to normal endpoint";
return "Not a media file, redirecting to normal endpoint";
}
const { cmd, mimeType } = convertWithFFmpeg(filePath, bitrate, query.format ?? MediaFormat["opus/ogg"]);
▾Mserver/src/utils.ts
@@ -45,12 +45,12 @@ export async function findCover(dirPath: string): Promise<CoverResult> {
export type FfprobeData = {
duration: number;
bitrate: number;
bitrate?: number;
};
export async function probeFile(filePath: string): Promise<FfprobeData> {
const parsed = await parseFile(filePath, { duration: true, skipCovers: true });
return { duration: parsed.format.duration as number, bitrate: parsed.format.bitrate as number };
return { duration: parsed.format.duration as number, bitrate: parsed.format.bitrate };
}
export async function getPathInfo(filePath: string): Promise<PathInfo | ServerError | undefined> {
@@ -85,11 +85,6 @@ export function matchesType(mimeType: string, types: string[]): boolean {
return types.includes(mimeType.split("/", 1)[0]);
}
export function needsTranscoding(probe: FfprobeData, requestBitrate?: unknown) {
const bitrate = Number(requestBitrate);
return probe.bitrate && bitrate > 0 && bitrate < probe.bitrate;
}
export function isBelow(basePath: string, targetPath: string) {
const relativePath = path.relative(basePath, targetPath);
return relativePath && !relativePath.startsWith("..");