From 23eceed19a94cab2cba224d38846787491e2c7f2 Mon Sep 17 00:00:00 2001 From: maztch Date: Thu, 4 Apr 2024 12:59:30 +0200 Subject: [PATCH] Develop (#47) * remainig files * allow download stream * update * fix content-disposition --- src/Exceptions/ExtendedException.php | 2 +- src/HtmlpdfTask.php | 2 +- src/Ilovepdf.php | 5 +- src/ProtectTask.php | 114 +++++++++++++++++++++++++++ src/Task.php | 80 +++++++++++++++---- 5 files changed, 184 insertions(+), 19 deletions(-) diff --git a/src/Exceptions/ExtendedException.php b/src/Exceptions/ExtendedException.php index 0bcac94..b22aca1 100644 --- a/src/Exceptions/ExtendedException.php +++ b/src/Exceptions/ExtendedException.php @@ -25,7 +25,7 @@ class ExtendedException extends Exception * @param int $code * @param \Throwable $previous */ - public function __construct($message, $responseBody = null, $code = 0, $previous = null) + public function __construct($message, $responseBody = null, $code = 0, $previous = null) { if (!$code) { $code = 0; diff --git a/src/HtmlpdfTask.php b/src/HtmlpdfTask.php index 1844b20..628be50 100644 --- a/src/HtmlpdfTask.php +++ b/src/HtmlpdfTask.php @@ -45,7 +45,7 @@ class HtmlpdfTask extends Task /** * @var string[] */ - private $pageSizeValues = ['A3', 'A4', 'A5', 'A6', 'Letter']; + private $pageSizeValues = ['A3', 'A4', 'A5', 'A6', 'Letter', 'Auto']; /** * @var string|null diff --git a/src/Ilovepdf.php b/src/Ilovepdf.php index 83c156e..174ea3c 100644 --- a/src/Ilovepdf.php +++ b/src/Ilovepdf.php @@ -11,6 +11,7 @@ use Ilovepdf\Http\Client; use Ilovepdf\Http\ClientException; use Firebase\JWT\JWT; +use Psr\Http\Message\ResponseInterface; /** * Class Ilovepdf @@ -194,13 +195,13 @@ public static function getTokenAlgorithm() * @param array $params * @param bool $start * - * @return mixed response from server + * @return ResponseInterface response from server * * @throws AuthException * @throws ProcessException * @throws UploadException */ - public function sendRequest(string $method, string $endpoint, array $params = [], bool $start = false) + public function sendRequest(string $method, string $endpoint, array $params = [], bool $start = false): ResponseInterface { $to_server = self::getStartServer(); if (!$start && !is_null($this->getWorkerServer())) { diff --git a/src/ProtectTask.php b/src/ProtectTask.php index 74a0f2e..1c0d3b7 100644 --- a/src/ProtectTask.php +++ b/src/ProtectTask.php @@ -13,6 +13,20 @@ class ProtectTask extends Task * @var string|null */ public $password; + /** + * @var string|null + */ + public $owner_password; + + public $allow_print = true; + public $allow_modify = true; + public $allow_copy = true; + public $allow_annotate = true; + public $allow_fill = true; + public $allow_accessibility = true; + public $allow_assemble = true; + public $keep_original = false; + public $allow_nothing = false; /** * UnlockTask constructor. @@ -36,4 +50,104 @@ public function setPassword(string $password): self $this->password = $password; return $this; } + + /** + * @param string $owner_password + * @return $this + */ + public function setOwnerPassword(string $owner_password): self + { + $this->owner_password = $owner_password; + return $this; + } + + /** + * @param bool $allow_print + * @return $this + */ + public function setAllowPrint(bool $allow_print): self + { + $this->allow_print = $allow_print; + return $this; + } + + /** + * @param bool $allow_modify + * @return $this + */ + public function setAllowModify(bool $allow_modify): self + { + $this->allow_modify = $allow_modify; + return $this; + } + + /** + * @param bool $allow_copy + * @return $this + */ + public function setAllowCopy(bool $allow_copy): self + { + $this->allow_copy = $allow_copy; + return $this; + } + + /** + * @param bool $allow_annotate + * @return $this + */ + public function setAllowAnnotate(bool $allow_annotate): self + { + $this->allow_annotate = $allow_annotate; + return $this; + } + + /** + * @param bool $allow_fill + * @return $this + */ + public function setAllowFill(bool $allow_fill): self + { + $this->allow_fill = $allow_fill; + return $this; + } + + /** + * @param bool $allow_accessibility + * @return $this + */ + public function setAllowAccessibility(bool $allow_accessibility): self + { + $this->allow_accessibility = $allow_accessibility; + return $this; + } + + /** + * @param bool $allow_assemble + * @return $this + */ + public function setAllowAssemble(bool $allow_assemble): self + { + $this->allow_assemble = $allow_assemble; + return $this; + } + + /** + * @param bool $keep_original + * @return $this + */ + public function setKeepOriginal(bool $keep_original): self + { + $this->keep_original = $keep_original; + return $this; + } + + /** + * @param bool $allow_nothing + * @return $this + */ + public function setAllowNothing(bool $allow_nothing): self + { + $this->allow_nothing = $allow_nothing; + return $this; + } } diff --git a/src/Task.php b/src/Task.php index 3e3d4f6..d63c142 100644 --- a/src/Task.php +++ b/src/Task.php @@ -8,6 +8,7 @@ use Ilovepdf\Exceptions\StartException; use Ilovepdf\Exceptions\PathException; use Ilovepdf\Exceptions\UploadException; +use Psr\Http\Message\ResponseInterface; /** * Class Ilovepdf @@ -116,6 +117,11 @@ class Task extends Ilovepdf public $outputFileType; + /** + * @var int|null + */ + public $remainingFiles; + /** * Task constructor. * @param string|null $publicKey @@ -139,7 +145,7 @@ function __construct(?string $publicKey, ?string $secretKey, bool $makeStart = f */ public function start(): void { - if($this->tool == null){ + if ($this->tool == null) { throw new StartException('Tool must be set'); } $data = ['v' => self::VERSION]; @@ -153,6 +159,7 @@ public function start(): void if (empty($responseBody->server)) { throw new StartException('no server assigned on start'); }; + $this->_setRemainingFiles($responseBody->remaining_files ?? null); $this->setWorkerServer('https://' . $responseBody->server); $this->setTask($responseBody->task); } @@ -275,11 +282,11 @@ public function addFile($filePath) * @param string $url * @return File */ - public function addFileFromUrl($url) + public function addFileFromUrl($url, $bearerToken = null) { $this->validateTaskStarted(); /** @psalm-suppress PossiblyNullArgument */ - $file = $this->uploadUrl($this->task, $url); + $file = $this->uploadUrl($this->task, $url, $bearerToken); array_push($this->files, $file); return end($this->files); } @@ -316,8 +323,7 @@ public function uploadFile(string $task, string $filepath) $response = $this->sendRequest('post', 'upload', $body); try { $responseBody = json_decode($response->getBody()); - } - catch(\Exception $e){ + } catch (\Exception $e) { throw new UploadException('Upload response error'); } return new File($responseBody->server_filename, basename($filepath)); @@ -367,7 +373,7 @@ public function delete() * @throws ProcessException * @throws UploadException */ - public function uploadUrl($task, $url) + public function uploadUrl($task, $url, $bearerToken = null) { //$data = ['task' => $task, 'cloud_file' => $url, 'v' => self::VERSION]; //$body = ['form_data' => $data]; @@ -378,6 +384,11 @@ public function uploadUrl($task, $url) ['name' => 'cloud_file', 'contents' => $url] ], ]; + + if ($bearerToken) { + $body['multipart'][] = ['name' => 'cloud_token', 'contents' => $bearerToken]; + } + $response = parent::sendRequest('post', 'upload', $body); $responseBody = json_decode($response->getBody()); return new File($responseBody->server_filename, basename($url)); @@ -427,6 +438,7 @@ public function blob() return $this->outputFile; } + /** * @return void * @throws AuthException @@ -475,6 +487,36 @@ public function toBrowser() * @throws DownloadException */ private function downloadFile($task): void + { + $response = $this->downloadRequestData($task); + + try { + $this->outputFile = $response->getBody()->getContents(); + } catch (\Exception $e) { + throw new DownloadException('No file content for download'); + } + } + + /** + * @param string $task + * @return ResponseInterface + */ + public function downloadStream(): ResponseInterface + { + $response = $this->downloadRequestData($this->task); + + return $response; + } + + + /** + * @param string $task + * @return ResponseInterface + * @throws AuthException + * @throws ProcessException + * @throws UploadException + */ + private function downloadRequestData(string $task): ResponseInterface { $data = array('v' => self::VERSION); $body = ['form_params' => $data]; @@ -482,20 +524,19 @@ private function downloadFile($task): void $response = parent::sendRequest('get', 'download/' . $task, $body); $responseHeaders = $response->getHeaders(); + $contentDisposition = isset($responseHeaders['Content-Disposition']) ? $responseHeaders['Content-Disposition'] : $responseHeaders['content-disposition']; - if (preg_match("/filename\*\=utf-8\'\'([\W\w]+)/", $responseHeaders['Content-Disposition'][0], $matchesUtf)) { + if (preg_match("/filename\*\=utf-8\'\'([\W\w]+)/", $contentDisposition[0], $matchesUtf)) { $filename = urldecode(str_replace('"', '', $matchesUtf[1])); } else { - preg_match('/ .*filename=\"([\W\w]+)\"/', $responseHeaders['Content-Disposition'][0], $matches); + preg_match('/ .*filename=\"([\W\w]+)\"/', $contentDisposition[0], $matches); $filename = str_replace('"', '', $matches[1]); } - try { - $this->outputFile = $response->getBody()->getContents(); - } catch (\Exception $e) { - throw new DownloadException('No file content for download'); - } + $this->outputFileName = $filename; $this->outputFileType = pathinfo($this->outputFileName, PATHINFO_EXTENSION); + + return $response; } /** @@ -558,13 +599,13 @@ public function __toArray() { $props = []; $reflection = new \ReflectionClass($this); - $properties = array_filter( + $properties = array_filter( $reflection->getProperties(\ReflectionProperty::IS_PUBLIC), function ($property) { return !$property->isStatic(); } ); - foreach($properties as $property) { + foreach ($properties as $property) { $name = $property->name; $props[$name] = $this->$name; } @@ -797,4 +838,13 @@ private function validateTaskStarted(): void throw new \Exception('Current task does not exists. You must start your task'); } } + + /** + * @param $remainingFiles + * @return void + */ + private function _setRemainingFiles($remainingFiles): void + { + $this->remainingFiles = $remainingFiles; + } }