adds MEDIA_ROOT environment variable for starting the server
This commit is contained in:
parent
0783e08fe9
commit
5f7b1aa7c1
3 changed files with 76 additions and 64 deletions
|
|
@ -3,18 +3,29 @@
|
|||
* serve_media.php - Serves media files with proper headers for streaming + Range
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/auth.php';
|
||||
require_auth(false); // plain text is fine for media endpoint
|
||||
set_time_limit(0);
|
||||
|
||||
// ---- Config ----
|
||||
$mediaRoot = getenv('MEDIA_ROOT');
|
||||
if ($mediaRoot === false || trim($mediaRoot) === '') {
|
||||
http_response_code(500);
|
||||
echo 'MEDIA_ROOT is not set';
|
||||
exit;
|
||||
}
|
||||
|
||||
// ---- Config (prefer env vars in production) ----
|
||||
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: ('/home/' . get_current_user()));
|
||||
$baseDir = rtrim($homeDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'GDrive';
|
||||
$baseDir = realpath(trim($mediaRoot));
|
||||
if ($baseDir === false || !is_dir($baseDir)) {
|
||||
http_response_code(500);
|
||||
echo 'MEDIA_ROOT base directory not found';
|
||||
exit;
|
||||
}
|
||||
|
||||
// ---- Input ----
|
||||
$requested = $_GET['file'] ?? '';
|
||||
if ($requested === '') {
|
||||
if (!is_string($requested) || $requested === '') {
|
||||
http_response_code(400);
|
||||
echo 'No file specified';
|
||||
exit;
|
||||
|
|
@ -24,16 +35,15 @@ if ($requested === '') {
|
|||
$requested = str_replace(['\\'], '/', $requested);
|
||||
|
||||
// Compute real paths and enforce base directory containment
|
||||
$realBase = realpath($baseDir);
|
||||
$realFile = realpath($baseDir . DIRECTORY_SEPARATOR . $requested);
|
||||
$realBase = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
$realFile = realpath($baseDir . DIRECTORY_SEPARATOR . ltrim($requested, "/"));
|
||||
|
||||
if ($realBase === false || $realFile === false) {
|
||||
if ($realFile === false) {
|
||||
http_response_code(404);
|
||||
echo 'File not found';
|
||||
exit;
|
||||
}
|
||||
|
||||
$realBase = rtrim($realBase, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
if (strpos($realFile, $realBase) !== 0) {
|
||||
http_response_code(403);
|
||||
echo 'Access denied';
|
||||
|
|
@ -82,7 +92,6 @@ if ($size === false) {
|
|||
// Always advertise range support
|
||||
header('Accept-Ranges: bytes');
|
||||
header('Content-Type: ' . $mime);
|
||||
// Optional caching (fine for local LAN usage)
|
||||
header('Cache-Control: public, max-age=3600');
|
||||
|
||||
$rangeHeader = $_SERVER['HTTP_RANGE'] ?? '';
|
||||
|
|
@ -98,7 +107,7 @@ if ($rangeHeader === '') {
|
|||
exit;
|
||||
}
|
||||
|
||||
$buf = 1024 * 256; // 256KB chunks (fewer syscalls than 8KB)
|
||||
$buf = 1024 * 256; // 256KB chunks
|
||||
while (!feof($fp) && !connection_aborted()) {
|
||||
$data = fread($fp, $buf);
|
||||
if ($data === false) break;
|
||||
|
|
@ -109,10 +118,6 @@ if ($rangeHeader === '') {
|
|||
}
|
||||
|
||||
// ---- Range response (single-range only) ----
|
||||
// Examples:
|
||||
// - bytes=0-999
|
||||
// - bytes=1000-
|
||||
// - bytes=-500 (suffix range)
|
||||
if (!preg_match('/^bytes=(\d*)-(\d*)$/', trim($rangeHeader), $m)) {
|
||||
http_response_code(416);
|
||||
header("Content-Range: bytes */{$size}");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue