Skip to content

Commit

Permalink
chore: clean folder before download binaries
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Jun 5, 2024
1 parent 5ebf7de commit 35ade2e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/Service/Install/InstallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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
*/
Expand Down Expand Up @@ -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:
Expand All @@ -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);

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 = [
[
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/Install/SignSetupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35ade2e

Please sign in to comment.