Skip to content

Commit

Permalink
feat: add downloadFrom form field
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Sep 20, 2024
1 parent 25e7076 commit 362845b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/DownloadFrom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Gotenberg;

use JsonSerializable;

class DownloadFrom implements JsonSerializable
{
/** @param array<string,string> $extraHttpHeaders */
public function __construct(
public readonly string $url,
public readonly array|null $extraHttpHeaders = null,
) {
}

/** @return array<string,string|array<string,string>> */
public function jsonSerialize(): array
{
$serialized = [
'url' => $this->url,
];

if (! empty($this->extraHttpHeaders)) {
$serialized['extraHttpHeaders'] = $this->extraHttpHeaders;
}

return $serialized;
}
}
19 changes: 19 additions & 0 deletions src/MultipartFormDataModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ public function outputFilename(string $filename): self
return $this;
}

/**
* Sets the URLs to download files from.
*
* @param DownloadFrom[] $downloadFrom
*
* @throws NativeFunctionErrored
*/
public function downloadFrom(array $downloadFrom): self
{
$json = json_encode($downloadFrom);
if ($json === false) {
throw NativeFunctionErrored::createFromLastPhpError();
}

$this->formValue('downloadFrom', $json);

return $this;
}

/**
* Sets the callback and error callback that Gotenberg will use to send
* respectively the output file and the error response.
Expand Down
8 changes: 8 additions & 0 deletions tests/MultipartFormDataModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Gotenberg\DownloadFrom;
use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\Test\DummyMultipartFormDataModule;

Expand All @@ -11,6 +12,10 @@ function (): void {
$dummy = new DummyMultipartFormDataModule('https://my.url/');
$request = $dummy
->outputFilename('my_filename')
->downloadFrom([
new DownloadFrom('https://my.url/my_filename'),
new DownloadFrom('https://my.url/my_filename_2', ['X-Header' => 'value']),
])
->webhook('https://my.webhook.url', 'https://my.webhook.error.url')
->webhookMethod('POST')
->webhookErrorMethod('PUT')
Expand All @@ -21,6 +26,9 @@ function (): void {
->build();

expect($request->getHeader('Gotenberg-Output-Filename'))->toMatchArray(['my_filename']);

expect(sanitize($request->getBody()->getContents()))->toContainFormValue('downloadFrom', '[{"url":"https:\/\/my.url\/my_filename"},{"url":"https:\/\/my.url\/my_filename_2","extraHttpHeaders":{"X-Header":"value"}}]');

expect($request->getHeader('Gotenberg-Webhook-Url'))->toMatchArray(['https://my.webhook.url']);
expect($request->getHeader('Gotenberg-Webhook-Error-Url'))->toMatchArray(['https://my.webhook.error.url']);
expect($request->getHeader('Gotenberg-Webhook-Method'))->toMatchArray(['POST']);
Expand Down

0 comments on commit 362845b

Please sign in to comment.