This commit is contained in:
lizandrogd
2026-01-21 12:20:46 -05:00
parent 6cec747ae3
commit 901d51c82f
11 changed files with 363 additions and 7 deletions
+49
View File
@@ -70,6 +70,49 @@ if ($providedUrl) {
}
}
// Soporte para archivos locales (local=uploads/media/...)
$local = $_GET['local'] ?? null;
if ($local) {
$localPath = realpath(__DIR__ . '/../../' . ltrim($local, '/'));
$base = realpath(__DIR__ . '/../../uploads/media');
// Prevención simple de path traversal
if (!$localPath || strpos($localPath, $base) !== 0) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Ruta local inválida']);
exit;
}
if (!file_exists($localPath)) {
http_response_code(404);
echo json_encode(['success' => false, 'error' => 'Archivo no encontrado']);
exit;
}
if ($wantsJson) {
// devolver URL pública directa (relative)
echo json_encode(['success' => true, 'url' => '/' . ltrim($local, '/')]);
exit;
}
if ($download) {
// Forzar descarga del archivo local
header('Cache-Control: public, max-age=3600');
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$ctype = finfo_file($finfo, $localPath);
finfo_close($finfo);
header('Content-Type: ' . $ctype);
header('Content-Length: ' . filesize($localPath));
header('Content-Disposition: attachment; filename="' . basename($localPath) . '"');
readfile($localPath);
exit;
}
// Redirigir al archivo local (servidor web lo servirá)
header('Cache-Control: public, max-age=3600');
header('Location: /' . ltrim($local, '/'), true, 302);
exit;
}
// Si se solicitó JSON, devolver la URL
if ($wantsJson) {
echo json_encode(['success' => true, 'url' => $mediaUrl]);
@@ -79,11 +122,17 @@ if ($wantsJson) {
// Si se pidió proxy (download=1), traer el contenido y retornarlo como descarga
if ($download) {
$ch = curl_init();
// Añadir Authorization si existe token en configuración
$token = getConfigFromDB('whatsapp_token', '');
$headers = ['User-Agent: WhatsAppMediaProxy/1.0', 'Accept: */*'];
if ($token) $headers[] = 'Authorization: Bearer ' . $token;
curl_setopt_array($ch, [
CURLOPT_URL => $mediaUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTPHEADER => $headers,
]);
$content = curl_exec($ch);
$info = curl_getinfo($ch);