cinema-studio-pro/backend/get_session_file.php
2026-01-28 00:10:09 +05:30

119 lines
3.5 KiB
PHP

<?php
/**
* Get Session File API
* Fetches a specific file from backend sessions and returns as base64
* 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 {
// Get parameters
$sessionId = $_GET['session_id'] ?? '';
$fileType = $_GET['file_type'] ?? 'image'; // 'image' or 'video'
$filename = $_GET['filename'] ?? '';
// Validate parameters
if (empty($sessionId)) {
throw new Exception('Missing session_id parameter');
}
if (empty($filename)) {
throw new Exception('Missing filename parameter');
}
if (!in_array($fileType, ['image', 'video'])) {
throw new Exception('Invalid file_type parameter (must be "image" or "video")');
}
// Sanitize inputs to prevent directory traversal
$sessionId = basename($sessionId);
$filename = basename($filename);
// Construct file path
$uploadsDir = __DIR__ . '/uploads/sessions';
$subDir = $fileType === 'image' ? 'images' : 'videos';
$filePath = $uploadsDir . '/' . $sessionId . '/' . $subDir . '/' . $filename;
// Check if file exists
if (!file_exists($filePath)) {
http_response_code(404);
echo json_encode([
'success' => false,
'error' => 'File not found',
'message' => 'The requested file does not exist or has been deleted'
]);
exit;
}
// Check if file is still valid (not expired)
$createdAt = filectime($filePath);
$expiresAt = $createdAt + (24 * 3600);
if (time() > $expiresAt) {
http_response_code(410);
echo json_encode([
'success' => false,
'error' => 'File expired',
'message' => 'This file has expired and will be deleted soon'
]);
exit;
}
// Read file
$fileContent = file_get_contents($filePath);
if ($fileContent === false) {
throw new Exception('Failed to read file');
}
// Convert to base64
$base64Data = base64_encode($fileContent);
// Get MIME type from metadata if available
$mimeType = $fileType === 'image' ? 'image/jpeg' : 'video/mp4';
$metaPath = $filePath . '.meta';
if (file_exists($metaPath)) {
$metaContent = file_get_contents($metaPath);
$meta = json_decode($metaContent, true);
if ($meta && isset($meta['mime_type'])) {
$mimeType = $meta['mime_type'];
}
}
// Get file size info
$fileSize = strlen($fileContent);
echo json_encode([
'success' => true,
'data' => $base64Data,
'filename' => $filename,
'mime_type' => $mimeType,
'file_type' => $fileType,
'size_bytes' => $fileSize,
'size_kb' => round($fileSize / 1024, 2),
'size_mb' => round($fileSize / (1024 * 1024), 2),
'created_at' => $createdAt,
'expires_at' => $expiresAt,
'time_remaining' => max(0, $expiresAt - time())
]);
} catch (Exception $e) {
error_log("Error in get_session_file.php: " . $e->getMessage());
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'Failed to fetch file',
'message' => $e->getMessage()
]);
}