|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraGeoData\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use LaraGeoData\Facades\GeoDataImporter; |
|
8
|
|
|
|
|
9
|
|
|
class DownloadFilesCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'geonames:download |
|
17
|
|
|
{files?* : Files to download. } |
|
18
|
|
|
{--postal=* : Postal files to download. } |
|
19
|
|
|
{--force : Override files any if exists. } |
|
20
|
|
|
{--extract : Extract archive. } |
|
21
|
|
|
{--defaults : ODownload default files set. } |
|
22
|
|
|
'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Download and extract files from server.'; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
4 |
|
public function handle() |
|
33
|
|
|
{ |
|
34
|
4 |
|
foreach ($this->getFiles() as $file) { |
|
35
|
4 |
|
$this->downloadGeneralFile($file); |
|
36
|
4 |
|
$this->extractFile($file); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
foreach ($this->getPostalFiles() as $file) { |
|
40
|
4 |
|
$this->downloadPostalCodeFile($file); |
|
41
|
4 |
|
$this->extractFile($this->postalCodeFile($file)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
return 0; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get default files. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
4 |
|
protected function getFiles(): array |
|
53
|
|
|
{ |
|
54
|
4 |
|
$files = $this->argument('files'); |
|
55
|
4 |
|
if (empty($files) && $this->option('defaults')) { |
|
56
|
|
|
$files = [ |
|
57
|
|
|
'allCountries.zip', |
|
58
|
|
|
'alternateNames.zip', |
|
59
|
|
|
'hierarchy.zip', |
|
60
|
|
|
'admin1CodesASCII.txt', |
|
61
|
|
|
'admin2Codes.txt', |
|
62
|
|
|
'featureCodes_en.txt', |
|
63
|
|
|
'timeZones.txt', |
|
64
|
|
|
'countryInfo.txt', |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
4 |
|
if (!is_array($files)) { |
|
68
|
|
|
throw new \InvalidArgumentException('"files" argument should be valid array.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
return $files; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get default files. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
4 |
|
protected function getPostalFiles(): array |
|
80
|
|
|
{ |
|
81
|
4 |
|
$files = $this->option('postal'); |
|
82
|
4 |
|
if (is_array($files)) { |
|
|
|
|
|
|
83
|
4 |
|
$files = array_filter($files); |
|
84
|
|
|
} |
|
85
|
4 |
|
if (empty($files) && $this->option('defaults')) { |
|
86
|
|
|
$files = [ |
|
87
|
|
|
'allCountries.zip', |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
4 |
|
if (!is_array($files)) { |
|
91
|
|
|
throw new \InvalidArgumentException('"postal" option should be valid array.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
return $files; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
protected function downloadGeneralFile(string $file) |
|
98
|
|
|
{ |
|
99
|
4 |
|
$baseUrl = rtrim(config('geonames.import_repo'), '/') . '/'; |
|
100
|
4 |
|
$url = $baseUrl . $file; |
|
101
|
4 |
|
$this->downloadFile($file, $url); |
|
102
|
4 |
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
protected function downloadPostalCodeFile(string $file) |
|
105
|
|
|
{ |
|
106
|
4 |
|
$baseUrl = rtrim(config('geonames.import_postal_code_repo'), '/') . '/'; |
|
107
|
4 |
|
$url = $baseUrl . $file; |
|
108
|
4 |
|
$this->downloadFile($this->postalCodeFile($file), $url); |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
protected function downloadFile(string $file, string $url) |
|
112
|
|
|
{ |
|
113
|
4 |
|
$this->info("Download: {$url} to {$file}"); |
|
114
|
4 |
|
$bar = $this->output->createProgressBar(10000); |
|
115
|
4 |
|
$bar->start(); |
|
116
|
4 |
|
$result = GeoDataImporter::storeFileFromUrl( |
|
|
|
|
|
|
117
|
4 |
|
$url, |
|
118
|
|
|
$file, |
|
119
|
4 |
|
function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bar) { |
|
|
|
|
|
|
120
|
|
|
if ($download_size > 0) { |
|
121
|
|
|
$percent = round($downloaded / $download_size * 10000); |
|
122
|
|
|
$bar->setProgress($percent); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
sleep(1); |
|
125
|
4 |
|
}, |
|
126
|
4 |
|
(bool) $this->option('force') |
|
127
|
|
|
); |
|
128
|
4 |
|
$bar->finish(); |
|
129
|
4 |
|
if ($result) { |
|
130
|
3 |
|
$this->info(' Downloaded.'); |
|
131
|
|
|
} else { |
|
132
|
2 |
|
$this->error(' Skipped.'); |
|
133
|
|
|
} |
|
134
|
4 |
|
} |
|
135
|
|
|
|
|
136
|
4 |
|
protected function postalCodeFile(string $file): string |
|
137
|
|
|
{ |
|
138
|
4 |
|
return rtrim(config('geonames.storage.postal_codes_dir'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
4 |
|
protected function extractFile(string $file) |
|
143
|
|
|
{ |
|
144
|
4 |
|
if (!$this->option('extract')) { |
|
145
|
3 |
|
return; |
|
146
|
|
|
} |
|
147
|
1 |
|
if (!Str::endsWith($file, '.zip')) { |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
1 |
|
$result = GeoDataImporter::extractFile($file); |
|
|
|
|
|
|
151
|
1 |
|
if ($result) { |
|
152
|
1 |
|
$this->info("Extracted file: {$file}"); |
|
153
|
|
|
} else { |
|
154
|
|
|
$this->error("Error extraction file: {$file}"); |
|
155
|
|
|
} |
|
156
|
1 |
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|