102 lines
3 KiB
PHP
102 lines
3 KiB
PHP
<?php
|
|
/**
|
|
* get_files.php - Recursively scans media directories and returns file structure as JSON
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Get the media type from query parameter
|
|
$type = isset($_GET['type']) ? $_GET['type'] : 'videos';
|
|
|
|
// Define base directories
|
|
$homeDir = getenv('HOME') ?: (getenv('USERPROFILE') ?: '/home/' . get_current_user());
|
|
$baseDir = $homeDir . '/GDrive';
|
|
|
|
$directories = [
|
|
'videos' => $baseDir . '/Videos',
|
|
'music' => $baseDir . '/Music'
|
|
];
|
|
|
|
// Validate type
|
|
if (!isset($directories[$type])) {
|
|
echo json_encode(['error' => 'Invalid media type']);
|
|
exit;
|
|
}
|
|
|
|
$targetDir = $directories[$type];
|
|
|
|
// Check if directory exists
|
|
if (!is_dir($targetDir)) {
|
|
echo json_encode(['error' => 'Directory not found', 'path' => $targetDir]);
|
|
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
|
|
*/
|
|
function scanDirectory($dir, $baseDir) {
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
// Handle permission errors gracefully
|
|
return [];
|
|
}
|
|
|
|
// 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);
|
|
});
|
|
|
|
return $result;
|
|
}
|
|
|
|
// Scan the directory and return JSON
|
|
$fileTree = scanDirectory($targetDir, $baseDir);
|
|
|
|
if (empty($fileTree)) {
|
|
echo json_encode(['message' => 'No media files found in this directory']);
|
|
} else {
|
|
echo json_encode($fileTree);
|
|
}
|
|
?>
|
|
|