downloads

This commit is contained in:
Mathieu 2024-01-11 12:39:51 +00:00
parent d719bc283c
commit c7cd2fbbca
2 changed files with 50 additions and 13 deletions

View File

@ -18,7 +18,7 @@ class MultiDownloaderClient
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
} }
public function download(array $files) public function downloadAsString(array $files)
{ {
$files = array_map(function (FileRequest $file) { $files = array_map(function (FileRequest $file) {
return $file->toArray(); return $file->toArray();
@ -27,20 +27,36 @@ class MultiDownloaderClient
return $this->sendRequest($files); return $this->sendRequest($files);
} }
private function sendRequest(array $files) public function downloadTo(string $path, array $files)
{
$files = array_map(function (FileRequest $file) {
return $file->toArray();
}, $files);
$response = $this->sendRequest($files, [
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FILE => fopen($path, 'w'),
]);
return $response;
}
private function sendRequest(array $files, array $options = [])
{ {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiUrl . '/v2/zip'); $options = array_replace($options, [
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); CURLOPT_URL => $this->apiUrl . '/v2/zip',
curl_setopt($ch, CURLOPT_POST, true); CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($files)); CURLOPT_POSTFIELDS => json_encode($files),
CURLOPT_HTTPHEADER => [
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json',
'Content-Type: application/json', ],
]); ]);
curl_setopt_array($ch, $options);
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

View File

@ -17,7 +17,7 @@ class MultiDownloaderClientTest extends TestCase
return $reflection->getValue($object); return $reflection->getValue($object);
} }
public function testRequest() public function testDownloadAsString()
{ {
$client = new MultiDownloaderClient(); $client = new MultiDownloaderClient();
@ -26,9 +26,30 @@ class MultiDownloaderClientTest extends TestCase
new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png') new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png')
]; ];
$response = $client->download($files); $response = $client->downloadAsString($files);
var_dump($response); $md5 = md5($response);
$this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5);
}
public function testDownloadTo()
{
$client = new MultiDownloaderClient();
$files = [
new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png'),
new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png')
];
$path = sys_get_temp_dir() . '/testDownloadTo.zip';
$client->downloadTo($path, $files);
$md5 = md5_file($path);
$this->assertFileExists($path);
$this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5);
} }
public function testApiKeyEnv() public function testApiKeyEnv()