From 35ade2e67634d0d442c79de7b8b01d51b86f8bca Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Wed, 5 Jun 2024 16:31:22 -0300 Subject: [PATCH] chore: clean folder before download binaries Signed-off-by: Vitor Mattos --- lib/Service/Install/InstallService.php | 29 ++++++++++++++++-------- lib/Service/Install/SignSetupService.php | 3 +++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/Service/Install/InstallService.php b/lib/Service/Install/InstallService.php index 6caf46bac1..e4fdf3e0dc 100644 --- a/lib/Service/Install/InstallService.php +++ b/lib/Service/Install/InstallService.php @@ -87,7 +87,7 @@ public function setArchitecture(string $architecture): void { $this->architecture = $architecture; } - private function getFolder(string $path = '', ?ISimpleFolder $folder = null): ISimpleFolder { + private function getFolder(string $path = '', ?ISimpleFolder $folder = null, $needToBeEmpty = false): ISimpleFolder { if (!$folder) { $folder = $this->appData->getFolder('/'); if (!$path) { @@ -103,6 +103,10 @@ private function getFolder(string $path = '', ?ISimpleFolder $folder = null): IS } try { $folder = $folder->getFolder($path, $folder); + if ($needToBeEmpty) { + $folder->delete(); + throw new \Exception('Need to be empty'); + } } catch (\Throwable $th) { try { $folder = $folder->newFolder($path); @@ -118,6 +122,10 @@ private function getFolder(string $path = '', ?ISimpleFolder $folder = null): IS return $folder; } + private function getEmptyFolder(string $path): ISimpleFolder { + return $this->getFolder($path, null, true); + } + /** * @todo check a best solution to don't use reflection */ @@ -346,8 +354,8 @@ public function installJava(?bool $async = false): void { $this->runAsync(); return; } + $folder = $this->getEmptyFolder($this->resource); $extractDir = $this->getFullPath() . '/' . $this->resource; - $javaFolder = $this->getFolder($this->resource); /** * Steps to update: @@ -373,9 +381,9 @@ public function installJava(?bool $async = false): void { $checksumUrl = $url . '.sha256.txt'; $hash = $this->getHash($compressedFileName, $checksumUrl); try { - $compressedFile = $javaFolder->getFile($compressedFileName); + $compressedFile = $folder->getFile($compressedFileName); } catch (NotFoundException $th) { - $compressedFile = $javaFolder->newFile($compressedFileName); + $compressedFile = $folder->newFile($compressedFileName); } $comporessedInternalFileName = $this->getDataDir() . '/' . $this->getInternalPathOfFile($compressedFile); @@ -427,13 +435,14 @@ public function installJSignPdf(?bool $async = false): void { $this->runAsync(); return; } + $folder = $this->getEmptyFolder($this->resource); $extractDir = $this->getFullPath() . '/' . $this->resource; $compressedFileName = 'jsignpdf-' . JSignPdfHandler::VERSION . '.zip'; try { - $compressedFile = $this->getFolder($this->resource)->getFile($compressedFileName); + $compressedFile = $folder->getFile($compressedFileName); } catch (\Throwable $th) { - $compressedFile = $this->getFolder($this->resource)->newFile($compressedFileName); + $compressedFile = $folder->newFile($compressedFileName); } $comporessedInternalFileName = $this->getDataDir() . '/' . $this->getInternalPathOfFile($compressedFile); $url = 'https://github.com/intoolswetrust/jsignpdf/releases/download/JSignPdf_' . str_replace('.', '_', JSignPdfHandler::VERSION) . '/jsignpdf-' . JSignPdfHandler::VERSION . '.zip'; @@ -474,11 +483,11 @@ public function installPdftk(?bool $async = false): void { $this->runAsync(); return; } - + $folder = $this->getEmptyFolder($this->resource); try { - $file = $this->getFolder($this->resource)->getFile('pdftk.jar'); + $file = $folder->getFile('pdftk.jar'); } catch (\Throwable $th) { - $file = $this->getFolder($this->resource)->newFile('pdftk.jar'); + $file = $folder->newFile('pdftk.jar'); } $fullPath = $this->getDataDir() . '/' . $this->getInternalPathOfFile($file); $url = 'https://gitlab.com/api/v4/projects/5024297/packages/generic/pdftk-java/v' . self::PDFTK_VERSION . '/pdftk-all.jar'; @@ -534,7 +543,7 @@ public function installCfssl(?bool $async = false): void { } private function installCfsslByArchitecture(string $arcitecture): void { - $folder = $this->getFolder($this->resource); + $folder = $this->getEmptyFolder($this->resource); $downloads = [ [ diff --git a/lib/Service/Install/SignSetupService.php b/lib/Service/Install/SignSetupService.php index 4d208859b7..9c09ee3a99 100644 --- a/lib/Service/Install/SignSetupService.php +++ b/lib/Service/Install/SignSetupService.php @@ -262,6 +262,9 @@ private function getInstallPath(): string { * @throws \Exception */ private function getFolderIterator(string $folderToIterate): \RecursiveIteratorIterator { + if (!is_dir($folderToIterate)) { + throw new InvalidSignatureException('No such directory ' . $folderToIterate); + } $dirItr = new \RecursiveDirectoryIterator( $folderToIterate, \RecursiveDirectoryIterator::SKIP_DOTS