diff --git a/lib/Service/Install/InstallService.php b/lib/Service/Install/InstallService.php index 6caf46bac1..8e69ff7b0f 100644 --- a/lib/Service/Install/InstallService.php +++ b/lib/Service/Install/InstallService.php @@ -23,6 +23,7 @@ use OCA\Libresign\Handler\JSignPdfHandler; use OCP\AppFramework\Services\IAppConfig; use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\Folder; use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -87,7 +88,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 +104,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 +123,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 +355,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 +382,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 +436,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 +484,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 +544,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