feat: resolve media music sources
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
41
src/media/mediaResolver.ts
Normal file
41
src/media/mediaResolver.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user