serve files cleanup
This commit is contained in:
parent
c9f51869cf
commit
8f545c3f67
2 changed files with 233 additions and 208 deletions
149
get_files.php
149
get_files.php
|
|
@ -3,100 +3,111 @@
|
|||
* get_files.php - Recursively scans media directories and returns file structure as JSON
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
declare(strict_types=1);
|
||||
|
||||
// Get the media type from query parameter
|
||||
$type = isset($_GET['type']) ? $_GET['type'] : 'videos';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Define base directories
|
||||
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: '/home/' . get_current_user());
|
||||
$baseDir = $homeDir . '/GDrive';
|
||||
// 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';
|
||||
|
||||
$directories = [
|
||||
'videos' => $baseDir . '/Videos',
|
||||
'music' => $baseDir . '/Music'
|
||||
'videos' => $baseDir . DIRECTORY_SEPARATOR . 'Videos',
|
||||
'music' => $baseDir . DIRECTORY_SEPARATOR . 'Music',
|
||||
];
|
||||
|
||||
// Extensions per tab (don’t mix)
|
||||
$extensionsByType = [
|
||||
'videos' => ['mp4', 'mkv', 'avi', 'mov', 'wmv', 'flv', 'webm', 'm4v'],
|
||||
'music' => ['mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'wma', 'opus'],
|
||||
];
|
||||
|
||||
// Validate type
|
||||
if (!isset($directories[$type])) {
|
||||
echo json_encode(['error' => 'Invalid media type']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$targetDir = $directories[$type];
|
||||
$validExtensions = $extensionsByType[$type];
|
||||
|
||||
// Check if directory exists
|
||||
// Fail fast if dir missing
|
||||
if (!is_dir($targetDir)) {
|
||||
echo json_encode(['error' => 'Directory not found', 'path' => $targetDir]);
|
||||
echo json_encode(['error' => 'Directory not found']);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively scan directory and build file tree
|
||||
* @param string $dir Directory to scan
|
||||
* @param string $baseDir Base directory for relative paths
|
||||
* @return array File tree structure
|
||||
* Recursively scan a directory and build a folder->(folders/files) structure.
|
||||
* Files are returned as paths relative to $baseDir.
|
||||
*/
|
||||
function scanDirectory($dir, $baseDir) {
|
||||
function scanDirectory(string $dir, string $baseDir, array $validExtensions): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
// Valid media extensions
|
||||
$videoExtensions = ['mp4', 'mkv', 'avi', 'mov', 'wmv', 'flv', 'webm', 'm4v'];
|
||||
$audioExtensions = ['mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'wma', 'opus'];
|
||||
$validExtensions = array_merge($videoExtensions, $audioExtensions);
|
||||
|
||||
try {
|
||||
$items = scandir($dir);
|
||||
|
||||
foreach ($items as $item) {
|
||||
// Skip hidden files and parent directory references
|
||||
if ($item === '.' || $item === '..' || $item[0] === '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fullPath = $dir . DIRECTORY_SEPARATOR . $item;
|
||||
|
||||
if (is_dir($fullPath)) {
|
||||
// Recursively scan subdirectory
|
||||
$subItems = scanDirectory($fullPath, $baseDir);
|
||||
if (!empty($subItems)) {
|
||||
$result[$item] = $subItems;
|
||||
}
|
||||
} else {
|
||||
// Check if file has valid media extension
|
||||
$extension = strtolower(pathinfo($item, PATHINFO_EXTENSION));
|
||||
if (in_array($extension, $validExtensions)) {
|
||||
// Store relative path from base directory
|
||||
$relativePath = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $fullPath);
|
||||
$result[$item] = $relativePath;
|
||||
}
|
||||
}
|
||||
|
||||
// scandir() can return false on permissions
|
||||
$items = @scandir($dir);
|
||||
if ($items === false) return [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
// Skip dot/hidden entries
|
||||
if ($item === '.' || $item === '..' || ($item !== '' && $item[0] === '.')) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Handle permission errors gracefully
|
||||
return [];
|
||||
|
||||
$fullPath = $dir . DIRECTORY_SEPARATOR . $item;
|
||||
|
||||
if (is_dir($fullPath)) {
|
||||
$sub = scanDirectory($fullPath, $baseDir, $validExtensions);
|
||||
if (!empty($sub)) {
|
||||
$result[$item] = $sub;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$ext = strtolower(pathinfo($item, PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, $validExtensions, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build a relative path from baseDir. Use realpath() to normalize.
|
||||
$realBase = realpath($baseDir);
|
||||
$realFile = realpath($fullPath);
|
||||
if ($realBase === false || $realFile === false) {
|
||||
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
|
||||
$result[$item] = $relativePath;
|
||||
}
|
||||
|
||||
// Sort results: directories first, then files
|
||||
uksort($result, function($a, $b) use ($result) {
|
||||
$aIsDir = is_array($result[$a]);
|
||||
$bIsDir = is_array($result[$b]);
|
||||
|
||||
if ($aIsDir && !$bIsDir) return -1;
|
||||
if (!$aIsDir && $bIsDir) return 1;
|
||||
return strcasecmp($a, $b);
|
||||
|
||||
// Sort: folders first, then files, alphabetically
|
||||
uksort($result, function ($a, $b) use ($result) {
|
||||
$aIsDir = is_array($result[$a]);
|
||||
$bIsDir = is_array($result[$b]);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Scan the directory and return JSON
|
||||
$fileTree = scanDirectory($targetDir, $baseDir);
|
||||
$tree = scanDirectory($targetDir, $baseDir, $validExtensions);
|
||||
|
||||
if (empty($fileTree)) {
|
||||
echo json_encode(['message' => 'No media files found in this directory']);
|
||||
} else {
|
||||
echo json_encode($fileTree);
|
||||
}
|
||||
?>
|
||||
echo json_encode(
|
||||
empty($tree) ? ['message' => 'No media files found'] : $tree,
|
||||
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue