2026-05-15 17:43:53 +07:00
|
|
|
import type { Router } from "express";
|
|
|
|
|
import express from "express";
|
|
|
|
|
import { AppError } from "../errors";
|
|
|
|
|
import type { MediaController } from "../media/mediaController";
|
2026-05-16 15:48:28 +07:00
|
|
|
import type { MediaMode } from "../media/mediaTypes";
|
2026-05-15 17:43:53 +07:00
|
|
|
|
|
|
|
|
export type MediaRouteController = Pick<
|
|
|
|
|
MediaController,
|
|
|
|
|
"getState" | "queue" | "skip" | "stop"
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
export function createMediaRoutes(controller: MediaRouteController): Router {
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
router.get("/media/status", (_req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
res.json(controller.getState());
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/media/queue", async (req, res, next) => {
|
|
|
|
|
try {
|
2026-05-16 15:48:28 +07:00
|
|
|
const { source, mode = "music" } = req.body as {
|
|
|
|
|
source?: string;
|
|
|
|
|
mode?: MediaMode;
|
|
|
|
|
};
|
2026-05-15 17:43:53 +07:00
|
|
|
if (!source) {
|
2026-05-15 18:04:39 +07:00
|
|
|
throw new AppError(
|
|
|
|
|
"Media source is required",
|
|
|
|
|
"MISSING_MEDIA_SOURCE",
|
|
|
|
|
400,
|
|
|
|
|
);
|
2026-05-15 17:43:53 +07:00
|
|
|
}
|
2026-05-16 15:48:28 +07:00
|
|
|
if (mode !== "music" && mode !== "screen") {
|
|
|
|
|
throw new AppError("Invalid media mode", "INVALID_MEDIA_MODE", 400);
|
|
|
|
|
}
|
|
|
|
|
res.json(await controller.queue(source, { mode }));
|
2026-05-15 17:43:53 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/media/skip", async (_req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
res.json(await controller.skip());
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/media/stop", async (_req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
res.json(await controller.stop());
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return router;
|
2026-05-15 18:04:39 +07:00
|
|
|
}
|