From 1bd7588e866afdd4e6195728c1e1fd4714a193d9 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 21 Feb 2026 09:29:27 -0500 Subject: [PATCH] up --- classes/MediaService.php | 63 ++++++++++++++++++++++++++++------------ scripts/media_worker.php | 4 ++- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/classes/MediaService.php b/classes/MediaService.php index 6a069ff..cdd307c 100644 --- a/classes/MediaService.php +++ b/classes/MediaService.php @@ -22,12 +22,14 @@ class MediaService { if (!is_dir($this->uploadsDir)) mkdir($this->uploadsDir, 0755, true); } - public function fetchAndStoreFromGraph($mediaId, $subdir = '') { - // Obtener metadata (url) via Graph API - $endpoint = "{$this->apiUrl}/{$mediaId}"; + /** + * Hacer petición GET al Graph API con fallback a wget si curl falla + */ + private function graphApiRequest($url) { + // Intentar con curl primero $ch = curl_init(); curl_setopt_array($ch, [ - CURLOPT_URL => $endpoint, + CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token], CURLOPT_TIMEOUT => 15, @@ -39,8 +41,36 @@ class MediaService { $err = curl_error($ch); curl_close($ch); - if ($err || $http >= 400) { - throw new Exception('Graph API fetch failed: ' . $err); + if (!$err && $http >= 200 && $http < 400 && $resp) { + return $resp; + } + + // Fallback a wget para la petición al Graph API + error_log("[MediaService::graphApiRequest] curl failed (http={$http} err={$err}), trying wget..."); + $tmpFile = tempnam(sys_get_temp_dir(), 'graph_'); + $cmd = 'wget -q --prefer-family=IPv4 -O ' . escapeshellarg($tmpFile) + . ' --header=' . escapeshellarg('Authorization: Bearer ' . $this->token) + . ' ' . escapeshellarg($url) . ' 2>&1'; + exec($cmd, $output, $exitCode); + + if ($exitCode === 0 && file_exists($tmpFile) && filesize($tmpFile) > 0) { + $content = file_get_contents($tmpFile); + unlink($tmpFile); + error_log("[MediaService::graphApiRequest] wget OK, " . strlen($content) . " bytes"); + return $content; + } + + error_log("[MediaService::graphApiRequest] wget also failed exit={$exitCode}"); + if (file_exists($tmpFile)) unlink($tmpFile); + return false; + } + + public function fetchAndStoreFromGraph($mediaId, $subdir = '') { + // Obtener metadata (url) via Graph API + $endpoint = "{$this->apiUrl}/{$mediaId}"; + $resp = $this->graphApiRequest($endpoint); + if ($resp === false) { + throw new Exception('Graph API fetch failed for media ' . $mediaId); } $decoded = json_decode($resp, true); $mediaUrl = $decoded['url'] ?? ($decoded['data'][0]['url'] ?? null); @@ -50,19 +80,14 @@ class MediaService { $content = $this->downloadWithWget($mediaUrl, !empty($this->token) ? ['Authorization: Bearer ' . $this->token] : []); if ($content === false) { // Obtener URL fresca del Graph API (la anterior puede haber expirado tras el intento fallido) - error_log("[MediaService] wget failed, trying curl with fresh URL..."); - $ch2 = curl_init($endpoint); - curl_setopt_array($ch2, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token], - CURLOPT_TIMEOUT => 15, - CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - ]); - $resp2 = curl_exec($ch2); - curl_close($ch2); - $decoded2 = json_decode($resp2, true); - $freshUrl = $decoded2['url'] ?? $mediaUrl; + error_log("[MediaService] wget download failed, getting fresh URL..."); + $resp2 = $this->graphApiRequest($endpoint); + if ($resp2) { + $decoded2 = json_decode($resp2, true); + $freshUrl = $decoded2['url'] ?? $mediaUrl; + } else { + $freshUrl = $mediaUrl; + } $content = $this->downloadUrl($freshUrl); } diff --git a/scripts/media_worker.php b/scripts/media_worker.php index b2bf427..1b0c61c 100644 --- a/scripts/media_worker.php +++ b/scripts/media_worker.php @@ -26,7 +26,9 @@ $processed = 0; $success = 0; $failed = 0; -foreach ($rows as $r) { +foreach ($rows as $idx => $r) { + // Pausa entre items para evitar saturar conexiones a Facebook + if ($idx > 0) sleep(2); $processed++; $attempts = intval($r['attempts'] ?? 0) + 1;