json([ 'error' => "Error: Failed to fetch file (HTTP Code: {$httpCode}). " . ($curlError ? " cURL Error: {$curlError}" : '') ], $httpCode); } // Determine the filename for download $filename = basename($filePath); // Return the file content with download headers return response($response, 200) ->header('Content-Description', 'File Transfer') ->header('Content-Type', 'application/octet-stream') ->header('Content-Disposition', "attachment; filename=\"{$filename}\"") ->header('Expires', '0') ->header('Cache-Control', 'must-revalidate') ->header('Pragma', 'public') ->header('Content-Length', strlen($response)); } }