Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,20 +531,7 @@ private function getUpdateServerResponse(): array {
$this->silentLog('[info] updateURL: ' . $updateURL);

// Download update response
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $updateURL,
CURLOPT_USERAGENT => 'Nextcloud Updater',
]);

if ($this->getConfigOption('proxy') !== null) {
curl_setopt_array($curl, [
CURLOPT_PROXY => $this->getConfigOptionString('proxy'),
CURLOPT_PROXYUSERPWD => $this->getConfigOptionString('proxyuserpwd'),
CURLOPT_HTTPPROXYTUNNEL => $this->getConfigOption('proxy') ? 1 : 0,
]);
}
$curl = $this->getCurl($updateURL);

/** @var false|string $response */
$response = curl_exec($curl);
Expand Down Expand Up @@ -608,9 +595,35 @@ public function downloadUpdate(): void {
]);
}

return $ch;
}

private function downloadArchive(string $fromUrl, string $toLocation): bool {
$ch = $this->getCurl($fromUrl);

// see if there's an existing incomplete download to resume
if (is_file($toLocation)) {
$size = (int)filesize($toLocation);
$range = $size . '-';
curl_setopt($ch, CURLOPT_RANGE, $range);
$this->silentLog('[info] previous download found; resuming from ' . $this->formatBytes($size));
}

$fp = fopen($toLocation, 'ab');
if ($fp === false) {
throw new \Exception('Fail to open file in ' . $toLocation);
}

curl_setopt_array($ch, [
CURLOPT_NOPROGRESS => false,
CURLOPT_PROGRESSFUNCTION => [$this, 'downloadProgressCallback'],
CURLOPT_FILE => $fp,
]);

if (curl_exec($ch) === false) {
throw new \Exception('Curl error: ' . curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
$statusCodes = [
Expand Down Expand Up @@ -644,6 +657,15 @@ public function downloadUpdate(): void {
fclose($fp);

$this->silentLog('[info] end of downloadUpdate()');
return true;
}

/**
* Check if PHP is able to decompress archive format
*/
private function isAbleToDecompress(string $ext): bool {
// Only zip is supported for now
return $ext === 'zip' && extension_loaded($ext);
}

/**
Expand All @@ -655,13 +677,14 @@ private function getDownloadedFilePath(): string {

$filesInStorageLocation = scandir($storageLocation);
$files = array_values(array_filter($filesInStorageLocation, function (string $path) {
return $path !== '.' && $path !== '..';
// Match files with - in the name and extension (*-*.*)
return preg_match('/^.*-.*\..*$/i', $path);
}));
// only the downloaded archive
if (count($files) !== 1) {
throw new \Exception('There are more files than the downloaded archive in the downloads/ folder.');
}
return $storageLocation . '/' . $files[0];
return $storageLocation . $files[0];
}

/**
Expand Down
57 changes: 41 additions & 16 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace NC\Updater;

use CurlHandle;

class Updater {
private string $baseDir;
private array $configValues = [];
Expand Down Expand Up @@ -493,20 +495,7 @@ private function getUpdateServerResponse(): array {
$this->silentLog('[info] updateURL: ' . $updateURL);

// Download update response
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $updateURL,
CURLOPT_USERAGENT => 'Nextcloud Updater',
]);

if ($this->getConfigOption('proxy') !== null) {
curl_setopt_array($curl, [
CURLOPT_PROXY => $this->getConfigOptionString('proxy'),
CURLOPT_PROXYUSERPWD => $this->getConfigOptionString('proxyuserpwd'),
CURLOPT_HTTPPROXYTUNNEL => $this->getConfigOption('proxy') ? 1 : 0,
]);
}
$curl = $this->getCurl($updateURL);

/** @var false|string $response */
$response = curl_exec($curl);
Expand Down Expand Up @@ -570,9 +559,35 @@ public function downloadUpdate(): void {
]);
}

return $ch;
}

private function downloadArchive(string $fromUrl, string $toLocation): bool {
$ch = $this->getCurl($fromUrl);

// see if there's an existing incomplete download to resume
if (is_file($toLocation)) {
$size = (int)filesize($toLocation);
$range = $size . '-';
curl_setopt($ch, CURLOPT_RANGE, $range);
$this->silentLog('[info] previous download found; resuming from ' . $this->formatBytes($size));
}

$fp = fopen($toLocation, 'ab');
if ($fp === false) {
throw new \Exception('Fail to open file in ' . $toLocation);
}

curl_setopt_array($ch, [
CURLOPT_NOPROGRESS => false,
CURLOPT_PROGRESSFUNCTION => [$this, 'downloadProgressCallback'],
CURLOPT_FILE => $fp,
]);

if (curl_exec($ch) === false) {
throw new \Exception('Curl error: ' . curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
$statusCodes = [
Expand Down Expand Up @@ -606,6 +621,15 @@ public function downloadUpdate(): void {
fclose($fp);

$this->silentLog('[info] end of downloadUpdate()');
return true;
}

/**
* Check if PHP is able to decompress archive format
*/
private function isAbleToDecompress(string $ext): bool {
// Only zip is supported for now
return $ext === 'zip' && extension_loaded($ext);
}

/**
Expand All @@ -617,13 +641,14 @@ private function getDownloadedFilePath(): string {

$filesInStorageLocation = scandir($storageLocation);
$files = array_values(array_filter($filesInStorageLocation, function (string $path) {
return $path !== '.' && $path !== '..';
// Match files with - in the name and extension (*-*.*)
return preg_match('/^.*-.*\..*$/i', $path);
}));
// only the downloaded archive
if (count($files) !== 1) {
throw new \Exception('There are more files than the downloaded archive in the downloads/ folder.');
}
return $storageLocation . '/' . $files[0];
return $storageLocation . $files[0];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Feature: CLI updater
And there is an update to version 25.0.503 available
When the CLI updater is run
Then the return code should not be 0
And the output should contain "Download failed - Not Found (HTTP 404)"
And the output should contain "All downloads failed."
And the installed version should be 25.0.0
And maintenance mode should be off
And upgrade is not required
Expand Down