This commit is contained in:
Mathieu 2024-01-11 13:05:59 +00:00
parent 1774c1eeff
commit c00c6d1dab
3 changed files with 78 additions and 23 deletions

View File

@ -22,22 +22,21 @@ class MultiDownloaderClient
}
}
public function downloadAsString(array $files)
private function buildRequest(array $files): array
{
$files = array_map(function (FileRequest $file) {
return array_map(function (FileRequest $file) {
return $file->toArray();
}, $files);
return $this->sendRequest($files);
}
public function downloadTo(string $path, array $files)
public function downloadAsString(array $files): string
{
$files = array_map(function (FileRequest $file) {
return $file->toArray();
}, $files);
return $this->sendRequest($this->buildRequest($files));
}
$response = $this->sendRequest($files, [
public function downloadTo(string $path, array $files): string
{
$response = $this->sendRequest($this->buildRequest($files), [
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FILE => fopen($path, 'w'),
]);
@ -45,7 +44,14 @@ class MultiDownloaderClient
return $response;
}
private function sendRequest(array $files, array $options = [])
public function htmlForm(array $files): string
{
ob_start();
include __DIR__ . '/htmlForm.php';
return ob_get_clean();
}
private function sendRequest(array $request, array $options = []): string
{
$ch = curl_init();
@ -53,9 +59,10 @@ class MultiDownloaderClient
CURLOPT_URL => $this->apiUrl . '/v2/zip',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($files),
CURLOPT_POSTFIELDS => json_encode($request),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey,
],
]);

41
src/htmlForm.php Normal file
View File

@ -0,0 +1,41 @@
<?php
use Nwb\MultiDownloaderClient\MultiDownloaderClient;
/**
* @var MultiDownloaderClient $this
*/
?>
<!DOCTYPE html>
<html>
<head>
<title>Forwarding ...</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0">
<style type="text/css">
body {
display: inline;
}
</style>
</head>
<body>
<div id="button_div" class="modal-dialog-buttons">
<form name="f" id="f" method="POST" action="<?= $this->apiUrl ?>/v2/form/zip" enctype="multipart/form-data">
<input type="hidden" name="json" value="<?= json_encode($this->buildRequest($files)) ?>" />
<noscript>
<button id="submit_approve_access" type="submit" tabindex="1" style="overflow:visible;">Continue</button>
</noscript>
</form>
</div>
<script type="text/javascript">
document.forms['f'].submit();
</script>
</body>
</html>

View File

@ -8,6 +8,18 @@ use PHPUnit\Framework\TestCase;
class MultiDownloaderClientTest extends TestCase
{
private $testFiles = [
'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png',
'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png'
];
private function testFiles()
{
return array_map(function ($url) {
return new FileRequest($url);
}, $this->testFiles);
}
public static function getProperty($object, $property)
{
$reflectedClass = new \ReflectionClass($object);
@ -21,12 +33,7 @@ class MultiDownloaderClientTest extends TestCase
{
$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')
];
$response = $client->downloadAsString($files);
$response = $client->downloadAsString($this->testFiles());
$md5 = md5($response);
@ -37,14 +44,9 @@ class MultiDownloaderClientTest extends TestCase
{
$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);
$client->downloadTo($path, $this->testFiles());
$md5 = md5_file($path);
@ -61,6 +63,11 @@ class MultiDownloaderClientTest extends TestCase
$this->assertEquals('1234567890', $apiKey);
}
public function testForm()
{
echo (new MultiDownloaderClient())->htmlForm($this->testFiles());
}
public function testApiKeyParam()
{
$client = new MultiDownloaderClient([