const express = require("express"); const fs = require("fs"); const path = require("path"); const jwt = require("jsonwebtoken"); const mongoose = require("mongoose"); const Movie = require("../models/Movie"); const router = express.Router(); function parseCookies(header) { const cookies = {}; const raw = String(header || ""); if (!raw) return cookies; raw.split(";").forEach((part) => { const idx = part.indexOf("="); if (idx <= 0) return; const k = part.slice(0, idx).trim(); const v = part.slice(idx + 1).trim(); if (!k) return; cookies[k] = decodeURIComponent(v); }); return cookies; } function isSafeRelativeFile(p) { if (typeof p !== "string") return false; const s = p.trim(); if (!s) return false; if (s.includes("\0")) return false; // No absolute paths if (path.isAbsolute(s)) return false; // Normalize and prevent traversal const norm = path.posix.normalize(s.replaceAll("\\", "/")); if (norm.startsWith("../") || norm === "..") return false; return true; } function guessContentType(filePath) { const ext = String(path.extname(filePath) || "").toLowerCase(); if (ext === ".mp4") return "video/mp4"; if (ext === ".webm") return "video/webm"; if (ext === ".ogv" || ext === ".ogg") return "video/ogg"; if (ext === ".m4v") return "video/x-m4v"; if (ext === ".mov") return "video/quicktime"; if (ext === ".m3u8") return "application/vnd.apple.mpegurl"; if (ext === ".ts") return "video/mp2t"; return "application/octet-stream"; } function getVideoBaseDir() { // Default to your existing public/video folder. const fromEnv = typeof process.env.VIDEO_FILES_DIR === "string" ? process.env.VIDEO_FILES_DIR.trim() : ""; const base = fromEnv || path.join(process.cwd(), "public", "video"); return path.resolve(base); } function getAuthToken(req) { //