From 69d481b0da587e421fe702f5afdbd0a01e217bea Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Thu, 22 Feb 2024 18:26:56 -0300 Subject: [PATCH] Use jar version of pdftk Signed-off-by: Vitor Mattos --- lib/Service/ConfigureCheckService.php | 3 +- lib/Service/InstallService.php | 47 ++++----------------------- lib/Service/SignFileService.php | 3 +- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/lib/Service/ConfigureCheckService.php b/lib/Service/ConfigureCheckService.php index cab998848..a12a91ff3 100644 --- a/lib/Service/ConfigureCheckService.php +++ b/lib/Service/ConfigureCheckService.php @@ -133,7 +133,8 @@ public function checkPdftk(): array { $pdftkPath = $this->appConfig->getAppValue('pdftk_path'); if ($pdftkPath) { if (file_exists($pdftkPath)) { - \exec($pdftkPath . " --version 2>&1", $version); + $javaPath = $this->appConfig->getAppValue('java_path'); + \exec($javaPath . ' -jar ' . $pdftkPath . " --version 2>&1", $version); if (isset($version[0])) { preg_match('/pdftk port to java (?.*) a Handy Tool/', $version[0], $matches); if (isset($matches['version'])) { diff --git a/lib/Service/InstallService.php b/lib/Service/InstallService.php index b4a3cb231..dd32bec95 100644 --- a/lib/Service/InstallService.php +++ b/lib/Service/InstallService.php @@ -448,56 +448,21 @@ public function installPdftk(?bool $async = false): void { $this->runAsync(); return; } - $architecture = php_uname('m'); - if ($architecture === 'x86_64') { - $fullPath = $this->installPdfTk64(); - } elseif ($architecture === 'aarch64') { - $fullPath = $this->installPdfTkArm(); - } else { - throw new RuntimeException(sprintf('Architecture %s is incompatible with LibreSign.', $architecture)); - } - chmod($fullPath, 0700); - $this->appConfig->setAppValue('pdftk_path', $fullPath); - $this->removeDownloadProgress(); - } - private function installPdfTk64():string { try { - $file = $this->getFolder()->getFile('pdftk'); + $file = $this->getFolder()->getFile('pdftk.jar'); } catch (\Throwable $th) { - $file = $this->getFolder()->newFile('pdftk'); + $file = $this->getFolder()->newFile('pdftk.jar'); } $fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $this->getInternalPathOfFile($file); - $url = 'https://gitlab.com/api/v4/projects/5024297/packages/generic/pdftk-java/v' . self::PDFTK_VERSION . '/pdftk'; + $url = 'https://gitlab.com/api/v4/projects/5024297/packages/generic/pdftk-java/v' . self::PDFTK_VERSION . '/pdftk-all.jar'; /** WHEN UPDATE version: generate this hash handmade and update here */ - $hash = 'dc5abe9885b26c616821ba1f24f03195'; + $hash = '59a28bed53b428595d165d52988bf4cf'; $this->download($url, 'pdftk', $fullPath, $hash); - return $fullPath; - } - - private function installPdfTkArm():string { - if (!extension_loaded('zip')) { - throw new RuntimeException('Zip extension is not available'); - } - $extractDir = $this->getFullPath(); - $compressedFileName = 'pdftk-' . self::PDFTK_VERSION . '-aarch64.zip'; - try { - $file = $this->getFolder()->getFile($compressedFileName); - } catch (\Throwable $th) { - $file = $this->getFolder()->newFile($compressedFileName); - } - $fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $this->getInternalPathOfFile($file); - $url = 'https://gitlab.com/pdftk-java/pdftk/uploads/2b32d1c2f855f92cea379f566a882ac3/native-image.zip'; - /** WHEN UPDATE version: generate this hash handmade and update here */ - $hash = '3a60ab7d631e2e2cd155fbe73bfa126d'; - - $this->download($url, 'pdftk', $fullPath, $hash); - - $zip = new ZIP($extractDir . DIRECTORY_SEPARATOR . $compressedFileName); - $zip->extractFile('native-image/pdftk', $extractDir . DIRECTORY_SEPARATOR . 'pdftk'); - return $fullPath; + $this->appConfig->setAppValue('pdftk_path', $fullPath); + $this->removeDownloadProgress(); } public function uninstallPdftk(): void { diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index d92e937f9..13eac595f 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -536,10 +536,11 @@ private function getPdfToSign(FileEntity $fileData, File $originalFile): File { $dest = $this->tempManager->getTemporaryFile('signed.pdf'); file_put_contents($dest, $originalFile->getContent()); + $javaPath = $this->appConfig->getAppValue('java_path'); $pdftkPath = $this->appConfig->getAppValue('pdftk_path'); $pdf = new Pdf(); $command = new Command(); - $command->setCommand($pdftkPath); + $command->setCommand($javaPath . ' -jar ' . $pdftkPath); $pdf->setCommand($command); $pdf->addFile($dest); $buffer = $pdf->stamp($background)