feat: resolve media music sources

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-05-15 17:05:37 +07:00
parent 2194d4a8b6
commit 93134a9793
2 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
import { existsSync, statSync } from "node:fs";
import path from "node:path";
import { AppError } from "../errors";
import type { ResolvedMediaSource } from "./mediaTypes";
export async function resolveMediaSource(
input: string,
): Promise<ResolvedMediaSource> {
const source = input.trim();
if (!source) {
throw new AppError("Media source is required", "MISSING_MEDIA_SOURCE", 400);
}
if (source.startsWith("http://") || source.startsWith("https://")) {
return {
source,
title: titleFromUrl(source),
kind: "url",
};
}
if (existsSync(source) && statSync(source).isFile()) {
return {
source,
title: path.basename(source),
kind: "local",
};
}
throw new AppError(
"Media source must be an HTTP(S) URL or existing local file",
"UNSUPPORTED_MEDIA_SOURCE",
400,
);
}
function titleFromUrl(source: string): string {
const url = new URL(source);
const filename = decodeURIComponent(url.pathname.split("/").pop() || "");
return filename || url.hostname;
}