-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlowzoomupload.pl
executable file
·345 lines (290 loc) · 9.97 KB
/
lowzoomupload.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/perl
use lib '.';
use strict;
use LWP::UserAgent;
use File::Copy;
use Digest::MD5 qw(md5_hex);
use English '-no_match_vars';
use tahconfig;
use tahlib;
#-----------------------------------------------------------------------------
# OpenStreetMap tiles@home, upload module
# Takes any tiles generated, adds them into ZIP files, and uploads them
#
# Contact OJW on the Openstreetmap wiki for help using this program
#-----------------------------------------------------------------------------
# Copyright 2006, Oliver White
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-----------------------------------------------------------------------------
# conf file, will contain username/password and environment info
my %Config = ReadConfig(
"freemapdiskclient.conf", "general.conf",
"authentication.conf", "layers.conf",
"freemapdisk.conf"
);
# Handle the command-line
my $Mode = shift();
my $ConfigName;
if ( $Mode eq "config" ) {
my $ConfigName = shift();
if ( not defined $ConfigName ) {
die "Must specify config file name \n";
}
AppendConfig ($ConfigName);
# precitam dalsi prikaz
} else {
unshift(@_,$Mode);
}
my $ZipFileCount = 0;
my $ZipDir = $Config{WorkingDirectory} . "uploadable";
my @sorted;
# when called from tilesGen, use these for nice display
my $progress = 0;
my $progressPercent = 0;
my $progressJobs = $ARGV[0] or 1;
my $currentSubTask;
my $lastmsglen;
my $tileCount;
my @tiles;
# Upload any ZIP files which are still waiting to go
if (opendir(ZIPDIR, $ZipDir))
{
$currentSubTask = "uploadZ";
$progress = 0;
my @zipfiles = grep { /\.zip$/ } readdir(ZIPDIR);
close ZIPDIR;
@sorted = sort { $a cmp $b }
@zipfiles
; # sort by ASCII value (i.e. upload oldest first if timestamps used)
my $zipCount = scalar(@sorted);
statusMessage(scalar(@sorted) . " zip files to upload",
$Config{Verbose}, $currentSubTask, $progressJobs,
$progressPercent, 0);
while (my $File = shift @sorted)
{
if ($File =~ /\.zip$/i)
{
upload("$ZipDir$Config{Slash}$File");
}
$progress++;
$progressPercent = $progress * 100 / $zipCount;
statusMessage(
scalar(@sorted) . " zip files left to upload",
$Config{Verbose},
$currentSubTask,
$progressJobs,
$progressPercent,
0
);
}
}
$currentSubTask = " upload";
# We calculate % differently this time so we don't need "progress"
# $progress = 0;
$progressPercent = 0;
my $TileDir = $Config{WorkingDirectory};
# Group and upload the tiles
statusMessage("Searching for tiles in $TileDir",
$Config{Verbose}, $currentSubTask, $progressJobs,
$progressPercent, 0);
# compile a list of the "Prefix" values of all configured layers,
# # separated by |
my $allowedPrefixes = join(
"|",
map($Config{"Layer.$_.Prefix"},
split(/,/, $Config{"LowLayers"}))
);
opendir(my $dp, $TileDir) or die("Can't open directory $TileDir\n");
my @dir = readdir($dp);
@tiles = grep { /($allowedPrefixes)_\d+_\d+_\d+\.png$/ } @dir;
closedir($dp);
$tileCount = scalar(@tiles);
exit if ($tileCount == 0);
while (uploadTileBatch($TileDir, $TileDir . "gather", $ZipDir, $allowedPrefixes)) {
}
#-----------------------------------------------------------------------------
# Moves tiles into a "gather" directory until a certain size is reached,
# then compress and upload those files
#-----------------------------------------------------------------------------
sub uploadTileBatch
{
my ($TileDir, $TempDir, $OutputDir, $allowedPrefixes) = @_;
my ($Size, $Count) = (0, 0);
my $MB = 1024 * 1024;
my $SizeLimit = $Config{"UploadChunkSize"} * $MB;
my $CountLimit = $Config{"UploadChunkCount"};
#prevent too small zips, 683=half a tileset
$CountLimit = 683 if ($CountLimit < 100);
mkdir $TempDir if !-d $TempDir;
mkdir $OutputDir if !-d $OutputDir;
$progressPercent = ($tileCount - scalar(@tiles)) * 100 / $tileCount;
statusMessage(scalar(@tiles) . " tiles to process",
$Config{Verbose}, $currentSubTask, $progressJobs,
$progressPercent, 0);
while ( ($Size < $SizeLimit)
&& ($Count < $CountLimit)
&& (my $file = shift @tiles))
{
my $Filename1 = "$TileDir$Config{Slash}$file";
my $Filename2 = "$TempDir$Config{Slash}$file";
if ($file =~ /($allowedPrefixes)_\d+_\d+_\d+\.png$/i)
{
$Size += -s $Filename1;
$Count++;
rename($Filename1, $Filename2);
}
}
if ($Count)
{
statusMessage(
sprintf(
"Got %d files (%d bytes), compressing",
$Count, $Size
),
$Config{Verbose},
$currentSubTask,
$progressJobs,
$progressPercent,
0
);
return compressTiles($TempDir, $OutputDir);
}
else
{
$progressPercent = ($tileCount - scalar(@tiles)) * 100 / $tileCount;
statusMessage("upload finished",
$Config{Verbose}, $currentSubTask, $progressJobs,
$progressPercent, 0);
return 0;
}
}
#-----------------------------------------------------------------------------
# Compress all PNG files from one directory, creating
#-----------------------------------------------------------------------------
sub compressTiles
{
my ($Dir, $OutputDir) = @_;
my $Filename;
my $epochtime = time;
if ($Config{UseHostnameInZipname})
{
my $hostname = `hostname` . "XXX";
$Filename = sprintf("%s$Config{Slash}%s_%d_%d.zip",
$OutputDir, substr($hostname, 0, 3),
$$, $ZipFileCount++);
}
else
{
$Filename = sprintf("%s$Config{Slash}%d_%d_%d.zip",
$OutputDir, $epochtime, $$, $ZipFileCount++);
}
# ZIP all the tiles into a single file
my $stdOut = $Config{WorkingDirectory} . "/" . $PID . ".stdout";
my $Command1 = sprintf("%s %s %s > %s",
" $Config{Zip} $Config{ZipAdd}", $Filename,
"$Dir$Config{Slash}*.png", $stdOut);
# ZIP filename is currently our process ID - change this if one process
# becomes capable of generating multiple zip files
## FIXME: this is one of the things that make upload.pl not multithread safe
# Delete files in the gather directory
opendir(GATHERDIR, $Dir);
my @zippedFiles = grep { /.png$/ } readdir(GATHERDIR);
closedir(GATHERDIR);
# Run the two commands
if (runCommand($Command1, $PID))
{
while (my $File = shift @zippedFiles)
{
killafile($Dir . "$Config{Slash}" . $File);
}
}
else
{
while (my $File = shift @zippedFiles)
{
rename($Dir . "$Config{Slash}" . $File,
$Config{WorkingDirectory} . $File);
}
}
killafile($stdOut) if ( !$Config{Debug} );
return upload($Filename);
}
#-----------------------------------------------------------------------------
# Upload a ZIP file
#-----------------------------------------------------------------------------
sub upload()
{
my ($File) = @_;
my $ua = LWP::UserAgent->new(keep_alive => 1, timeout => 360);
$ua->protocols_allowed(['http']);
$ua->agent("FreemapDiSK/2011.05.11");
$ua->env_proxy();
my $URL = $Config{"DiSKUploadURL"};
#print $URL;
statusMessage("Uploading $File", $Config{Verbose}, $currentSubTask, $progressJobs,$progressPercent, 0);
my $res = $ua->post( $URL,
Content_Type => 'form-data',
Content => [ File => [$File],
Email => $Config{DiSKUsername} ,
PasswordMD5 => md5_hex($Config{DiSKPassword}),
ClientVersion => $Config{DiSKAPIVersion}
]);
if (!$res->is_success())
{
print STDERR "ERROR\n";
print STDERR " Error uploading $File to $URL:\n";
print STDERR " " . $res->status_line . "\n";
return 0;
}
else
{
print $res->content."\n";
}
if ($Config{DeleteZipFilesAfterUpload})
{
unlink($File);
}
else
{
rename($File, $File . "_uploaded");
}
return 1;
}
sub AppendConfig {
my $Filename = shift();
open( my $fp, "<$Filename" ) || die("Can't open \"$Filename\" ($!)\n");
while ( my $Line = <$fp> ) {
$Line =~ s/#.*$//; # Comments
$Line =~ s/\s*$//; # Trailing whitespace
if (
$Line =~ m{
^
\s*
([A-Za-z0-9._-]+) # Keyword: just one single word no spaces
\s* # Optional whitespace
= # Equals
\s* # Optional whitespace
(.*) # Value
}x
)
{
# Store config options in a hash array
$Config{$1} = $2;
print "Found $1 ($2)\n" if (0); # debug option
}
}
close $fp;
}