addfile
This commit is contained in:
parent
c00c6d1dab
commit
4c2e559bab
@ -7,6 +7,8 @@ class MultiDownloaderClient
|
||||
private string $apiUrl = 'https://multi-dl.kub.nwb.fr';
|
||||
private string $apiKey;
|
||||
|
||||
private array $files = [];
|
||||
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$apiKey = $options['apiKey'] ?? getenv('MULTI_DOWNLOADER_API_KEY');
|
||||
@ -22,21 +24,45 @@ class MultiDownloaderClient
|
||||
}
|
||||
}
|
||||
|
||||
private function buildRequest(array $files): array
|
||||
public function setFiles(array $files)
|
||||
{
|
||||
$this->files = [];
|
||||
$this->addFiles($files);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFile(FileRequest $file)
|
||||
{
|
||||
$this->files[] = $file;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFiles(array $files)
|
||||
{
|
||||
foreach ($files as $file) {
|
||||
$this->addFile($file);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function buildRequest(): array
|
||||
{
|
||||
return array_map(function (FileRequest $file) {
|
||||
return $file->toArray();
|
||||
}, $files);
|
||||
}, $this->files);
|
||||
}
|
||||
|
||||
public function downloadAsString(array $files): string
|
||||
public function downloadAsString(): string
|
||||
{
|
||||
return $this->sendRequest($this->buildRequest($files));
|
||||
return $this->sendRequest();
|
||||
}
|
||||
|
||||
public function downloadTo(string $path, array $files): string
|
||||
public function downloadTo(string $path): string
|
||||
{
|
||||
$response = $this->sendRequest($this->buildRequest($files), [
|
||||
$response = $this->sendRequest([
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_FILE => fopen($path, 'w'),
|
||||
]);
|
||||
@ -44,14 +70,14 @@ class MultiDownloaderClient
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function htmlForm(array $files): string
|
||||
public function htmlForm(): string
|
||||
{
|
||||
ob_start();
|
||||
include __DIR__ . '/htmlForm.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
private function sendRequest(array $request, array $options = []): string
|
||||
private function sendRequest(array $options = []): string
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
@ -59,7 +85,7 @@ class MultiDownloaderClient
|
||||
CURLOPT_URL => $this->apiUrl . '/v2/zip',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => json_encode($request),
|
||||
CURLOPT_POSTFIELDS => json_encode($this->buildRequest()),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Bearer ' . $this->apiKey,
|
||||
|
@ -25,7 +25,7 @@ use Nwb\MultiDownloaderClient\MultiDownloaderClient;
|
||||
<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)) ?>" />
|
||||
<input type="hidden" name="json" value="<?= json_encode($this->buildRequest()) ?>" />
|
||||
|
||||
<noscript>
|
||||
<button id="submit_approve_access" type="submit" tabindex="1" style="overflow:visible;">Continue</button>
|
||||
|
@ -33,7 +33,9 @@ class MultiDownloaderClientTest extends TestCase
|
||||
{
|
||||
$client = new MultiDownloaderClient();
|
||||
|
||||
$response = $client->downloadAsString($this->testFiles());
|
||||
$client->setFiles($this->testFiles());
|
||||
|
||||
$response = $client->downloadAsString();
|
||||
|
||||
$md5 = md5($response);
|
||||
|
||||
@ -46,7 +48,8 @@ class MultiDownloaderClientTest extends TestCase
|
||||
|
||||
$path = sys_get_temp_dir() . '/testDownloadTo.zip';
|
||||
|
||||
$client->downloadTo($path, $this->testFiles());
|
||||
$client->setFiles($this->testFiles());
|
||||
$client->downloadTo($path);
|
||||
|
||||
$md5 = md5_file($path);
|
||||
|
||||
@ -65,7 +68,10 @@ class MultiDownloaderClientTest extends TestCase
|
||||
|
||||
public function testForm()
|
||||
{
|
||||
echo (new MultiDownloaderClient())->htmlForm($this->testFiles());
|
||||
$client = new MultiDownloaderClient();
|
||||
$client->setFiles($this->testFiles());
|
||||
|
||||
echo $client->htmlForm($this->testFiles());
|
||||
}
|
||||
|
||||
public function testApiKeyParam()
|
||||
|
Loading…
Reference in New Issue
Block a user