169 lines
5.5 KiB
PHP
169 lines
5.5 KiB
PHP
<?php
|
|
/**
|
|
* List Session Files API
|
|
* Scans uploads/sessions/ directory and returns all available files
|
|
* Used for importing backend files to projects
|
|
*/
|
|
|
|
// Suppress HTML error output
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
ini_set('log_errors', 1);
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
// Handle OPTIONS preflight
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$uploadsDir = __DIR__ . '/uploads/sessions';
|
|
|
|
// Check if directory exists
|
|
if (!is_dir($uploadsDir)) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'sessions' => [],
|
|
'message' => 'No sessions directory found'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$sessions = [];
|
|
$sessionDirs = glob($uploadsDir . '/*', GLOB_ONLYDIR);
|
|
|
|
foreach ($sessionDirs as $sessionPath) {
|
|
$sessionId = basename($sessionPath);
|
|
$imagesDir = $sessionPath . '/images';
|
|
$videosDir = $sessionPath . '/videos';
|
|
|
|
$sessionData = [
|
|
'session_id' => $sessionId,
|
|
'images' => [],
|
|
'videos' => []
|
|
];
|
|
|
|
// Scan images directory
|
|
if (is_dir($imagesDir)) {
|
|
$imageFiles = glob($imagesDir . '/*.{jpg,jpeg,png,webp}', GLOB_BRACE);
|
|
|
|
foreach ($imageFiles as $imagePath) {
|
|
$filename = basename($imagePath);
|
|
$metaPath = $imagePath . '.meta';
|
|
|
|
// Get file stats
|
|
$fileSize = filesize($imagePath);
|
|
$createdAt = filectime($imagePath);
|
|
$expiresAt = $createdAt + (24 * 3600); // 24 hours from creation
|
|
|
|
// Read metadata if available
|
|
$mimeType = 'image/jpeg';
|
|
if (file_exists($metaPath)) {
|
|
$metaContent = file_get_contents($metaPath);
|
|
$meta = json_decode($metaContent, true);
|
|
if ($meta && isset($meta['mime_type'])) {
|
|
$mimeType = $meta['mime_type'];
|
|
}
|
|
}
|
|
|
|
$sessionData['images'][] = [
|
|
'filename' => $filename,
|
|
'path' => 'sessions/' . $sessionId . '/images/' . $filename,
|
|
'created_at' => $createdAt,
|
|
'expires_at' => $expiresAt,
|
|
'time_remaining' => max(0, $expiresAt - time()),
|
|
'mime_type' => $mimeType,
|
|
'size_kb' => round($fileSize / 1024, 2),
|
|
'size_bytes' => $fileSize
|
|
];
|
|
}
|
|
}
|
|
|
|
// Scan videos directory
|
|
if (is_dir($videosDir)) {
|
|
$videoFiles = glob($videosDir . '/*.{mp4,webm,mov}', GLOB_BRACE);
|
|
|
|
foreach ($videoFiles as $videoPath) {
|
|
$filename = basename($videoPath);
|
|
$metaPath = $videoPath . '.meta';
|
|
|
|
// Get file stats
|
|
$fileSize = filesize($videoPath);
|
|
$createdAt = filectime($videoPath);
|
|
$expiresAt = $createdAt + (24 * 3600);
|
|
|
|
// Read metadata if available
|
|
$mimeType = 'video/mp4';
|
|
if (file_exists($metaPath)) {
|
|
$metaContent = file_get_contents($metaPath);
|
|
$meta = json_decode($metaContent, true);
|
|
if ($meta && isset($meta['mime_type'])) {
|
|
$mimeType = $meta['mime_type'];
|
|
}
|
|
}
|
|
|
|
$sessionData['videos'][] = [
|
|
'filename' => $filename,
|
|
'path' => 'sessions/' . $sessionId . '/videos/' . $filename,
|
|
'created_at' => $createdAt,
|
|
'expires_at' => $expiresAt,
|
|
'time_remaining' => max(0, $expiresAt - time()),
|
|
'mime_type' => $mimeType,
|
|
'size_kb' => round($fileSize / 1024, 2),
|
|
'size_mb' => round($fileSize / (1024 * 1024), 2),
|
|
'size_bytes' => $fileSize
|
|
];
|
|
}
|
|
}
|
|
|
|
// Only include sessions that have files
|
|
if (count($sessionData['images']) > 0 || count($sessionData['videos']) > 0) {
|
|
$sessions[] = $sessionData;
|
|
}
|
|
}
|
|
|
|
// Sort sessions by most recent file
|
|
usort($sessions, function($a, $b) {
|
|
$aMax = 0;
|
|
$bMax = 0;
|
|
|
|
foreach ($a['images'] as $img) {
|
|
$aMax = max($aMax, $img['created_at']);
|
|
}
|
|
foreach ($a['videos'] as $vid) {
|
|
$aMax = max($aMax, $vid['created_at']);
|
|
}
|
|
|
|
foreach ($b['images'] as $img) {
|
|
$bMax = max($bMax, $img['created_at']);
|
|
}
|
|
foreach ($b['videos'] as $vid) {
|
|
$bMax = max($bMax, $vid['created_at']);
|
|
}
|
|
|
|
return $bMax - $aMax; // Descending order (newest first)
|
|
});
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'sessions' => $sessions,
|
|
'total_sessions' => count($sessions),
|
|
'total_files' => array_reduce($sessions, function($carry, $session) {
|
|
return $carry + count($session['images']) + count($session['videos']);
|
|
}, 0)
|
|
], JSON_PRETTY_PRINT);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error in list_session_files.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Failed to list session files',
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|