Skip to content

Commit

Permalink
Develop (#47)
Browse files Browse the repository at this point in the history
* remainig files

* allow download stream

* update

* fix content-disposition
  • Loading branch information
maztch authored Apr 4, 2024
1 parent c169013 commit 23eceed
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/ExtendedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlpdfTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Ilovepdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ilovepdf\Http\Client;
use Ilovepdf\Http\ClientException;
use Firebase\JWT\JWT;
use Psr\Http\Message\ResponseInterface;

/**
* Class Ilovepdf
Expand Down Expand Up @@ -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())) {
Expand Down
114 changes: 114 additions & 0 deletions src/ProtectTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
80 changes: 65 additions & 15 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ilovepdf\Exceptions\StartException;
use Ilovepdf\Exceptions\PathException;
use Ilovepdf\Exceptions\UploadException;
use Psr\Http\Message\ResponseInterface;

/**
* Class Ilovepdf
Expand Down Expand Up @@ -116,6 +117,11 @@ class Task extends Ilovepdf
public $outputFileType;


/**
* @var int|null
*/
public $remainingFiles;

/**
* Task constructor.
* @param string|null $publicKey
Expand All @@ -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];
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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];
Expand All @@ -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));
Expand Down Expand Up @@ -427,6 +438,7 @@ public function blob()
return $this->outputFile;
}


/**
* @return void
* @throws AuthException
Expand Down Expand Up @@ -475,27 +487,56 @@ 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];
/** @psalm-suppress PossiblyNullOperand */
$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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 23eceed

Please sign in to comment.