up
This commit is contained in:
@@ -290,6 +290,44 @@ if ($wantsJson) {
|
||||
|
||||
// Si se pidió proxy (download=1), traer el contenido y retornarlo como descarga
|
||||
if ($download) {
|
||||
// Si la URL apunta al mismo servidor (localhost/127.0.0.1 o al host actual), no usar curl (evita deadlocks en PHP built-in server)
|
||||
$parsedUrl = parse_url($mediaUrl);
|
||||
$host = isset($parsedUrl['host']) ? strtolower($parsedUrl['host']) : '';
|
||||
$port = isset($parsedUrl['port']) ? $parsedUrl['port'] : null;
|
||||
|
||||
$isLocalHost = in_array($host, ['127.0.0.1', 'localhost']) || ($host && isset($_SERVER['HTTP_HOST']) && stripos($_SERVER['HTTP_HOST'], $host) !== false);
|
||||
|
||||
if ($isLocalHost) {
|
||||
media_log("Serving local file directly for URL: {$mediaUrl}");
|
||||
|
||||
// Mapear path a archivo local seguro
|
||||
$path = $parsedUrl['path'] ?? '';
|
||||
// Asumir que los archivos subidos están en uploads/
|
||||
$localPath = realpath(__DIR__ . '/../../' . ltrim($path, '/'));
|
||||
$base = realpath(__DIR__ . '/../../uploads');
|
||||
|
||||
if (!$localPath || strpos($localPath, $base) !== 0 || !file_exists($localPath)) {
|
||||
http_response_code(404);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode(['success' => false, 'error' => 'Archivo local no encontrado o ruta inválida', 'path' => $localPath]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Determinar mime type
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$contentType = finfo_file($finfo, $localPath);
|
||||
finfo_close($finfo);
|
||||
|
||||
header_remove('Content-Type');
|
||||
header('Cache-Control: public, max-age=3600');
|
||||
if (!empty($contentType)) header('Content-Type: ' . $contentType);
|
||||
header('Content-Length: ' . filesize($localPath));
|
||||
header('Content-Disposition: attachment; filename="' . basename($localPath) . '"');
|
||||
readfile($localPath);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Si no es local, hacer curl normal (incluye Authorization si aplica)
|
||||
$ch = curl_init();
|
||||
// Añadir Authorization si existe token en configuración
|
||||
$token = getConfigFromDB('whatsapp_token', '');
|
||||
|
||||
Reference in New Issue
Block a user