mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
fix for safari browser handling media files
This commit is contained in:
@@ -26,13 +26,49 @@ export async function GET(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const buffer = await fsp.readFile(filePath)
|
const stat = await fsp.stat(filePath)
|
||||||
|
const fileSize = stat.size
|
||||||
const mime = mimeForStoredFile(safeName)
|
const mime = mimeForStoredFile(safeName)
|
||||||
|
const range = _request.headers.get("range")
|
||||||
|
|
||||||
|
// Basic range support improves Safari/iOS playback
|
||||||
|
if (range) {
|
||||||
|
const bytesPrefix = "bytes="
|
||||||
|
if (!range.startsWith(bytesPrefix)) {
|
||||||
|
return new NextResponse(null, { status: 416 })
|
||||||
|
}
|
||||||
|
|
||||||
|
const [rawStart, rawEnd] = range.replace(bytesPrefix, "").split("-")
|
||||||
|
const start = Number(rawStart)
|
||||||
|
const end = rawEnd ? Number(rawEnd) : fileSize - 1
|
||||||
|
|
||||||
|
if (Number.isNaN(start) || Number.isNaN(end) || start > end) {
|
||||||
|
return new NextResponse(null, { status: 416 })
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunkSize = end - start + 1
|
||||||
|
const stream = fs.createReadStream(filePath, { start, end })
|
||||||
|
|
||||||
|
return new NextResponse(stream as any, {
|
||||||
|
status: 206,
|
||||||
|
headers: {
|
||||||
|
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
|
||||||
|
"Accept-Ranges": "bytes",
|
||||||
|
"Content-Length": chunkSize.toString(),
|
||||||
|
"Content-Type": mime,
|
||||||
|
"Cache-Control": "public, max-age=31536000, immutable",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await fsp.readFile(filePath)
|
||||||
|
|
||||||
return new NextResponse(buffer, {
|
return new NextResponse(buffer, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": mime,
|
"Content-Type": mime,
|
||||||
|
"Content-Length": fileSize.toString(),
|
||||||
|
"Accept-Ranges": "bytes",
|
||||||
"Cache-Control": "public, max-age=31536000, immutable",
|
"Cache-Control": "public, max-age=31536000, immutable",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user