fix: media playback uses dedicated stream endpoint with correct MIME types
- New ?action=stream endpoint with proper MIME-Type detection - Uses PHP finfo for accurate detection, extension fallback - Supports HTTP Range requests for seeking in video/audio - Media overlay now uses /stream instead of /download - Downloads still use octet-stream (attachment mode) - Added Accept-Ranges and Cache-Control headers
This commit is contained in:
@@ -150,7 +150,112 @@ function handleDownload(): void
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── API Router ─────────────────────────────────────────────────────────────
|
||||
// ── MIME-Type Mapping ────────────────────────────────────────────
|
||||
|
||||
function getMimeType(string $filePath): string
|
||||
{
|
||||
// Try finfo first (most accurate)
|
||||
if (function_exists('finfo_open')) {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mime = finfo_file($finfo, $filePath);
|
||||
finfo_close($finfo);
|
||||
if ($mime && $mime !== 'application/octet-stream') {
|
||||
return $mime;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: extension-based mapping
|
||||
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
||||
$mimes = [
|
||||
// Images
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
'svg' => 'image/svg+xml',
|
||||
'bmp' => 'image/bmp',
|
||||
'ico' => 'image/x-icon',
|
||||
'tiff' => 'image/tiff',
|
||||
'tif' => 'image/tiff',
|
||||
'avif' => 'image/avif',
|
||||
// Audio
|
||||
'mp3' => 'audio/mpeg',
|
||||
'ogg' => 'audio/ogg',
|
||||
'wav' => 'audio/wav',
|
||||
'flac' => 'audio/flac',
|
||||
'aac' => 'audio/aac',
|
||||
'wma' => 'audio/x-ms-wma',
|
||||
'opus' => 'audio/opus',
|
||||
'm4a' => 'audio/mp4',
|
||||
// Video
|
||||
'mp4' => 'video/mp4',
|
||||
'webm' => 'video/webm',
|
||||
'ogg' => 'video/ogg',
|
||||
'mov' => 'video/quicktime',
|
||||
'avi' => 'video/x-msvideo',
|
||||
'mkv' => 'video/x-matroska',
|
||||
'm4v' => 'video/x-m4v',
|
||||
];
|
||||
return $mimes[$ext] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
// ── API: File Stream (for media playback) ──────────────────────────
|
||||
|
||||
function handleStream(): void
|
||||
{
|
||||
$rawFile = $_GET['file'] ?? '';
|
||||
$filePath = ENTRY_POINT . '/' . urldecode($rawFile);
|
||||
|
||||
if (!is_file($filePath)) {
|
||||
http_response_code(404);
|
||||
echo 'File not found';
|
||||
return;
|
||||
}
|
||||
|
||||
$mime = getMimeType($filePath);
|
||||
$size = filesize($filePath);
|
||||
|
||||
// Handle Range requests (seeking support)
|
||||
$rangeHeader = $_SERVER['HTTP_RANGE'] ?? '';
|
||||
if (!empty($rangeHeader)) {
|
||||
// Parse Range header (format: bytes=start-end)
|
||||
if (preg_match('/bytes=(\d+)-(\d+)?/', $rangeHeader, $matches)) {
|
||||
$start = (int)$matches[1];
|
||||
$end = isset($matches[2]) && $matches[2] !== '' ? (int)$matches[2] : $size - 1;
|
||||
$length = $end - $start + 1;
|
||||
|
||||
header('HTTP/1.1 206 Partial Content');
|
||||
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
|
||||
header('Accept-Ranges: bytes');
|
||||
header('Content-Length: ' . $length);
|
||||
header('Content-Type: ' . $mime);
|
||||
header('Cache-Control: public, max-age=3600');
|
||||
|
||||
$fp = fopen($filePath, 'rb');
|
||||
fseek($fp, $start);
|
||||
$remaining = $length;
|
||||
while ($remaining > 0 && !feof($fp)) {
|
||||
$chunk = min(8192, $remaining);
|
||||
echo fread($fp, $chunk);
|
||||
$remaining -= $chunk;
|
||||
}
|
||||
fclose($fp);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Full file response
|
||||
header('HTTP/1.1 200 OK');
|
||||
header('Content-Type: ' . $mime);
|
||||
header('Content-Length: ' . $size);
|
||||
header('Accept-Ranges: bytes');
|
||||
header('Cache-Control: public, max-age=3600');
|
||||
readfile($filePath);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── API Router ─────────────────────────────────────────────────────
|
||||
|
||||
if (isset($_GET['action'])) {
|
||||
header('Content-Type: application/json');
|
||||
@@ -165,6 +270,11 @@ if (isset($_GET['action'])) {
|
||||
handleDownload();
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_GET['action'] === 'stream') {
|
||||
handleStream();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// ── HTML Output ────────────────────────────────────────────────────────────
|
||||
@@ -1088,7 +1198,7 @@ if (isset($_GET['action'])) {
|
||||
|
||||
// ── Media Overlay ──────────────────────────────────────────────
|
||||
function showMediaOverlay(filename, type) {
|
||||
const src = `index.php?action=download&file=${encodeURIComponent(currentPath ? currentPath + '/' + filename : filename)}`;
|
||||
const src = `index.php?action=stream&file=${encodeURIComponent(currentPath ? currentPath + '/' + filename : filename)}`;
|
||||
|
||||
// Remove existing content (except close button)
|
||||
const existingMedia = overlayContent.querySelector('img, audio, video');
|
||||
|
||||
Reference in New Issue
Block a user