Skip to content

Commit

Permalink
Extract into current location. Revert if unsuccessful. Download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Dec 1, 2015
1 parent 418ff52 commit 642a7db
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
35 changes: 29 additions & 6 deletions src/Command/DetectCommand.php
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use GuzzleHttp\Event\ProgressEvent;
use Owncloud\Updater\Utils\Fetcher;
use Owncloud\Updater\Utils\Feed;
use Owncloud\Updater\Utils\ConfigReader;
Expand Down Expand Up @@ -48,6 +49,9 @@ protected function configure(){
}

protected function execute(InputInterface $input, OutputInterface $output){
$container = $this->getApplication()->getContainer();
$locator = $container['utils.locator'];
$fsHelper = $container['utils.filesystemhelper'];
try{
$currentVersion = $this->configReader->getByPath('system.version');
if (!strlen($currentVersion)){
Expand All @@ -63,7 +67,14 @@ protected function execute(InputInterface $input, OutputInterface $output){
$path = $this->fetcher->getBaseDownloadPath($feed);
$fileExists = $this->isCached($feed, $output);
if (!$fileExists){
$this->fetcher->getOwncloud($feed);
$this->fetcher->getOwncloud($feed, function (ProgressEvent $e) use ($output) {
$percentString = '';
if ($e->downloadSize){
$percent = intval(100* $e->downloaded / $e->downloadSize );
$percentString = $percent . '%';
}
$output->write( 'Downloaded ' . $percentString . ' (' . $e->downloaded . ' of ' . $e->downloadSize . ")\r");
});
if (md5_file($path) !== $this->fetcher->getMd5($feed)){
$output->writeln('Downloaded ' . $feed->getDownloadedFileName() . '. Checksum is incorrect.');
@unlink($path);
Expand All @@ -72,18 +83,30 @@ protected function execute(InputInterface $input, OutputInterface $output){
}
}
if ($fileExists){
$zipExtractor = new ZipExtractor(
$path, '/tmp'
);
$zipExtractor->extract();
$fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion();
if (!file_exists($fullExtractionPath)){
try {
$fsHelper->mkdir($fullExtractionPath, true);
} catch (\Exception $ex) {
$output->writeln('Unable create directory ' . $fullExtractionPath);
}
}
$output->writeln('Extracting source into ' . $fullExtractionPath);

$zipExtractor = new ZipExtractor($path, $fullExtractionPath);
try {
$zipExtractor->extract();
} catch (\Exception $ex) {
$output->writeln('Extraction has been failed');
$fsHelper->removeIfExists($locator->getExtractionBaseDir());
}
}
} else {
$output->writeln('No updates found');
}
} catch (\Exception $e){
$this->getApplication()->getLogger()->error($e->getMessage());
}
$output->writeln($this->getDescription());
}

public function isCached(Feed $feed, OutputInterface $output){
Expand Down
20 changes: 14 additions & 6 deletions src/Utils/Fetcher.php
Expand Up @@ -3,7 +3,7 @@
namespace Owncloud\Updater\Utils;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Event\ProgressEvent;
use Owncloud\Updater\Utils\Feed;

class Fetcher {
Expand Down Expand Up @@ -46,16 +46,23 @@ public function __construct(Client $httpClient, Locator $locator, ConfigReader $
/**
* Download new owncloud package
* @param Feed $feed
* @param Callable $onProgress
* @throws \UnexpectedValueException
*/
public function getOwncloud(Feed $feed){
public function getOwncloud(Feed $feed, callable $onProgress){
if ($feed->isValid()){
$downloadPath = $this->getBaseDownloadPath($feed);
$url = $feed->getUrl();
$response = $this->httpClient->get($url, [
'save_to' => $downloadPath,
'timeout' => 600
]);
$request = $this->httpClient->createRequest(
'GET',
$url,
[
'save_to' => $downloadPath,
'timeout' => 600
]
);
$request->getEmitter()->on('progress', $onProgress);
$response = $this->httpClient->send($request);
if ($response->getStatusCode() !== 200){
throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
}
Expand Down Expand Up @@ -113,6 +120,7 @@ public function getFeed(){
*/
protected function getFeedUrl(){
$currentVersion = $this->configReader->getByPath('system.version');
$currentVersion = '8.2.0.1';
$version = explode('.', $currentVersion);
$version['installed'] = $this->configReader->getByPath('apps.core.installedat');
$version['updated'] = $this->configReader->getByPath('apps.core.lastupdatedat');
Expand Down
31 changes: 31 additions & 0 deletions src/Utils/FilesystemHelper.php
Expand Up @@ -83,4 +83,35 @@ public function checkr($src, $collection){
}
}

public function removeIfExists($path) {
if (!file_exists($path)) {
return;
}

if (is_dir($path)) {
$this->rmdirr($path);
} else {
@unlink($path);
}
}

protected function rmdirr($dir) {
if(is_dir($dir)) {
$files = scandir($dir);
foreach($files as $file) {
if ($file != "." && $file != "..") {
$this->rmdirr("$dir/$file");
}
}
@rmdir($dir);
}elseif(file_exists($dir)) {
@unlink($dir);
}
if(file_exists($dir)) {
return false;
}else{
return true;
}
}

}
4 changes: 4 additions & 0 deletions src/Utils/Locator.php
Expand Up @@ -76,6 +76,10 @@ public function getDownloadBaseDir(){
return $this->getDataDir() . '/download';
}

public function getExtractionBaseDir(){
return $this->owncloudRootPath . "/_oc_upgrade";
}

/**
*
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ZipExtractor.php
Expand Up @@ -45,7 +45,7 @@ private function extractZipArchive(){
throw new \RuntimeException("There was an error extracting the ZIP file. Corrupt file?");
}

$zipArchive->close();
return true;
}

protected function getErrorMessage($retval){
Expand Down

0 comments on commit 642a7db

Please sign in to comment.