Update MediaService.php
This commit is contained in:
+32
-12
@@ -61,20 +61,40 @@ class MediaService {
|
||||
$mediaUrl = $decoded['url'] ?? ($decoded['data'][0]['url'] ?? null);
|
||||
if (!$mediaUrl) throw new Exception('No media url from Graph');
|
||||
|
||||
// Descargar contenido usando cascade robusto (curl, file_get_contents, shell/proc/wget)
|
||||
$content = $this->downloadUrl($mediaUrl);
|
||||
if ($content === false) {
|
||||
// Obtener URL fresca del Graph API (la anterior puede haber expirado tras el intento fallido)
|
||||
error_log("[MediaService] downloadUrl failed, getting fresh URL...");
|
||||
$resp2 = $this->graphApiRequest($endpoint);
|
||||
if ($resp2) {
|
||||
$decoded2 = json_decode($resp2, true);
|
||||
$freshUrl = $decoded2['url'] ?? $mediaUrl;
|
||||
} else {
|
||||
$freshUrl = $mediaUrl;
|
||||
// Para URLs de lookaside.fbsbx.com, usar wget directamente porque curl/OpenSSL falla con ese CDN
|
||||
// Obtener URL fresca justo antes de descargar para evitar expiración
|
||||
$content = false;
|
||||
if (strpos($mediaUrl, 'lookaside.fbsbx.com') !== false) {
|
||||
$authHeaders = !empty($this->token) ? ['Authorization: Bearer ' . $this->token] : [];
|
||||
// Intentar wget directo con URL actual
|
||||
$content = $this->shellDownload($mediaUrl, $authHeaders);
|
||||
if ($content === false) {
|
||||
// URL puede haber expirado — obtener URL fresca y reintentar
|
||||
error_log("[MediaService] wget failed, getting fresh URL for {$mediaId}...");
|
||||
$resp2 = $this->graphApiRequest($endpoint);
|
||||
if ($resp2) {
|
||||
$decoded2 = json_decode($resp2, true);
|
||||
$freshUrl = $decoded2['url'] ?? $mediaUrl;
|
||||
$content = $this->shellDownload($freshUrl, $authHeaders);
|
||||
}
|
||||
}
|
||||
$content = $this->downloadUrl($freshUrl);
|
||||
}
|
||||
|
||||
// Fallback al cascade completo (curl → file_get_contents → wget) para otras URLs
|
||||
if ($content === false) {
|
||||
$content = $this->downloadUrl($mediaUrl);
|
||||
if ($content === false) {
|
||||
// Obtener URL fresca y reintentar
|
||||
error_log("[MediaService] downloadUrl failed, getting fresh URL...");
|
||||
$resp2 = $this->graphApiRequest($endpoint);
|
||||
if ($resp2) {
|
||||
$decoded2 = json_decode($resp2, true);
|
||||
$freshUrl = $decoded2['url'] ?? $mediaUrl;
|
||||
$content = $this->downloadUrl($freshUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($content === false) throw new Exception('Failed to download media content from ' . $mediaUrl);
|
||||
|
||||
// Detectar extension desde mime o url
|
||||
|
||||
Reference in New Issue
Block a user