downloads
This commit is contained in:
parent
d719bc283c
commit
c7cd2fbbca
@ -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);
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user