Skip to content

Commit

Permalink
Improve archive download
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Dec 20, 2024
1 parent 693f301 commit 6b9604a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
41 changes: 30 additions & 11 deletions classes/Task/Update/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
namespace PrestaShop\Module\AutoUpgrade\Task\Update;

use Exception;
use PrestaShop\Module\AutoUpgrade\Exceptions\DistributionApiException;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
Expand All @@ -41,6 +43,7 @@
class Download extends AbstractTask
{
const TASK_TYPE = TaskType::TASK_TYPE_UPDATE;
const MAX_DOWNLOAD_TRY = 3;

/**
* @throws Exception
Expand All @@ -58,9 +61,7 @@ public function run(): int
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$upgrader = $this->container->getUpgrader();

$this->logger->debug($this->translator->trans('Downloading from %s', [$upgrader->getOnlineDestinationRelease()->getZipDownloadUrl()]));
$this->logger->debug($this->translator->trans('Downloading from %s', [$this->container->getUpgrader()->getOnlineDestinationRelease()->getZipDownloadUrl()]));
$this->logger->debug($this->translator->trans('File will be saved in %s', [$this->container->getFilePath()]));
if (file_exists($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH))) {
FilesystemAdapter::deleteDirectory($this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH), false);
Expand All @@ -69,30 +70,48 @@ public function run(): int
$report = '';
$relative_download_path = str_replace(_PS_ROOT_DIR_, '', $this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH));
if (\ConfigurationTest::test_dir($relative_download_path, false, $report)) {
$res = $upgrader->downloadLast(
$this->downloadArchive();
} else {
$this->logger->error($this->translator->trans('Download directory %s is not writable.', [$this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH)]));
$this->next = TaskName::TASK_ERROR;
}

return $this->next == TaskName::TASK_ERROR ? ExitCode::FAIL : ExitCode::SUCCESS;
}

/**
* @throws DistributionApiException
* @throws UpgradeException
* @throws Exception
*/
public function downloadArchive(): void
{
$attempt = 0;
$downloadSuccess = false;

while ($attempt < self::MAX_DOWNLOAD_TRY && !$downloadSuccess) {
++$attempt;
$res = $this->container->getUpgrader()->downloadLast(
$this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH),
$this->container->getProperty(UpgradeContainer::ARCHIVE_FILENAME)
);
if ($res) {
$md5file = md5_file(realpath($this->container->getProperty(UpgradeContainer::ARCHIVE_FILEPATH)));
if ($md5file == $upgrader->getOnlineDestinationRelease()->getZipMd5()) {
if ($md5file == $this->container->getUpgrader()->getOnlineDestinationRelease()->getZipMd5()) {
$this->next = TaskName::TASK_UNZIP;
$this->logger->debug($this->translator->trans('Download complete.'));
$this->logger->info($this->translator->trans('Download complete. Now extracting...'));
$downloadSuccess = true;
} else {
$this->logger->error($this->translator->trans('Download complete but MD5 sum does not match (%s).', [$md5file]));
$this->logger->info($this->translator->trans('Download complete but MD5 sum does not match (%s). Operation aborted.', [$md5file]));
$this->next = TaskName::TASK_ERROR;
break;
}
} else {
$this->logger->error($this->translator->trans('Error during download'));
$this->logger->error($this->translator->trans('The .zip archive could not be downloaded. The update is currently impossible. Please try again later.'));
$this->next = TaskName::TASK_ERROR;
}
} else {
$this->logger->error($this->translator->trans('Download directory %s is not writable.', [$this->container->getProperty(UpgradeContainer::DOWNLOAD_PATH)]));
$this->next = TaskName::TASK_ERROR;
}

return $this->next == TaskName::TASK_ERROR ? ExitCode::FAIL : ExitCode::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion classes/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function downloadLast(string $dest, string $filename): bool
Tools14::copy($this->onlineDestinationRelease->getZipDownloadUrl(), $destPath);
}

return is_file($destPath);
return is_file($destPath) && filesize($destPath) > 0;
}

/**
Expand Down

0 comments on commit 6b9604a

Please sign in to comment.