up
This commit is contained in:
@@ -101,6 +101,42 @@ if (!$mediaId && !$providedUrl) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── PRIORIDAD: Buscar archivo local en BD antes de consultar Graph API ──
|
||||
if ($mediaId) {
|
||||
try {
|
||||
$db = Database::getInstance();
|
||||
// Buscar por media_url (que almacena el media ID) o por whatsapp_media_id
|
||||
$row = $db->fetchOne(
|
||||
"SELECT local_file, local_thumb, mime_type, filename FROM conversations WHERE (media_url = :mid OR whatsapp_media_id = :mid2) AND local_file IS NOT NULL AND local_file != '' ORDER BY id DESC LIMIT 1",
|
||||
['mid' => $mediaId, 'mid2' => $mediaId]
|
||||
);
|
||||
if ($row && !empty($row['local_file'])) {
|
||||
$localPath = realpath(__DIR__ . '/../../' . ltrim($row['local_file'], '/'));
|
||||
$base = realpath(__DIR__ . '/../../uploads');
|
||||
if ($localPath && strpos($localPath, $base) === 0 && file_exists($localPath)) {
|
||||
media_log("Serving local file for media ID {$mediaId}: {$localPath}");
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$ctype = finfo_file($finfo, $localPath) ?: ($row['mime_type'] ?? 'application/octet-stream');
|
||||
finfo_close($finfo);
|
||||
|
||||
header_remove('Content-Type');
|
||||
header('Cache-Control: public, max-age=86400');
|
||||
header('Content-Type: ' . $ctype);
|
||||
header('Content-Length: ' . filesize($localPath));
|
||||
if ($download) {
|
||||
$fname = $row['filename'] ?: basename($localPath);
|
||||
header('Content-Disposition: attachment; filename="' . $fname . '"');
|
||||
}
|
||||
readfile($localPath);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
media_log("Error buscando local_file en BD: " . $e->getMessage());
|
||||
// Continuar con Graph API como fallback
|
||||
}
|
||||
}
|
||||
|
||||
$mediaUrl = null;
|
||||
|
||||
if ($providedUrl) {
|
||||
|
||||
Reference in New Issue
Block a user