adds MEDIA_ROOT environment variable for starting the server

This commit is contained in:
markmental 2026-02-03 13:19:20 -05:00
commit 5f7b1aa7c1
3 changed files with 76 additions and 64 deletions

View file

@ -3,19 +3,27 @@
* get_files.php - Recursively scans media directories and returns file structure as JSON
*/
declare(strict_types=1);
require_once __DIR__ . '/auth.php';
require_auth(true);
header('Content-Type: application/json; charset=utf-8');
// Media type from query parameter (default: videos)
$type = $_GET['type'] ?? 'videos';
// Resolve base directory (make this configurable via env in production)
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: ('/home/' . get_current_user()));
$baseDir = rtrim($homeDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'GDrive';
// Resolve base directory via MEDIA_ROOT
$mediaRoot = getenv('MEDIA_ROOT');
if ($mediaRoot === false || trim($mediaRoot) === '') {
echo json_encode(['error' => 'MEDIA_ROOT is not set']);
exit;
}
$baseDir = realpath(trim($mediaRoot));
if ($baseDir === false || !is_dir($baseDir)) {
echo json_encode(['error' => 'MEDIA_ROOT base directory not found']);
exit;
}
$directories = [
'videos' => $baseDir . DIRECTORY_SEPARATOR . 'Videos',
@ -50,6 +58,13 @@ function scanDirectory(string $dir, string $baseDir, array $validExtensions): ar
{
$result = [];
// Resolve base once (prevents per-file realpath overhead and handles normalization)
$realBase = realpath($baseDir);
if ($realBase === false) {
return [];
}
$realBase = rtrim($realBase, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
// scandir() can return false on permissions
$items = @scandir($dir);
if ($items === false) return [];
@ -75,35 +90,32 @@ function scanDirectory(string $dir, string $baseDir, array $validExtensions): ar
continue;
}
// Build a relative path from baseDir. Use realpath() to normalize.
$realBase = realpath($baseDir);
// Normalize and enforce: file must live under baseDir
$realFile = realpath($fullPath);
if ($realBase === false || $realFile === false) {
if ($realFile === false) {
continue;
}
if (strpos($realFile, $realBase) !== 0) {
continue;
}
// Enforce: file must live under baseDir
if (strpos($realFile, $realBase . DIRECTORY_SEPARATOR) !== 0) {
continue;
}
$relativePath = substr($realFile, strlen($realBase) + 1); // +1 skips the slash
// Return relative path from baseDir
$relativePath = substr($realFile, strlen($realBase));
$result[$item] = $relativePath;
}
// Sort: folders first, then files, alphabetically
uksort($result, function ($a, $b) use ($result) {
$aIsDir = is_array($result[$a]);
$bIsDir = is_array($result[$b]);
$aIsDir = is_array($result[$a]);
$bIsDir = is_array($result[$b]);
if ($aIsDir && !$bIsDir) return -1;
if (!$aIsDir && $bIsDir) return 1;
if ($aIsDir && !$bIsDir) return -1;
if (!$aIsDir && $bIsDir) return 1;
// IMPORTANT: keys can be ints if the filename is numeric (e.g. "01", "2026")
return strcasecmp((string)$a, (string)$b);
// IMPORTANT: keys can be ints if the filename is numeric (e.g. "01", "2026")
return strcasecmp((string)$a, (string)$b);
});
return $result;
}