invidious: move searching to backend

This commit is contained in:
2025-02-22 01:09:33 -08:00
parent cfe48e28f8
commit 880057b0fc
6 changed files with 265 additions and 38 deletions

View File

@@ -1,6 +1,8 @@
import express from "express";
import expressWs from "express-ws";
import { MediaPlayer } from "./MediaPlayer";
import { searchInvidious, getInvidiousThumbnailURL } from "./InvidiousAPI";
import fetch from "node-fetch";
const app = express();
app.use(express.json());
@@ -95,6 +97,44 @@ apiRouter.ws("/events", (ws, req) => {
});
});
apiRouter.get("/search", withErrorHandling(async (req, res) => {
const query = req.query.q as string;
if (!query) {
res.status(400)
.send(JSON.stringify({ success: false, error: "Query parameter 'q' is required" }));
return;
}
const results = await searchInvidious(query);
res.send(JSON.stringify({ success: true, results }));
}));
apiRouter.get("/thumbnail", withErrorHandling(async (req, res) => {
const thumbnailUrl = req.query.url as string;
if (!thumbnailUrl) {
res.status(400)
.send(JSON.stringify({ success: false, error: "URL parameter is required" }));
return;
}
try {
const response = await fetch(getInvidiousThumbnailURL(thumbnailUrl));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Forward the content type header
res.set('Content-Type', response.headers.get('content-type') || 'image/jpeg');
// Pipe the thumbnail data directly to the response
response.body.pipe(res);
} catch (error) {
console.error('Failed to proxy thumbnail:', error);
res.status(500)
.send(JSON.stringify({ success: false, error: 'Failed to fetch thumbnail' }));
}
}));
// Serve static files for React app (after building)
app.use(express.static("dist/frontend"));