|
7 | 7 | use Fig\Http\Message\RequestMethodInterface as RequestMethod; |
8 | 8 | use GuzzleHttp\ClientInterface; |
9 | 9 | use GuzzleHttp\RequestOptions; |
10 | | -use PharData; |
11 | 10 | use Shlinkio\Shlink\IpGeolocation\Exception\DbUpdateException; |
12 | 11 | use Shlinkio\Shlink\IpGeolocation\Exception\MissingLicenseException; |
13 | 12 | use Symfony\Component\Filesystem\Exception as FilesystemException; |
14 | 13 | use Symfony\Component\Filesystem\Filesystem; |
15 | 14 | use Throwable; |
16 | 15 |
|
| 16 | +use function fclose; |
| 17 | +use function feof; |
| 18 | +use function fopen; |
| 19 | +use function fread; |
| 20 | +use function fseek; |
| 21 | +use function fwrite; |
| 22 | +use function gzclose; |
| 23 | +use function gzopen; |
| 24 | +use function gzread; |
| 25 | +use function min; |
| 26 | +use function octdec; |
17 | 27 | use function sprintf; |
| 28 | +use function str_ends_with; |
| 29 | +use function strlen; |
| 30 | +use function substr; |
| 31 | +use function trim; |
| 32 | + |
| 33 | +use const SEEK_CUR; |
18 | 34 |
|
19 | 35 | class DbUpdater implements DbUpdaterInterface |
20 | 36 | { |
21 | 37 | private const DB_COMPRESSED_FILE = 'GeoLite2-City.tar.gz'; |
| 38 | + private const DB_TEMP_TAR_FILE = 'GeoLite2-City.tmp.tar'; |
22 | 39 | private const DB_DECOMPRESSED_FILE = 'GeoLite2-City.mmdb'; |
23 | 40 |
|
24 | 41 | public function __construct( |
@@ -64,25 +81,86 @@ private function downloadDbFile(string $dest, callable|null $handleProgress = nu |
64 | 81 | } |
65 | 82 | } |
66 | 83 |
|
| 84 | + /** |
| 85 | + * Decompress provided GZ file into a temp location, and return the path to that location |
| 86 | + */ |
67 | 87 | private function extractDbFile(string $compressedFile, string $tempDir): string |
68 | 88 | { |
69 | | - try { |
70 | | - $phar = new PharData($compressedFile); |
71 | | - $internalPathToDb = sprintf('%s/%s', $phar->getBasename(), self::DB_DECOMPRESSED_FILE); |
72 | | - $phar->extractTo($tempDir, $internalPathToDb, true); |
| 89 | + // Decompress temporary tar package |
| 90 | + $tarPath = $tempDir . '/' . self::DB_TEMP_TAR_FILE; |
| 91 | + $gz = @gzopen($compressedFile, 'rb'); |
| 92 | + if (! $gz) { |
| 93 | + throw DbUpdateException::forFailedExtraction(); |
| 94 | + } |
73 | 95 |
|
74 | | - return sprintf('%s/%s', $tempDir, $internalPathToDb); |
75 | | - } catch (Throwable $e) { |
76 | | - throw DbUpdateException::forFailedExtraction($compressedFile, $e); |
| 96 | + $tar = @fopen($tarPath, 'wb'); |
| 97 | + if (! $tar) { |
| 98 | + gzclose($gz); |
| 99 | + throw DbUpdateException::forFailedExtraction(); |
| 100 | + } |
| 101 | + while ($chunk = gzread($gz, 4096)) { |
| 102 | + fwrite($tar, $chunk); |
77 | 103 | } |
| 104 | + gzclose($gz); |
| 105 | + fclose($tar); |
| 106 | + |
| 107 | + // Process tar file sequentially, looking for the db file |
| 108 | + $tar = fopen($tarPath, 'rb'); |
| 109 | + if (! $tar) { |
| 110 | + throw DbUpdateException::forFailedExtraction(); |
| 111 | + } |
| 112 | + |
| 113 | + $outputPath = $tempDir . '/' . self::DB_DECOMPRESSED_FILE; |
| 114 | + while (! feof($tar)) { |
| 115 | + $header = fread($tar, 512); |
| 116 | + if (! $header || strlen($header) < 512) { |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + $filename = trim(substr($header, offset: 0, length: 100)); |
| 121 | + if ($filename === '') { |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + $size = octdec(trim(substr($header, offset: 124, length: 12))); |
| 126 | + |
| 127 | + // Once we find the file, read it sequentially and return |
| 128 | + if (str_ends_with($filename, self::DB_DECOMPRESSED_FILE) && $out = fopen($outputPath, 'wb')) { |
| 129 | + $remaining = $size; |
| 130 | + while ($remaining > 0) { |
| 131 | + /** @var positive-int $readLen */ |
| 132 | + $readLen = min(4096, $remaining); |
| 133 | + $data = fread($tar, $readLen); |
| 134 | + if (! $data) { |
| 135 | + break; |
| 136 | + } |
| 137 | + fwrite($out, $data); |
| 138 | + $remaining -= strlen($data); |
| 139 | + } |
| 140 | + fclose($out); |
| 141 | + fclose($tar); |
| 142 | + $this->deleteTempFiles([$tarPath]); |
| 143 | + |
| 144 | + return $outputPath; |
| 145 | + } |
| 146 | + |
| 147 | + // Skip this file |
| 148 | + $skip = $size + (512 - ($size % 512)) % 512; |
| 149 | + fseek($tar, offset: (int) $skip, whence: SEEK_CUR); |
| 150 | + } |
| 151 | + |
| 152 | + fclose($tar); |
| 153 | + $this->deleteTempFiles([$tarPath]); |
| 154 | + |
| 155 | + throw DbUpdateException::forFailedExtraction(); |
78 | 156 | } |
79 | 157 |
|
80 | 158 | private function copyNewDbFile(string $from): void |
81 | 159 | { |
82 | 160 | $destination = $this->options->dbLocation; |
83 | 161 |
|
84 | 162 | try { |
85 | | - $this->filesystem->copy($from, $destination, true); |
| 163 | + $this->filesystem->copy($from, $destination, overwriteNewerFiles: true); |
86 | 164 | $this->filesystem->chmod([$destination], 0666); |
87 | 165 | } catch (FilesystemException\FileNotFoundException | FilesystemException\IOException $e) { |
88 | 166 | throw DbUpdateException::forFailedCopyToDestination($destination, $e); |
|
0 commit comments