voice2text/download.php
2025-11-03 15:23:55 +00:00

86 lines
2.4 KiB
PHP
Executable file

<?php
/**
* Download handler for transcribed files
*/
// Prevent any output before headers
ob_start();
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 0); // Don't display errors, log them
// Load configuration and authentication
require_once 'config.php';
// Check authentication
if (!isAuthenticated()) {
http_response_code(401);
die('Authentication required. Please log in.');
}
if (!isset($_GET['file'])) {
http_response_code(400);
die('No file specified');
}
$filename = basename($_GET['file']); // Security: prevent directory traversal
// Check if file belongs to current user
if (!isset($_SESSION['user_files']) || !in_array($filename, $_SESSION['user_files'])) {
http_response_code(403);
error_log("Unauthorized download attempt: " . $filename . " by user " . ($_SESSION['user_id'] ?? 'unknown'));
die('Access denied. You do not have permission to download this file.');
}
$filepath = __DIR__ . '/outputs/' . $filename;
// Debug logging
error_log("Download request for: " . $filename);
error_log("Full path: " . $filepath);
error_log("File exists: " . (file_exists($filepath) ? 'yes' : 'no'));
if (!file_exists($filepath)) {
http_response_code(404);
error_log("File not found: " . $filepath);
die('File not found: ' . $filename);
}
// Check if file is readable
if (!is_readable($filepath)) {
http_response_code(403);
error_log("File not readable: " . $filepath);
die('File not readable');
}
// Determine content type based on extension
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$contentTypes = [
'txt' => 'text/plain; charset=utf-8',
'vtt' => 'text/vtt; charset=utf-8',
'srt' => 'text/plain; charset=utf-8' // Changed to text/plain for better compatibility
];
$contentType = $contentTypes[$extension] ?? 'application/octet-stream';
// Clear all output buffers
while (ob_get_level()) {
ob_end_clean();
}
// Prevent any caching
header('Content-Description: File Transfer');
header('Content-Type: ' . $contentType);
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
// Flush system output buffer
flush();
// Output file
readfile($filepath);
exit;