From c7cd2fbbca2f9b6879d64efc22689ca807d36be6 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Thu, 11 Jan 2024 12:39:51 +0000 Subject: [PATCH] downloads --- src/MultiDownloaderClient.php | 36 +++++++++++++++++++++-------- tests/MultiDownloaderClientTest.php | 27 +++++++++++++++++++--- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index b8ad78f..3401bf4 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -18,7 +18,7 @@ class MultiDownloaderClient $this->apiKey = $apiKey; } - public function download(array $files) + public function downloadAsString(array $files) { $files = array_map(function (FileRequest $file) { return $file->toArray(); @@ -27,20 +27,36 @@ class MultiDownloaderClient 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(); - curl_setopt($ch, CURLOPT_URL, $this->apiUrl . '/v2/zip'); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_POST, true); - - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($files)); - - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'Content-Type: application/json', + $options = array_replace($options, [ + CURLOPT_URL => $this->apiUrl . '/v2/zip', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode($files), + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + ], ]); + curl_setopt_array($ch, $options); + $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); diff --git a/tests/MultiDownloaderClientTest.php b/tests/MultiDownloaderClientTest.php index 9400bcd..8470136 100644 --- a/tests/MultiDownloaderClientTest.php +++ b/tests/MultiDownloaderClientTest.php @@ -17,7 +17,7 @@ class MultiDownloaderClientTest extends TestCase return $reflection->getValue($object); } - public function testRequest() + public function testDownloadAsString() { $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') ]; - $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()