fix(vikon): use cURL streaming for ZIP downloads to avoid memory/timeout issues
This commit is contained in:
@@ -43,15 +43,14 @@ class UpdateCoreAction
|
||||
return;
|
||||
}
|
||||
|
||||
$zipContent = $this->http->downloadWithToken(
|
||||
'pull_updates/generateEmptyModuleCore/' . $moduleId,
|
||||
$accessToken
|
||||
);
|
||||
|
||||
File::makeDirectory($tempPath, 0755, true, true);
|
||||
|
||||
$zipFile = $tempPath . '/module.zip';
|
||||
file_put_contents($zipFile, $zipContent);
|
||||
$this->http->downloadToFile(
|
||||
'pull_updates/generateEmptyModuleCore/' . $moduleId,
|
||||
$accessToken,
|
||||
$zipFile
|
||||
);
|
||||
|
||||
$this->extractZip($zipFile, $tempPath);
|
||||
|
||||
|
||||
@@ -62,16 +62,15 @@ class UpdatePartAction
|
||||
}
|
||||
|
||||
// Step 4: Download ZIP
|
||||
$zipContent = $this->http->downloadWithToken(
|
||||
"pull_updates/downloadPartByNewCoreResult?operation_identity={$operationIdentity}&part={$part}",
|
||||
$accessToken
|
||||
);
|
||||
|
||||
$tempPath = $this->storagePath . '/temp/' . $config['path'] . '_part';
|
||||
File::makeDirectory($tempPath, 0755, true, true);
|
||||
|
||||
$zipFile = $tempPath . '/part.zip';
|
||||
file_put_contents($zipFile, $zipContent);
|
||||
$this->http->downloadToFile(
|
||||
"pull_updates/downloadPartByNewCoreResult?operation_identity={$operationIdentity}&part={$part}",
|
||||
$accessToken,
|
||||
$zipFile
|
||||
);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipFile) !== true) {
|
||||
|
||||
@@ -54,6 +54,7 @@ class HttpTask
|
||||
$response = $this->client()
|
||||
->withToken($token)
|
||||
->withHeaders(['Accept-Encoding' => 'zip, gzip'])
|
||||
->timeout(300)
|
||||
->get($url);
|
||||
|
||||
if ($response->failed()) {
|
||||
@@ -63,6 +64,66 @@ class HttpTask
|
||||
return $response->body();
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a file directly to disk using cURL streaming (avoids memory limit for large files).
|
||||
*/
|
||||
public function downloadToFile(string $endpoint, string $token, string $filePath, string $service = 'api'): void
|
||||
{
|
||||
$url = $this->url($endpoint, $service);
|
||||
|
||||
$headers = [
|
||||
'Authorization: Bearer ' . $token,
|
||||
'Accept-Encoding: zip, gzip',
|
||||
];
|
||||
|
||||
if (config('vikon.domain_resolve')) {
|
||||
$resolve = [
|
||||
'db-nica.ru:443:' . config('vikon.vikon_domain_resolve_ip'),
|
||||
'file.db-nica.ru:443:' . config('vikon.fm_domain_resolve_ip'),
|
||||
];
|
||||
}
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_SSL_VERIFYHOST => 0,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
CURLOPT_TIMEOUT => 300,
|
||||
CURLOPT_CONNECTTIMEOUT => 30,
|
||||
]);
|
||||
|
||||
if (!empty($resolve)) {
|
||||
curl_setopt($ch, CURLOPT_RESOLVE, $resolve);
|
||||
}
|
||||
|
||||
$fp = fopen($filePath, 'w');
|
||||
if (!$fp) {
|
||||
curl_close($ch);
|
||||
throw new \RuntimeException("Cannot open file for writing: {$filePath}");
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||||
curl_exec($ch);
|
||||
|
||||
$error = curl_error($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
fclose($fp);
|
||||
|
||||
if ($error) {
|
||||
@unlink($filePath);
|
||||
throw new \RuntimeException('cURL download error: ' . $error);
|
||||
}
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
@unlink($filePath);
|
||||
throw new \RuntimeException("Download failed with HTTP {$httpCode}");
|
||||
}
|
||||
}
|
||||
|
||||
private function client(): PendingRequest
|
||||
{
|
||||
$client = Http::timeout($this->timeout)
|
||||
|
||||
@@ -90,9 +90,11 @@ class UpdatePartActionTest extends TestCase
|
||||
|
||||
$zipContent = file_get_contents($zipPath);
|
||||
|
||||
$http->shouldReceive('downloadWithToken')
|
||||
$http->shouldReceive('downloadToFile')
|
||||
->once()
|
||||
->andReturn($zipContent);
|
||||
->andReturnUsing(function ($endpoint, $token, $filePath) use ($zipContent) {
|
||||
file_put_contents($filePath, $zipContent);
|
||||
});
|
||||
|
||||
$fs->shouldReceive('validateFileTypes')
|
||||
->once()
|
||||
|
||||
Reference in New Issue
Block a user