diff --git a/.github/workflows/app-check-code.yml b/.github/workflows/app-check-code.yml deleted file mode 100644 index 399dba6902..0000000000 --- a/.github/workflows/app-check-code.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: PHP AppCode Check - -on: - pull_request: - push: - branches: - - master - -jobs: - unit-tests: - runs-on: ubuntu-latest - strategy: - matrix: - php-versions: ['7.4'] - nextcloud-versions: ['stable22'] - name: AppCode check - steps: - - name: Set up php${{ matrix.php-versions }} - uses: shivammathur/setup-php@master - with: - php-version: ${{ matrix.php-versions }} - extensions: ctype,curl,dom,gd,iconv,intl,json,mbstring,openssl,posix,sqlite,xml,zip - coverage: xdebug - - name: Checkout Nextcloud - run: git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b ${{ matrix.nextcloud-versions }} nextcloud - - name: Checkout - uses: actions/checkout@master - with: - path: nextcloud/apps/libresign - - name: App Code check - working-directory: nextcloud - run: php occ app:check-code libresign diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 2fe6c82ed4..42e33a64d4 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -76,7 +76,11 @@ jobs: --database-pass=rootpassword \ --admin-user=admin \ --admin-pass=password + - name: Enable app + run: | ./occ app:enable ${{ env.APP_NAME }} + - name: Start built in server + run: | php -S localhost:8080 & - name: Set up dependencies apps run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index 77a076c33a..3f6ed61544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ Types of changes: - *Security* in case of vulnerabilities. +## 3.1.1 - 2022-05-05 + +# Fixed +- Replaced more usages of TCPDI by LibreSignCLI + ## 3.1.0 - 2022-04-26 # Added diff --git a/appinfo/info.xml b/appinfo/info.xml index cd6217e4b4..0d62df1bfc 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -10,7 +10,7 @@ ]]> - 3.1.0 + 3.1.1 agpl Libre Code diff --git a/composer.lock b/composer.lock index 3a0e69363e..419fb38db8 100644 --- a/composer.lock +++ b/composer.lock @@ -944,16 +944,16 @@ }, { "name": "byjg/uri", - "version": "2.1.2", + "version": "2.1.3", "source": { "type": "git", "url": "https://github.com/byjg/uri.git", - "reference": "3e69f1cc381623281cf5a20ba5e4ead898e2a2ca" + "reference": "2e5a2e4359c783dca98802f2b2fd61104233ade1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/byjg/uri/zipball/3e69f1cc381623281cf5a20ba5e4ead898e2a2ca", - "reference": "3e69f1cc381623281cf5a20ba5e4ead898e2a2ca", + "url": "https://api.github.com/repos/byjg/uri/zipball/2e5a2e4359c783dca98802f2b2fd61104233ade1", + "reference": "2e5a2e4359c783dca98802f2b2fd61104233ade1", "shasum": "" }, "require": { @@ -982,9 +982,9 @@ "description": "An implementation of PSR UriInterface ", "support": { "issues": "https://github.com/byjg/uri/issues", - "source": "https://github.com/byjg/uri/tree/2.1.2" + "source": "https://github.com/byjg/uri/tree/2.1.3" }, - "time": "2022-03-15T16:41:39+00:00" + "time": "2022-04-27T20:12:24+00:00" }, { "name": "byjg/webrequest", @@ -4423,16 +4423,16 @@ }, { "name": "symfony/console", - "version": "v5.4.7", + "version": "v5.4.8", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "900275254f0a1a2afff1ab0e11abd5587a10e1d6" + "reference": "ffe3aed36c4d60da2cf1b0a1cee6b8f2e5fa881b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/900275254f0a1a2afff1ab0e11abd5587a10e1d6", - "reference": "900275254f0a1a2afff1ab0e11abd5587a10e1d6", + "url": "https://api.github.com/repos/symfony/console/zipball/ffe3aed36c4d60da2cf1b0a1cee6b8f2e5fa881b", + "reference": "ffe3aed36c4d60da2cf1b0a1cee6b8f2e5fa881b", "shasum": "" }, "require": { @@ -4502,7 +4502,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v5.4.7" + "source": "https://github.com/symfony/console/tree/v5.4.8" }, "funding": [ { @@ -4518,7 +4518,7 @@ "type": "tidelift" } ], - "time": "2022-03-31T17:09:19+00:00" + "time": "2022-04-12T16:02:29+00:00" }, { "name": "symfony/event-dispatcher", @@ -4750,16 +4750,16 @@ }, { "name": "symfony/finder", - "version": "v5.4.3", + "version": "v5.4.8", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "231313534dded84c7ecaa79d14bc5da4ccb69b7d" + "reference": "9b630f3427f3ebe7cd346c277a1408b00249dad9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/231313534dded84c7ecaa79d14bc5da4ccb69b7d", - "reference": "231313534dded84c7ecaa79d14bc5da4ccb69b7d", + "url": "https://api.github.com/repos/symfony/finder/zipball/9b630f3427f3ebe7cd346c277a1408b00249dad9", + "reference": "9b630f3427f3ebe7cd346c277a1408b00249dad9", "shasum": "" }, "require": { @@ -4793,7 +4793,7 @@ "description": "Finds files and directories via an intuitive fluent interface", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/finder/tree/v5.4.3" + "source": "https://github.com/symfony/finder/tree/v5.4.8" }, "funding": [ { @@ -4809,7 +4809,7 @@ "type": "tidelift" } ], - "time": "2022-01-26T16:34:36+00:00" + "time": "2022-04-15T08:07:45+00:00" }, { "name": "symfony/options-resolver", @@ -5370,16 +5370,16 @@ }, { "name": "symfony/process", - "version": "v5.4.7", + "version": "v5.4.8", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "38a44b2517b470a436e1c944bf9b9ba3961137fb" + "reference": "597f3fff8e3e91836bb0bd38f5718b56ddbde2f3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/38a44b2517b470a436e1c944bf9b9ba3961137fb", - "reference": "38a44b2517b470a436e1c944bf9b9ba3961137fb", + "url": "https://api.github.com/repos/symfony/process/zipball/597f3fff8e3e91836bb0bd38f5718b56ddbde2f3", + "reference": "597f3fff8e3e91836bb0bd38f5718b56ddbde2f3", "shasum": "" }, "require": { @@ -5412,7 +5412,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v5.4.7" + "source": "https://github.com/symfony/process/tree/v5.4.8" }, "funding": [ { @@ -5428,7 +5428,7 @@ "type": "tidelift" } ], - "time": "2022-03-18T16:18:52+00:00" + "time": "2022-04-08T05:07:18+00:00" }, { "name": "symfony/service-contracts", @@ -5577,16 +5577,16 @@ }, { "name": "symfony/string", - "version": "v5.4.3", + "version": "v5.4.8", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "92043b7d8383e48104e411bc9434b260dbeb5a10" + "reference": "3c061a76bff6d6ea427d85e12ad1bb8ed8cd43e8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/92043b7d8383e48104e411bc9434b260dbeb5a10", - "reference": "92043b7d8383e48104e411bc9434b260dbeb5a10", + "url": "https://api.github.com/repos/symfony/string/zipball/3c061a76bff6d6ea427d85e12ad1bb8ed8cd43e8", + "reference": "3c061a76bff6d6ea427d85e12ad1bb8ed8cd43e8", "shasum": "" }, "require": { @@ -5643,7 +5643,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v5.4.3" + "source": "https://github.com/symfony/string/tree/v5.4.8" }, "funding": [ { @@ -5659,7 +5659,7 @@ "type": "tidelift" } ], - "time": "2022-01-02T09:53:40+00:00" + "time": "2022-04-19T10:40:37+00:00" }, { "name": "symfony/yaml", @@ -5788,16 +5788,16 @@ }, { "name": "vimeo/psalm", - "version": "4.22.0", + "version": "4.23.0", "source": { "type": "git", "url": "https://github.com/vimeo/psalm.git", - "reference": "fc2c6ab4d5fa5d644d8617089f012f3bb84b8703" + "reference": "f1fe6ff483bf325c803df9f510d09a03fd796f88" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vimeo/psalm/zipball/fc2c6ab4d5fa5d644d8617089f012f3bb84b8703", - "reference": "fc2c6ab4d5fa5d644d8617089f012f3bb84b8703", + "url": "https://api.github.com/repos/vimeo/psalm/zipball/f1fe6ff483bf325c803df9f510d09a03fd796f88", + "reference": "f1fe6ff483bf325c803df9f510d09a03fd796f88", "shasum": "" }, "require": { @@ -5822,6 +5822,7 @@ "php": "^7.1|^8", "sebastian/diff": "^3.0 || ^4.0", "symfony/console": "^3.4.17 || ^4.1.6 || ^5.0 || ^6.0", + "symfony/polyfill-php80": "^1.25", "webmozart/path-util": "^2.3" }, "provide": { @@ -5888,9 +5889,9 @@ ], "support": { "issues": "https://github.com/vimeo/psalm/issues", - "source": "https://github.com/vimeo/psalm/tree/4.22.0" + "source": "https://github.com/vimeo/psalm/tree/4.23.0" }, - "time": "2022-02-24T20:34:05+00:00" + "time": "2022-04-28T17:35:49+00:00" }, { "name": "webmozart/assert", diff --git a/lib/Migration/Version2040Date20211027183759.php b/lib/Migration/Version2040Date20211027183759.php index 8d83d48609..d5ad0911b0 100644 --- a/lib/Migration/Version2040Date20211027183759.php +++ b/lib/Migration/Version2040Date20211027183759.php @@ -6,13 +6,10 @@ use Closure; use Doctrine\DBAL\Types\Types; -use OC\SystemConfig; -use OCA\Libresign\AppInfo\Application; -use OCA\Libresign\Command\Install; +use OCA\Libresign\Service\PdfParserService; use OCP\DB\ISchemaWrapper; use OCP\Files\File; use OCP\Files\IRootFolder; -use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; @@ -22,28 +19,20 @@ class Version2040Date20211027183759 extends SimpleMigrationStep { /** @var IRootFolder*/ private $root; - /** @var IDBConnection */ - private $connection; - /** @var Install */ - private $install; - /** @var IConfig */ - private $config; - /** @var SystemConfig */ - private $systemConfig; + /** @var PdfParserService */ + private $PdfParserService; /** @var array */ private $rows; - public function __construct(IRootFolder $root, - IDBConnection $connection, - IRootFolder $rootfolder, - Install $install, - IConfig $config, - SystemConfig $systemConfig) { + public function __construct(IDBConnection $connection, + IRootFolder $root, + PdfParserService $PdfParserService) { $this->connection = $connection; $this->install = $install; $this->config = $config; $this->systemConfig = $systemConfig; $this->rootFolder = $rootfolder; $this->root = $root; + $this->PdfParserService = $PdfParserService; } public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void { @@ -81,7 +70,7 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array /** @var File[] */ $file = $userFolder->getById($row['node_id']); if (count($file) >= 1) { - $data = $this->getMetadataFromCli($cli, $file[0]->getPath()); + $data = $this->PdfParserService->getMetadata($file[0]->getPath()); $json = json_encode($data); $query = $this->connection->getQueryBuilder(); $query @@ -89,7 +78,7 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array ->set('metadata', $query->createNamedParameter($json)) ->where($query->expr()->eq('id', $query->createNamedParameter($row['id']))); - $query->execute(); + $query->executeStatement(); } } } diff --git a/lib/Service/InstallService.php b/lib/Service/InstallService.php new file mode 100644 index 0000000000..78dde44dda --- /dev/null +++ b/lib/Service/InstallService.php @@ -0,0 +1,337 @@ +tempManager = $tempManager; + $this->clientService = $clientService; + $this->config = $config; + $this->systemConfig = $systemConfig; + $this->rootFolder = $rootFolder; + } + + public function setOutput(OutputInterface $output): void { + $this->output = $output; + } + + public function getFolder($path = ''): Folder { + $rootFolder = $this->getAppRootFolder(); + try { + $folder = $rootFolder->newFolder(Application::APP_ID . DIRECTORY_SEPARATOR . $path); + } catch (\Throwable $th) { + $folder = $rootFolder->get(Application::APP_ID . DIRECTORY_SEPARATOR . $path); + } + return $folder; + } + + private function getAppDataFolderName(): string { + $instanceId = $this->systemConfig->getValue('instanceid', null); + if ($instanceId === null) { + throw new \RuntimeException('no instance id!'); + } + + return 'appdata_' . $instanceId; + } + + private function getDataDir(): string { + $dataDir = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/'); + return $dataDir; + } + + private function getAppRootFolder(): Folder { + $path = $this->getAppDataFolderName(); + try { + $folder = $this->rootFolder->get($path); + } catch (\Throwable $th) { + $folder = $this->rootFolder->newFolder($path); + } + return $folder; + } + + public function getFullPath(): string { + $folder = $this->getFolder(); + return $this->getDataDir() . '/' . $folder->getInternalPath(); + } + + public function installJava(): void { + $extractDir = $this->getFullPath(); + + /** + * To update: + * Check the compatible version of Java to use JSignPdf and update all the follow data + * URL used to get the MD5 and URL to download: + * https://jdk.java.net/java-se-ri/8-MR3 + */ + if (PHP_OS_FAMILY === 'Windows') { + $url = 'https://download.java.net/openjdk/jdk8u41/ri/openjdk-8u41-b04-windows-i586-14_jan_2020.zip'; + $tempFile = $this->tempManager->getTemporaryFile('.zip'); + $executableExtension = '.exe'; + $class = ZIP::class; + $md5 = '48ac2152d1fb0ad1d343104be210d532'; + } else { + $url = 'https://download.java.net/openjdk/jdk8u41/ri/openjdk-8u41-b04-linux-x64-14_jan_2020.tar.gz'; + $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); + $executableExtension = ''; + $class = TAR::class; + $md5 = '35f515e9436f4fefad091db2c1450c5f'; + } + + $this->download($url, 'java', $tempFile, $md5); + + $extractor = new $class($tempFile); + $extractor->extract($extractDir); + + $this->config->setAppValue(Application::APP_ID, 'java_path', $extractDir . '/java-se-8u41-ri/bin/java' . $executableExtension); + } + + public function uninstallJava(): void { + $javaPath = $this->config->getAppValue(Application::APP_ID, 'java_path'); + if (!$javaPath) { + return; + } + $appFolder = $this->getAppRootFolder(); + $name = $appFolder->getName(); + // Remove prefix + $path = explode($name, $javaPath)[1]; + // Remove binary path + $path = explode(DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR, $path)[0]; + try { + $folder = $appFolder->get($path); + $folder->delete(); + } catch (NotFoundException $e) { + } + $this->config->deleteAppValue(Application::APP_ID, 'java_path'); + } + + public function installJSignPdf(): void { + if (!extension_loaded('zip')) { + throw new RuntimeException('Zip extension is not available'); + } + $extractDir = $this->getFullPath(); + + $tempFile = $this->tempManager->getTemporaryFile('.zip'); + $url = 'https://sourceforge.net/projects/jsignpdf/files/stable/JSignPdf%20' . JSignPdfHandler::VERSION . '/jsignpdf-' . JSignPdfHandler::VERSION . '.zip'; + + $this->download($url, 'JSignPdf', $tempFile); + + $zip = new ZIP($tempFile); + $zip->extract($extractDir); + + $fullPath = $extractDir . DIRECTORY_SEPARATOR. 'jsignpdf-' . JSignPdfHandler::VERSION . DIRECTORY_SEPARATOR. 'JSignPdf.jar'; + $this->config->setAppValue(Application::APP_ID, 'jsignpdf_jar_path', $fullPath); + } + + public function uninstallJSignPdf(): void { + $jsignpdJarPath = $this->config->getAppValue(Application::APP_ID, 'jsignpdf_jar_path'); + if (!$jsignpdJarPath) { + return; + } + $appFolder = $this->getAppRootFolder(); + $name = $appFolder->getName(); + // Remove prefix + $path = explode($name, $jsignpdJarPath)[1]; + // Remove sufix + $path = trim($path, DIRECTORY_SEPARATOR . 'JSignPdf.jar'); + try { + $folder = $appFolder->get($path); + $folder->delete(); + } catch (NotFoundException $e) { + } + $this->config->deleteAppValue(Application::APP_ID, 'jsignpdf_jar_path'); + } + + public function installCli(): void { + $folder = $this->getFolder(); + + if (PHP_OS_FAMILY === 'Windows') { + throw new \RuntimeException('LibreSign CLI do not work in Windows!'); + } elseif (PHP_OS_FAMILY === 'Darwin') { + $url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_arm64'; + } elseif (PHP_OS_FAMILY === 'Linux') { + if (PHP_INT_SIZE === 4) { + $url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_i386'; + } else { + $url = 'https://github.com/LibreSign/libresign-cli/releases/download/v0.0.4/libresign_0.0.4_Linux_x86_64'; + } + } + $file = $folder->newFile('libresign-cli'); + $fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $file->getInternalPath(); + + $this->download($url, 'libresign-cli', $fullPath); + + if (PHP_OS_FAMILY !== 'Windows') { + chmod($fullPath, 0700); + } + + $this->config->setAppValue(Application::APP_ID, 'libresign_cli_path', $fullPath); + } + + public function uninstallCli(): void { + $libresignCliPath = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path'); + if (!$libresignCliPath) { + return; + } + $appFolder = $this->getAppRootFolder(); + $name = $appFolder->getName(); + // Remove prefix + $path = explode($name, $libresignCliPath)[1]; + try { + $folder = $appFolder->get($path); + $folder->delete(); + } catch (NotFoundException $e) { + } + $this->config->deleteAppValue(Application::APP_ID, 'libresign_cli_path'); + } + + public function installCfssl(): void { + $folder = $this->getFolder(); + + if (PHP_OS_FAMILY === 'Windows') { + $downloads = [ + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssl_1.6.1_windows_amd64.exe', + 'destination' => 'cfssl.exe', + ], + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssljson_1.6.1_windows_amd64.exe', + 'destination' => 'cfssljson.exe', + ], + ]; + } elseif (PHP_OS_FAMILY === 'Darwin') { + $downloads = [ + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssl_1.6.1_darwin_amd64', + 'destination' => 'cfssl', + ], + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssljson_1.6.1_darwin_amd64', + 'destination' => 'cfssljson', + ], + ]; + } else { + $downloads = [ + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssl_1.6.1_linux_amd64', + 'destination' => 'cfssl', + ], + [ + 'url' => 'https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssljson_1.6.1_linux_amd64', + 'destination' => 'cfssljson', + ], + ]; + } + foreach ($downloads as $download) { + $file = $folder->newFile($download['destination']); + $fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $file->getInternalPath(); + + $this->download($download['url'], $download['destination'], $fullPath); + + if (PHP_OS_FAMILY !== 'Windows') { + chmod($fullPath, 0700); + } + } + + $this->config->setAppValue(Application::APP_ID, 'cfssl_bin', 1); + } + + public function uninstallCfssl(): void { + $cfsslPath = $this->config->getAppValue(Application::APP_ID, 'cfssl_bin'); + if (!$cfsslPath) { + return; + } + $appFolder = $this->getAppRootFolder(); + $name = $appFolder->getName(); + // Remove prefix + $path = explode($name, $cfsslPath)[1]; + try { + $folder = $appFolder->get($path); + $folder->delete(); + } catch (NotFoundException $e) { + } + $this->config->deleteAppValue(Application::APP_ID, 'cfssl_bin'); + } + + protected function download(string $url, string $filename, string $path, ?string $md5 = '') { + if (php_sapi_name() === 'cli' && $this->output instanceof OutputInterface) { + $this->downloadCli($url, $filename, $path, $md5); + return; + } + $client = $this->clientService->newClient(); + try { + $client->get($url, [ + 'sink' => $path, + 'timeout' => 0 + ]); + } catch (\Exception $e) { + throw new LibresignException('Failure on download ' . $filename . " try again.\n" . $e->getMessage()); + } + if ($md5 && file_exists($path) && md5_file($path) !== $md5) { + throw new LibresignException('Failure on download ' . $filename . ' try again. Invalid md5.'); + } + } + + protected function downloadCli(string $url, string $filename, string $path, ?string $md5 = '') { + $client = $this->clientService->newClient(); + $progressBar = new ProgressBar($this->output); + $this->output->writeln('Downloading ' . $filename . '...'); + $progressBar->start(); + try { + $client->get($url, [ + 'sink' => $path, + 'timeout' => 0, + 'progress' => function ($downloadSize, $downloaded) use ($progressBar) { + $progressBar->setMaxSteps($downloadSize); + $progressBar->setProgress($downloaded); + }, + ]); + } catch (\Exception $e) { + $this->output->writeln('Failure on download ' . $filename . ' try again.'); + $this->output->writeln('' . $e->getMessage() . ''); + } + $progressBar->finish(); + $this->output->writeln(''); + $progressBar->finish(); + if ($md5 && file_exists($path) && md5_file($path) !== $md5) { + $this->output->writeln('Failure on download ' . $filename . ' try again'); + $this->output->writeln('Invalid MD5'); + } + } +} diff --git a/lib/Service/PdfParserService.php b/lib/Service/PdfParserService.php new file mode 100644 index 0000000000..1eaa389ce3 --- /dev/null +++ b/lib/Service/PdfParserService.php @@ -0,0 +1,58 @@ +systemConfig = $systemConfig; + $this->config = $config; + $this->installService = $installService; + $this->cliPath = $this->getLibesignCli(); + } + + private function getDataDir(): string { + return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/'); + } + + public function getMetadata(string $filePath): array { + $fullPath = $this->getDataDir() . $filePath; + $json = shell_exec($this->cliPath . ' info ' . $fullPath); + $array = json_decode($json, true); + $output = [ + 'p' => count($array['pages']), + ]; + foreach ($array['pages'] as $page) { + $output['d'][] = [ + 'w' => $page['width'], + 'h' => $page['height'], + ]; + } + return $output; + } + + private function getLibesignCli(): string { + $path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path'); + if (!file_exists($path)) { + $this->installService->installCli(); + $path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path'); + } + return $path; + } +} diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index d17da02f6d..6cf95efa52 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -15,7 +15,6 @@ use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Handler\Pkcs7Handler; use OCA\Libresign\Handler\Pkcs12Handler; -use OCA\Libresign\Handler\TCPDILibresign; use OCA\Libresign\Helper\JSActions; use OCA\Libresign\Helper\ValidateHelper; use OCP\Accounts\IAccountManager; @@ -91,6 +90,10 @@ class SignFileService { private $eventDispatcher; /** @var IURLGenerator */ private $urlGenerator; + /** @var IMimeTypeDetector */ + private $mimeTypeDetector; + /** @var PdfParserService */ + private $pdfParserService; /** @var ITempManager */ private $tempManager; /** @var FileUserEntity */ @@ -129,6 +132,7 @@ public function __construct( FileElementService $fileElementService, IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, + PdfParserService $pdfParserService, IMimeTypeDetector $mimeTypeDetector, ITempManager $tempManager ) { @@ -156,6 +160,7 @@ public function __construct( $this->fileElementService = $fileElementService; $this->eventDispatcher = $eventDispatcher; $this->urlGenerator = $urlGenerator; + $this->pdfParserService = $pdfParserService; $this->mimeTypeDetector = $mimeTypeDetector; $this->tempManager = $tempManager; } @@ -232,9 +237,7 @@ public function getFileMetadata(\OCP\Files\Node $node): array { 'extension' => $node->getExtension(), ]; if ($metadata['extension'] === 'pdf') { - $pdf = new TCPDILibresign(); - $pdf->setSourceData($node->getContent()); - $metadata = array_merge($metadata, $pdf->getPagesMetadata()); + $metadata = $this->pdfParserService->getMetadata($node->getPath()); } return $metadata; } diff --git a/package.json b/package.json index 61efebb4cb..10506aebfc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "libresign", "description": "A app for signing documents", - "version": "3.1.0", + "version": "3.1.1", "license": "agpl", "private": true, "scripts": { diff --git a/tests/Unit/Service/SignFileServiceTest.php b/tests/Unit/Service/SignFileServiceTest.php index 0764f21288..1e067aefee 100644 --- a/tests/Unit/Service/SignFileServiceTest.php +++ b/tests/Unit/Service/SignFileServiceTest.php @@ -14,6 +14,7 @@ use OCA\Libresign\Service\FileElementService; use OCA\Libresign\Service\FolderService; use OCA\Libresign\Service\MailService; +use OCA\Libresign\Service\PdfParserService; use OCA\Libresign\Service\SignFileService; use OCP\Accounts\IAccountManager; use OCP\App\IAppManager; @@ -84,6 +85,8 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase { private $eventDispatcher; /** @var IURLGenerator|MockObject */ private $urlGenerator; + /** @var PdfParserService */ + private $pdfParserService; /** @var IMimeTypeDetector|MockObject */ private $mimeTypeDetector; /** @var ITempManager|MockObject */ @@ -118,6 +121,7 @@ public function setUp(): void { $this->fileElementService = $this->createMock(FileElementService::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->pdfParserService = $this->createMock(PdfParserService::class); $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class); $this->tempManager = $this->createMock(ITempManager::class); } @@ -148,6 +152,7 @@ private function getService(): SignFileService { $this->fileElementService, $this->eventDispatcher, $this->urlGenerator, + $this->pdfParserService, $this->mimeTypeDetector, $this->tempManager );