Skip to content

Commit 1a3e362

Browse files
SystemKeeperAltahrim
authored andcommitted
feat: Show download progress on updater.phar
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de> Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 81078ba commit 1a3e362

5 files changed

Lines changed: 28 additions & 7 deletions

File tree

index.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LogException extends \Exception {
2727
}
2828

2929

30+
use Closure;
3031
use CurlHandle;
3132

3233
class Updater {
@@ -38,6 +39,7 @@ class Updater {
3839
private ?string $requestID = null;
3940
private bool $disabled = false;
4041
private int $previousProgress = 0;
42+
private ?Closure $downloadProgress = null;
4143

4244
/**
4345
* Updater constructor
@@ -561,8 +563,9 @@ private function getUpdateServerResponse(): array {
561563
*
562564
* @throws \Exception
563565
*/
564-
public function downloadUpdate(string $url = ''): void {
566+
public function downloadUpdate(string $url = '', ?Closure $downloadProgress = null): void {
565567
$this->silentLog('[info] downloadUpdate()');
568+
$this->downloadProgress = $downloadProgress;
566569

567570
if ($url !== '') {
568571
// If a URL is provided, use it directly
@@ -726,6 +729,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
726729
// log every 2% increment for the first 10% then only log every 10% increment after that
727730
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
728731
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');
732+
733+
if ($this->downloadProgress) {
734+
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
735+
}
729736
}
730737
}
731738
}

lib/UpdateCommand.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
229229

230230
$output->write('[ ] ' . $this->checkTexts[$i] . ' ...');
231231

232-
$result = $this->executeStep($i);
232+
$result = $this->executeStep($i, $output);
233233

234234
// Move the cursor to the beginning of the line
235235
$output->write("\x0D");
@@ -387,7 +387,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
387387
/**
388388
* @return array{proceed:bool,response:string|list<string>} with options 'proceed' which is a boolean and defines if the step succeeded and an optional 'response' string or array
389389
*/
390-
protected function executeStep(int $step): array {
390+
protected function executeStep(int $step, OutputInterface $output): array {
391391
if ($this->updater === null) {
392392
return ['proceed' => false, 'response' => 'Initialization problem'];
393393
}
@@ -411,7 +411,14 @@ protected function executeStep(int $step): array {
411411
}
412412
break;
413413
case 4:
414-
$this->updater->downloadUpdate($this->urlOverride);
414+
// Ensure that we have the same number of characters, that we want to override in the progress method
415+
$output->write(str_pad(' 0%', 5, ' ', STR_PAD_LEFT));
416+
417+
$this->updater->downloadUpdate($this->urlOverride, function (int $progress, string $downloaded, string $download_size) use ($output) {
418+
// Move cursor 5 to the left and write the new progress
419+
$output->write("\x1B[5D");
420+
$output->write(str_pad(' ' . $progress . '%', 5, ' ', STR_PAD_LEFT));
421+
});
415422
break;
416423
case 5:
417424
$this->updater->verifyIntegrity($this->urlOverride);

lib/Updater.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace NC\Updater;
1111

12+
use Closure;
1213
use CurlHandle;
1314

1415
class Updater {
@@ -20,6 +21,7 @@ class Updater {
2021
private ?string $requestID = null;
2122
private bool $disabled = false;
2223
private int $previousProgress = 0;
24+
private ?Closure $downloadProgress = null;
2325

2426
/**
2527
* Updater constructor
@@ -543,8 +545,9 @@ private function getUpdateServerResponse(): array {
543545
*
544546
* @throws \Exception
545547
*/
546-
public function downloadUpdate(string $url = ''): void {
548+
public function downloadUpdate(string $url = '', ?Closure $downloadProgress = null): void {
547549
$this->silentLog('[info] downloadUpdate()');
550+
$this->downloadProgress = $downloadProgress;
548551

549552
if ($url !== '') {
550553
// If a URL is provided, use it directly
@@ -708,6 +711,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
708711
// log every 2% increment for the first 10% then only log every 10% increment after that
709712
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
710713
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');
714+
715+
if ($this->downloadProgress) {
716+
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
717+
}
711718
}
712719
}
713720
}

updater.phar

760 Bytes
Binary file not shown.

vendor/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'name' => '__root__',
44
'pretty_version' => 'dev-master',
55
'version' => 'dev-master',
6-
'reference' => 'f945e59e0fa3520198947aea311fbae50bcd308f',
6+
'reference' => 'b98632f814d1be055487cbe7763760d9846a5a93',
77
'type' => 'library',
88
'install_path' => __DIR__ . '/../../',
99
'aliases' => array(),
@@ -13,7 +13,7 @@
1313
'__root__' => array(
1414
'pretty_version' => 'dev-master',
1515
'version' => 'dev-master',
16-
'reference' => 'f945e59e0fa3520198947aea311fbae50bcd308f',
16+
'reference' => 'b98632f814d1be055487cbe7763760d9846a5a93',
1717
'type' => 'library',
1818
'install_path' => __DIR__ . '/../../',
1919
'aliases' => array(),

0 commit comments

Comments
 (0)