adds skip, previous, current url

This commit is contained in:
2025-02-15 02:37:17 -08:00
parent b7ad824eb5
commit 4f94ca0b52
3 changed files with 78 additions and 53 deletions

View File

@@ -2,62 +2,63 @@ import express from "express";
import expressWs from "express-ws";
import { MediaPlayer } from "./MediaPlayer";
interface PlaylistAppendRequest {
url: string;
}
const app = express();
expressWs(app);
app.use(express.json());
expressWs(app);
const apiRouter = express.Router();
const mediaPlayer = new MediaPlayer();
apiRouter.get("/playlist", async (req, res) => {
const withErrorHandling = (func: (req: any, res: any) => Promise<any>) => {
return async (req: any, res: any) => {
try {
await func(req, res);
} catch (error: any) {
res.status(500).send(JSON.stringify({ success: false, error: error.message }));
}
};
};
apiRouter.get("/playlist", withErrorHandling(async (req, res) => {
const playlist = await mediaPlayer.getPlaylist();
res.send(playlist);
});
}));
apiRouter.post("/playlist", async (req, res) => {
try {
const { url } = req.body as PlaylistAppendRequest;
await mediaPlayer.append(url);
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
apiRouter.post("/playlist", withErrorHandling(async (req, res) => {
const { url } = req.body as { url: string };
await mediaPlayer.append(url);
res.send(JSON.stringify({ success: true }));
}));
apiRouter.delete("/playlist/:index", async (req, res) => {
apiRouter.delete("/playlist/:index", withErrorHandling(async (req, res) => {
const { index } = req.params as { index: string };
await mediaPlayer.deletePlaylistItem(parseInt(index));
res.send(JSON.stringify({ success: true }));
});
}));
apiRouter.post("/play", async (req, res) => {
try {
await mediaPlayer.play();
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
apiRouter.post("/play", withErrorHandling(async (req, res) => {
await mediaPlayer.play();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.post("/pause", async (req, res) => {
try {
await mediaPlayer.pause();
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
apiRouter.post("/pause", withErrorHandling(async (req, res) => {
await mediaPlayer.pause();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.get("/nowplaying", async (req, res) => {
apiRouter.post("/skip", withErrorHandling(async (req, res) => {
await mediaPlayer.skip();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.post("/previous", withErrorHandling(async (req, res) => {
await mediaPlayer.previous();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.get("/nowplaying", withErrorHandling(async (req, res) => {
const nowPlaying = await mediaPlayer.getNowPlaying();
const currentFile = await mediaPlayer.getCurrentFile();
const pauseState = await mediaPlayer.getPauseState();
const volume = await mediaPlayer.getVolume();
const idle = await mediaPlayer.getIdle();
@@ -67,15 +68,16 @@ apiRouter.get("/nowplaying", async (req, res) => {
nowPlaying: nowPlaying,
isPaused: pauseState,
volume: volume,
isIdle: idle
isIdle: idle,
currentFile: currentFile
}));
});
}));
apiRouter.post("/volume", async (req, res) => {
apiRouter.post("/volume", withErrorHandling(async (req, res) => {
const { volume } = req.body as { volume: number };
await mediaPlayer.setVolume(volume);
res.send(JSON.stringify({ success: true }));
});
}));
apiRouter.ws("/events", (ws, req) => {
console.log("Events client connected");