import type { Express } from "express"; import { createServer, type Server } from "http"; import { storage } from "./storage"; import { z } from "zod"; import { BunnyService } from "./bunny"; export async function registerRoutes(app: Express): Promise { // Get videos with pagination and filtering app.get("/api/videos", async (req, res) => { try { const limit = parseInt(req.query.limit as string) || 20; const offset = parseInt(req.query.offset as string) || 0; const search = req.query.search as string; const category = req.query.category as string; const videos = await storage.getVideos(limit, offset, search, category); const total = await storage.getVideoCount(search, category); res.json({ videos, total, hasMore: offset + limit < total }); } catch (error) { res.status(500).json({ message: "Failed to fetch videos" }); } }); // Get single video by ID app.get("/api/videos/:id", async (req, res) => { try { const video = await storage.getVideo(req.params.id); if (!video) { return res.status(404).json({ message: "Video not found" }); } res.json(video); } catch (error) { res.status(500).json({ message: "Failed to fetch video" }); } }); // Update video views app.post("/api/videos/:id/view", async (req, res) => { try { await storage.updateVideoViews(req.params.id); res.json({ success: true }); } catch (error) { res.status(500).json({ message: "Failed to update views" }); } }); // Get video categories app.get("/api/categories", async (req, res) => { try { const videos = await storage.getVideos(1000); const categories = Array.from(new Set(videos.map(v => v.category).filter(Boolean))); res.json(categories); } catch (error) { console.error("Error fetching categories:", error); res.status(500).json({ message: "Failed to fetch categories" }); } }); // Serve video page with meta tags for social sharing app.get("/video/:id", async (req, res) => { try { const video = await storage.getVideo(req.params.id); if (!video) { return res.redirect("/"); } const shareUrl = `${req.protocol}://${req.get('host')}/video/${video.id}`; const html = ` ${video.title} - VideoStream
`; res.send(html); } catch (error) { console.error("Error serving video page:", error); res.redirect("/"); } }); // Public thumbnail endpoint that generates SVG thumbnails app.get("/thumbnail/:videoId", async (req, res) => { try { const { videoId } = req.params; // Get video info for generating proper thumbnail const video = await storage.getVideo(videoId); let title = "Video"; let duration = "0:00"; if (video) { title = video.title.replace('.mp4', '').substring(0, 35); const minutes = Math.floor(video.duration / 60); const seconds = video.duration % 60; duration = `${minutes}:${seconds.toString().padStart(2, '0')}`; } // Generate SVG thumbnail with video title and duration const svg = ` ${title.split(' ').map((word, i) => `${word}`).join('')} ${duration} `; res.setHeader('Content-Type', 'image/svg+xml'); res.setHeader('Cache-Control', 'public, max-age=86400'); res.send(svg); } catch (error) { console.error("Error generating thumbnail:", error); // Fallback SVG const fallbackSvg = ` Video Thumbnail `; res.setHeader('Content-Type', 'image/svg+xml'); res.send(fallbackSvg); } }); const httpServer = createServer(app); return httpServer; }