From 66dc55cc15a1be96ca9ee96c4dfabbb0c581fd07 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 22 Jan 2026 11:35:05 -0500 Subject: [PATCH] up --- api/upload_media.php | 52 +++++++++++++++++++++++++++++--------------- chat_window.php | 6 +++++ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/api/upload_media.php b/api/upload_media.php index b36402b..52ba6ea 100644 --- a/api/upload_media.php +++ b/api/upload_media.php @@ -98,27 +98,43 @@ try { } // Si es audio WebM, intentar convertir a OGG/Opus (formatos más compatibles con WhatsApp) + $converted = false; + $conversion_error = null; if (strpos($fileType, 'audio/webm') === 0) { - $ffmpeg = trim(@shell_exec('which ffmpeg 2>/dev/null')); - if ($ffmpeg) { - $convertedName = uniqid('media_', true) . '.ogg'; - $convertedPath = $uploadDir . $convertedName; - // Convertir a Opus (bitrate moderado) - $cmd = escapeshellcmd($ffmpeg) . ' -y -i ' . escapeshellarg($uploadPath) . ' -c:a libopus -b:a 64000 ' . escapeshellarg($convertedPath) . ' 2>&1'; - $out = shell_exec($cmd); - if (file_exists($convertedPath) && filesize($convertedPath) > 0) { - // Reemplazar archivo original por el convertido - @unlink($uploadPath); - $uploadPath = $convertedPath; - $uniqueName = $convertedName; - $fileType = 'audio/ogg'; - error_log('upload_media.php - Audio convertido a OGG/Opus: ' . $convertedName); - error_log('upload_media.php - ffmpeg output: ' . substr($out, 0, 2000)); + // Evitar fatal si shell_exec está deshabilitado en el hosting + if (function_exists('shell_exec')) { + $ffmpeg = trim(@shell_exec('which ffmpeg 2>/dev/null')); + if ($ffmpeg) { + try { + $convertedName = uniqid('media_', true) . '.ogg'; + $convertedPath = $uploadDir . $convertedName; + // Convertir a Opus (bitrate moderado) + $cmd = escapeshellcmd($ffmpeg) . ' -y -i ' . escapeshellarg($uploadPath) . ' -c:a libopus -b:a 64000 ' . escapeshellarg($convertedPath) . ' 2>&1'; + $out = @shell_exec($cmd); + if (file_exists($convertedPath) && filesize($convertedPath) > 0) { + // Reemplazar archivo original por el convertido + @unlink($uploadPath); + $uploadPath = $convertedPath; + $uniqueName = $convertedName; + $fileType = 'audio/ogg'; + $converted = true; + error_log('upload_media.php - Audio convertido a OGG/Opus: ' . $convertedName); + error_log('upload_media.php - ffmpeg output: ' . substr($out ?: '', 0, 2000)); + } else { + $conversion_error = 'Conversion failed or no output'; + error_log('upload_media.php - Falló conversión con ffmpeg. Output: ' . substr($out ?: '', 0, 2000)); + } + } catch (Exception $ex) { + $conversion_error = 'Conversion exception: ' . $ex->getMessage(); + error_log('upload_media.php - Conversion exception: ' . $ex->getMessage()); + } } else { - error_log('upload_media.php - Falló conversión con ffmpeg. Output: ' . substr($out, 0, 2000)); + $conversion_error = 'ffmpeg not found'; + error_log('upload_media.php - ffmpeg no encontrado, no se puede convertir audio/webm.'); } } else { - error_log('upload_media.php - ffmpeg no encontrado, no se puede convertir audio/webm.'); + $conversion_error = 'shell_exec disabled'; + error_log('upload_media.php - shell_exec disabled, no se puede convertir audio/webm.'); } } @@ -160,6 +176,8 @@ try { 'media_type' => $mediaType, 'mime_type' => $fileType, 'size' => $fileSize, + 'converted' => isset($converted) ? (bool)$converted : false, + 'conversion_error' => isset($conversion_error) ? $conversion_error : null, // Mantener compatibilidad con código existente 'data' => [ 'id' => $mediaId, diff --git a/chat_window.php b/chat_window.php index 6579d5c..1944bec 100644 --- a/chat_window.php +++ b/chat_window.php @@ -1461,6 +1461,12 @@ if (empty($user_id)) { if (!uploadData.success) { throw new Error(uploadData.error || 'Error subiendo archivo'); } + + // Avisar si la conversión falló en el servidor (ej. shell_exec deshabilitado o sin ffmpeg) + if (uploadData.conversion_error) { + console.warn('upload conversion issue:', uploadData.conversion_error); + showAlert('Advertencia: La conversión de audio no se realizó en el servidor. El audio puede no reproducirse en algunos clientes. (' + uploadData.conversion_error + ')', 'warning'); + } // Enviar mensaje con el archivo const caption = document.getElementById('mediaCaption').value.trim();