nextav/src/app/api/stream/[id]/route.ts

113 lines
3.7 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getDatabase } from "@/db";
import fs from "fs";
import path from "path";
export async function OPTIONS(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
return new Response(null, {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Range',
'Access-Control-Max-Age': '86400',
},
});
}
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const db = getDatabase();
const searchParams = request.nextUrl.searchParams;
const forceTranscode = searchParams.get('transcode') === 'true';
try {
const videoId = parseInt(id);
const video = db.prepare("SELECT * FROM media WHERE id = ? AND type = 'video'").get(videoId) as { path: string, codec_info: string } | undefined;
if (!video) {
return NextResponse.json({ error: "Video not found" }, { status: 404 });
}
// Parse codec info to determine if transcoding is needed
let codecInfo = { needsTranscoding: false };
try {
codecInfo = JSON.parse(video.codec_info || '{}');
} catch {
// Fallback if codec info is invalid
}
const needsTranscoding = forceTranscode || codecInfo.needsTranscoding || false;
console.log(`[STREAM] Video ID: ${id}, Path: ${video.path}, Force Transcode: ${forceTranscode}, Needs Transcode: ${codecInfo.needsTranscoding}, Final Decision: ${needsTranscoding}`);
if (needsTranscoding) {
console.log(`[STREAM] Redirecting to transcoding endpoint: /api/stream/${id}/transcode`);
// Return CORS-enabled redirect
const response = NextResponse.redirect(
new URL(`/api/stream/${id}/transcode`, request.url),
302
);
response.headers.set('Access-Control-Allow-Origin', '*');
response.headers.set('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
return response;
}
const videoPath = video.path;
if (!fs.existsSync(videoPath)) {
return NextResponse.json({ error: "Video file not found" }, { status: 404 });
}
const stat = fs.statSync(videoPath);
const fileSize = stat.size;
const range = request.headers.get("range");
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = end - start + 1;
const file = fs.createReadStream(videoPath, { start, end });
const headers = new Headers({
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunksize.toString(),
"Content-Type": "video/mp4",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"Access-Control-Allow-Headers": "Range, Content-Type",
});
return new Response(file as any, {
status: 206,
headers,
});
} else {
const headers = new Headers({
"Content-Length": fileSize.toString(),
"Content-Type": "video/mp4",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"Access-Control-Allow-Headers": "Range, Content-Type",
});
const file = fs.createReadStream(videoPath);
return new Response(file as any, {
status: 200,
headers,
});
}
} catch (error) {
console.error("Error streaming video:", error);
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}