From 1a5697ff3feb8ec62daabb63ef33173e6e03ea14 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 21 Jan 2020 13:03:05 +0300 Subject: [PATCH 1/2] update decompress code for new archive structure --- composer.json | 1 + src/Command/UpdateDatabaseCommand.php | 82 ++++++++++++++++++++------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index b403441..c7e6ac8 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ } }, "require": { + "ext-phar": "*", "php": ">=5.4.0", "geoip2/geoip2": "~2.0", "gpslab/compressor": "~1.0", diff --git a/src/Command/UpdateDatabaseCommand.php b/src/Command/UpdateDatabaseCommand.php index 65789e8..4f7bd94 100644 --- a/src/Command/UpdateDatabaseCommand.php +++ b/src/Command/UpdateDatabaseCommand.php @@ -26,11 +26,6 @@ class UpdateDatabaseCommand extends Command */ private $stopwatch; - /** - * @var CompressorInterface - */ - private $compressor; - /** * @var Filesystem */ @@ -39,12 +34,12 @@ class UpdateDatabaseCommand extends Command /** * @var string */ - private $url = ''; + private $url; /** * @var string */ - private $cache = ''; + private $cache; /** * @param Filesystem $fs @@ -59,8 +54,6 @@ public function __construct(Filesystem $fs, Stopwatch $stopwatch, CompressorInte $this->url = $url; $this->cache = $cache; $this->stopwatch = $stopwatch; - $this->compressor = $compressor; - parent::__construct(); } @@ -99,24 +92,75 @@ protected function execute(InputInterface $input, OutputInterface $output) $io->title('Update the GeoIP2 database'); $this->stopwatch->start('update'); - $tmp_zip = sys_get_temp_dir().DIRECTORY_SEPARATOR.basename(parse_url($url, PHP_URL_PATH)); - $tmp_unzip = sys_get_temp_dir().DIRECTORY_SEPARATOR.basename($target); + $tmp_zip = sys_get_temp_dir().'/GeoLite2.tar.gz'; + $tmp_unzip = sys_get_temp_dir().'/GeoLite2.tar'; + $tmp_untar = sys_get_temp_dir().'/GeoLite2'; - $io->comment(sprintf('Beginning download of file: %s', $url)); + // remove old files and folders for correct overwrite it + $this->fs->remove([$tmp_zip, $tmp_unzip, $tmp_untar]); - $this->fs->copy($url, $tmp_zip, true); + $io->comment(sprintf('Beginning download of file %s', $url)); - $io->comment('Download complete'); - $io->comment('De-compressing file'); + file_put_contents($tmp_zip, fopen($url, 'rb')); + + $io->comment(sprintf('Download complete to %s', $tmp_zip)); + $io->comment(sprintf('De-compressing file to %s', $tmp_unzip)); $this->fs->mkdir(dirname($target), 0777); - $this->compressor->uncompress($tmp_zip, $tmp_unzip); - $io->comment('Decompression complete'); + // decompress gz file + $phar = new \PharData($tmp_zip); + $phar->decompress(); - $this->fs->copy($tmp_unzip, $target, true); + $io->comment('Decompression complete'); + $io->comment(sprintf('Extract tar file to %s', $tmp_untar)); + + // extract tar archive + $phar = new \PharData($tmp_unzip); + $phar->extractTo($tmp_untar); + + $io->comment('Tar archive extracted'); + + // find database in archive + $database = ''; + foreach (scandir($tmp_untar) as $folder) { + $path = $tmp_untar.'/'.$folder; + + // find folder with database + // expected something like that "GeoLite2-City_20200114" + if ( + preg_match('/^(?.+)_(?\d{4})(?\d{2})(?\d{2})$/', $folder, $match) && + is_dir($path) + ) { + // find database in folder + // expected something like that "GeoLite2-City.mmdb" + foreach (scandir($path) as $filename) { + $file = $path.'/'.$filename; + + if (strpos($filename, $match['database']) === 0 && is_file($file)) { + $io->comment(sprintf( + 'Found %s database updated at %s-%s-%s', + $match['database'], + $match['year'], + $match['month'], + $match['day'] + )); + + $database = $file; + } + } + } + } + + if (!$database) { + throw new \RuntimeException('Not found GeoLite2 database in archive.'); + } + + $this->fs->copy($database, $target, true); $this->fs->chmod($target, 0777); - $this->fs->remove([$tmp_zip, $tmp_unzip]); + $this->fs->remove([$tmp_zip, $tmp_unzip, $tmp_untar]); + + $io->comment(sprintf('Database moved to %s', $target)); $io->success('Finished downloading'); From 68a570364eb570820f1fe525de819441db1f69f3 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 21 Jan 2020 13:17:50 +0300 Subject: [PATCH 2/2] disable yoda_style CS rule --- .styleci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.styleci.yml b/.styleci.yml index da7ee66..402892e 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -5,3 +5,4 @@ enabled: disabled: - phpdoc_align + - yoda_style