[23087] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | use strict;
|
---|
| 4 |
|
---|
| 5 | use CGI::Carp qw(fatalsToBrowser);
|
---|
| 6 | use CGI;
|
---|
| 7 |
|
---|
| 8 | my $gsdl_cgi;
|
---|
[23177] | 9 | my $gsdl_home;
|
---|
[23087] | 10 | my $gsdl_tmp_dir;
|
---|
| 11 |
|
---|
[23183] | 12 | my $debugging_enabled = 0; # if 1, if enabled deleting files will not happen
|
---|
[23177] | 13 |
|
---|
[23087] | 14 | BEGIN {
|
---|
| 15 |
|
---|
| 16 | $|=1; # Force auto-flushing for output
|
---|
| 17 |
|
---|
| 18 | eval('require "./gsdlCGI.pm"');
|
---|
| 19 | if ($@)
|
---|
| 20 | {
|
---|
| 21 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 22 | print STDOUT "ERROR: $@\n";
|
---|
| 23 | exit 0;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 |
|
---|
[23149] | 29 | sub get_progress_filename
|
---|
| 30 | {
|
---|
| 31 | my ($uploaded_file) = @_;
|
---|
| 32 |
|
---|
| 33 | my $progress_file = $uploaded_file;
|
---|
| 34 |
|
---|
| 35 | $progress_file =~ s{^(.*)\/}{};
|
---|
| 36 | $progress_file =~ s/\.*?$//;
|
---|
| 37 | $progress_file .= "-progress.txt";
|
---|
| 38 |
|
---|
| 39 | my $progress_filename = &util::filename_cat($gsdl_tmp_dir, $progress_file);
|
---|
| 40 |
|
---|
| 41 | return $progress_filename;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | sub get_file_central_filename
|
---|
| 45 | {
|
---|
| 46 | my $file_central = &util::filename_cat($gsdl_tmp_dir,"file-central.txt");
|
---|
| 47 |
|
---|
| 48 | return $file_central;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | sub read_file_central
|
---|
| 52 | {
|
---|
| 53 | my $fc_filename = get_file_central_filename();
|
---|
| 54 |
|
---|
| 55 | my @fc_list;
|
---|
| 56 |
|
---|
| 57 | if (open(FCIN,"<$fc_filename")) {
|
---|
| 58 |
|
---|
| 59 | my $fc_list_str = do { local $/; <FCIN> };
|
---|
| 60 | @fc_list = split(/\n/,$fc_list_str);
|
---|
| 61 |
|
---|
| 62 | close(FCIN);
|
---|
| 63 | }
|
---|
| 64 | else {
|
---|
| 65 | # OK to have no file-central.txt to start with
|
---|
| 66 | # return empty list
|
---|
| 67 | @fc_list = ();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | return \@fc_list;
|
---|
| 71 |
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | sub remove_from_file_central
|
---|
| 75 | {
|
---|
| 76 | my ($filename,$fc_list) = @_;
|
---|
| 77 |
|
---|
| 78 | my @new_fc_list = ();
|
---|
| 79 |
|
---|
| 80 | my $removed = 0;
|
---|
| 81 |
|
---|
| 82 | foreach my $f (@$fc_list) {
|
---|
| 83 |
|
---|
| 84 | if ($f ne $filename) {
|
---|
| 85 | push(@new_fc_list,$f);
|
---|
| 86 | }
|
---|
| 87 | else {
|
---|
| 88 | $removed = 1;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (!$removed) {
|
---|
| 93 | print STDERR "Warning: Failed to locate '$filename' in file-central.txt\n";
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | return \@new_fc_list;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | sub add_to_file_central
|
---|
| 100 | {
|
---|
| 101 | my ($filename,$fc_list) = @_;
|
---|
| 102 |
|
---|
| 103 | my @new_fc_list = @$fc_list;
|
---|
| 104 |
|
---|
| 105 | my $duplicate = 0;
|
---|
| 106 | foreach my $f (@new_fc_list) {
|
---|
| 107 |
|
---|
| 108 | if ($f eq $filename) {
|
---|
| 109 | $duplicate = 1;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (!$duplicate) {
|
---|
| 114 | push(@new_fc_list,$filename);
|
---|
| 115 | }
|
---|
| 116 | else {
|
---|
| 117 | print STDERR "Warning: Ingoring request to add duplicate entry:\n";
|
---|
| 118 | print STDERR " '$filename' into file-central.txt\n"
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return \@new_fc_list;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | sub write_file_central
|
---|
| 127 | {
|
---|
| 128 | my ($fc_list) = @_;
|
---|
| 129 |
|
---|
| 130 | my $fc_filename = get_file_central_filename();
|
---|
| 131 |
|
---|
[23183] | 132 |
|
---|
[23149] | 133 | if (open(FCOUT,">$fc_filename")) {
|
---|
| 134 |
|
---|
| 135 | foreach my $f (@$fc_list) {
|
---|
| 136 | print FCOUT "$f\n";
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | close(FCOUT);
|
---|
[23183] | 140 |
|
---|
| 141 |
|
---|
| 142 | # Ensure it can be edited by owner of Greenstone install (if needed)
|
---|
| 143 | chmod(0777,$fc_filename);
|
---|
[23149] | 144 | }
|
---|
| 145 | else {
|
---|
| 146 | print STDERR "Error: Failed to write out $fc_filename\n";
|
---|
| 147 | print STDERR "$!\n";
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[23087] | 151 | sub monitor_upload
|
---|
| 152 | {
|
---|
[23149] | 153 | my ($uploading_file, $buffer, $bytes_read, $data) = @_;
|
---|
[23087] | 154 |
|
---|
| 155 | $bytes_read ||= 0;
|
---|
[23149] | 156 |
|
---|
| 157 | my $progress_filename = get_progress_filename($uploading_file);
|
---|
[23087] | 158 |
|
---|
[23149] | 159 | if (! -f $progress_filename) {
|
---|
| 160 | my $fc_list = read_file_central();
|
---|
| 161 | $fc_list = add_to_file_central($uploading_file,$fc_list);
|
---|
| 162 | write_file_central($fc_list);
|
---|
| 163 | }
|
---|
[23087] | 164 |
|
---|
[23149] | 165 | open(COUNTER, ">$progress_filename");
|
---|
| 166 |
|
---|
[23087] | 167 | my $per = 0;
|
---|
[23137] | 168 | if ($ENV{CONTENT_LENGTH} > 0) {
|
---|
[23087] | 169 | $per = int(($bytes_read * 100) / $ENV{CONTENT_LENGTH});
|
---|
| 170 | }
|
---|
| 171 | print COUNTER $per;
|
---|
| 172 | close(COUNTER);
|
---|
| 173 |
|
---|
| 174 | # Useful debug to slow down a 'fast' upload
|
---|
| 175 | # Sleep for 10 msecs
|
---|
[23183] | 176 | #select(undef, undef, undef, 0.01);
|
---|
[23149] | 177 | #select(undef, undef, undef, 0.1);
|
---|
[23087] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | sub upload_file {
|
---|
| 183 |
|
---|
| 184 | my ($gsdl_cgi,$full_filename) = @_;
|
---|
| 185 |
|
---|
| 186 | my $fh = $gsdl_cgi->upload('uploadedfile');
|
---|
| 187 | my $filename = $gsdl_cgi->param('uploadedfile');
|
---|
| 188 |
|
---|
| 189 | return '' if ! $filename;
|
---|
| 190 |
|
---|
| 191 | open (OUTFILE, '>' . $full_filename)
|
---|
| 192 | || die("Can't write to $full_filename: $!");
|
---|
| 193 |
|
---|
| 194 | binmode(OUTFILE);
|
---|
| 195 |
|
---|
| 196 | while (my $bytesread = read($fh, my $buffer, 1024)) {
|
---|
| 197 | print OUTFILE $buffer;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | close (OUTFILE);
|
---|
| 201 | chmod(0666, $full_filename);
|
---|
| 202 |
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | sub remove_progress_file
|
---|
| 206 | {
|
---|
[23149] | 207 | my ($uploaded_file) = @_;
|
---|
[23087] | 208 |
|
---|
[23149] | 209 | my $progress_filename = get_progress_filename($uploaded_file);
|
---|
[23087] | 210 |
|
---|
[23177] | 211 | unlink($progress_filename)
|
---|
| 212 | unless $debugging_enabled;
|
---|
[23149] | 213 |
|
---|
| 214 | my $fc_list = read_file_central();
|
---|
| 215 | $fc_list = remove_from_file_central($uploaded_file,$fc_list);
|
---|
| 216 | write_file_central($fc_list);
|
---|
[23087] | 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
[23177] | 220 | sub unzip_archives_doc
|
---|
| 221 | {
|
---|
| 222 | my ($gsdl_cgi,$gsdl_home,$collect_home,$collect,$zip_filename) = @_;
|
---|
| 223 |
|
---|
| 224 | my $lang_env = $gsdl_cgi->clean_param("lr") || "";
|
---|
| 225 |
|
---|
| 226 | my $import_dir = &util::filename_cat($collect_home,$collect,"import");
|
---|
| 227 |
|
---|
| 228 | # Unzip $zip_filename in the collection's import folder
|
---|
| 229 | my $java = $gsdl_cgi->get_java_path();
|
---|
| 230 | my $jar_dir= &util::filename_cat($gsdl_home, "bin", "java");
|
---|
| 231 | my $java_classpath = &util::filename_cat($jar_dir,"GLIServer.jar");
|
---|
| 232 |
|
---|
| 233 | my $java_args = "\"$zip_filename\" \"$import_dir\"";
|
---|
| 234 |
|
---|
| 235 | $ENV{'LANG'} = $lang_env;
|
---|
| 236 | my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
|
---|
| 237 |
|
---|
| 238 | my $java_output = `$java_command`;
|
---|
| 239 | my $java_status = $?;
|
---|
| 240 | if ($java_status > 0) {
|
---|
| 241 | my $report = "Java failed: $java_command\n--\n";
|
---|
| 242 | $report .= "$java_output\n";
|
---|
| 243 | $report .= "Exit status: " . ($java_status / 256) . "\n";
|
---|
| 244 | $report .= $gsdl_cgi->check_java_home();
|
---|
| 245 |
|
---|
| 246 | $gsdl_cgi->generate_error($report);
|
---|
| 247 | }
|
---|
| 248 | else {
|
---|
| 249 | # Remove the zip file once we have unzipped it, since it is an intermediate file only
|
---|
[23183] | 250 | `chmod -R a+rw $import_dir/HASH*`;
|
---|
[23177] | 251 | unlink($zip_filename) unless $debugging_enabled;
|
---|
| 252 | }
|
---|
[23183] | 253 | }
|
---|
[23177] | 254 |
|
---|
| 255 |
|
---|
[23183] | 256 | sub rebuild_collection
|
---|
| 257 | {
|
---|
| 258 | my ($collect) = @_;
|
---|
| 259 |
|
---|
| 260 | my $bin_script = &util::filename_cat($gsdl_home,"bin","script");
|
---|
| 261 | ## my $inc_rebuild_pl = &util::filename_cat($bin_script,"incremental-rebuild.pl");
|
---|
| 262 |
|
---|
| 263 | my $rebuild_cmd = "perl -S incremental-rebuild.pl \"$collect\"";
|
---|
| 264 |
|
---|
| 265 | my $rebuild_output = `$rebuild_cmd 2>&1`;
|
---|
| 266 | my $rebuild_status = $?;
|
---|
| 267 | if ($rebuild_status > 0) {
|
---|
| 268 | my $report = "Perl rebuild failed: $rebuild_cmd\n--\n";
|
---|
| 269 | $report .= "$rebuild_output\n";
|
---|
| 270 | $report .= "Exit status: " . ($rebuild_status / 256) . "\n";
|
---|
| 271 | ## $report .= $gsdl_cgi->check_perl_home();
|
---|
| 272 |
|
---|
| 273 | # $report .= "PATH = ". $ENV{'PATH'}. "\n";
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | $gsdl_cgi->generate_error($report);
|
---|
| 277 | }
|
---|
[23177] | 278 | }
|
---|
| 279 |
|
---|
[23087] | 280 | sub main {
|
---|
| 281 |
|
---|
| 282 | # gsdlCGI->prenew() constructs a 'lite' version of the object where the
|
---|
| 283 | # GSDL environment has been setup
|
---|
| 284 | #
|
---|
| 285 | # This is needed because the main call the gsdlCGI->new takes an
|
---|
| 286 | # initializer rountine -- monitor_upload() -- as a parameter, AND THAT
|
---|
| 287 | # routine (which is called before the constructor is finished) needs to
|
---|
| 288 | # know the tmp directory to use to write out the progress file.
|
---|
| 289 |
|
---|
| 290 | my $gsdl_config = gsdlCGI->prenew();
|
---|
| 291 |
|
---|
[23177] | 292 | $gsdl_home = $gsdl_config->get_gsdl_home();
|
---|
| 293 | $gsdl_tmp_dir = &util::get_toplevel_tmp_dir();
|
---|
| 294 |
|
---|
[23087] | 295 | require talkback;
|
---|
| 296 |
|
---|
| 297 | # Use the initializer mechanism so a 'callback' routine can monitor
|
---|
| 298 | # the progress of how much data has been uploaded
|
---|
| 299 |
|
---|
| 300 | $gsdl_cgi = gsdlCGI->new(\&monitor_upload);
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | require CGI::Ajax;
|
---|
| 304 |
|
---|
| 305 | my $perlAjax = new CGI::Ajax('check_status' => \&check_status);
|
---|
| 306 |
|
---|
| 307 |
|
---|
| 308 | if ($gsdl_cgi->param('process')) {
|
---|
| 309 |
|
---|
[23177] | 310 | my $uploaded_file = $gsdl_cgi->param('uploadedfile');
|
---|
| 311 | my $full_filename = &util::filename_cat($gsdl_tmp_dir,$uploaded_file);
|
---|
[23087] | 312 |
|
---|
| 313 | my $done_html = &talkback::generate_done_html($full_filename);
|
---|
| 314 |
|
---|
| 315 | if ($gsdl_cgi->param('yes_upload')) {
|
---|
| 316 | upload_file($gsdl_cgi,$full_filename);
|
---|
[23177] | 317 |
|
---|
| 318 | my $collect = $gsdl_cgi->param('toCollect');
|
---|
| 319 | my $collect_home = &util::filename_cat($gsdl_home,"collect");
|
---|
| 320 |
|
---|
| 321 | unzip_archives_doc($gsdl_cgi,$gsdl_home,$collect_home,$collect,$full_filename);
|
---|
[23183] | 322 | rebuild_collection($collect);
|
---|
[23087] | 323 | }
|
---|
| 324 |
|
---|
| 325 | print $gsdl_cgi->header();
|
---|
| 326 | print $done_html;
|
---|
| 327 |
|
---|
[23177] | 328 | remove_progress_file($uploaded_file);
|
---|
[23087] | 329 | }
|
---|
| 330 | else {
|
---|
| 331 |
|
---|
| 332 | my $upload_html_form;
|
---|
| 333 |
|
---|
[23139] | 334 | #my $oid = $gsdl_cgi->param('oid');
|
---|
| 335 | #my $collect = $gsdl_cgi->param('collect');
|
---|
[23140] | 336 | my $uniq_file = $gsdl_cgi->param('uploadedfile');
|
---|
[23087] | 337 |
|
---|
[23177] | 338 | #my $uniq_file = "$collect-$oid-doc.zip";
|
---|
[23138] | 339 | # Light-weight (hidden) form with progress bar
|
---|
[23087] | 340 |
|
---|
[23138] | 341 | $upload_html_form
|
---|
| 342 | = &talkback::generate_upload_form_progressbar($uniq_file);
|
---|
[23087] | 343 |
|
---|
| 344 | print $perlAjax->build_html($gsdl_cgi, $upload_html_form);
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 | main();
|
---|
| 350 |
|
---|
[23149] | 351 |
|
---|
[23087] | 352 | #=====
|
---|
| 353 |
|
---|
[23137] | 354 | sub inc_wait_dots
|
---|
| 355 | {
|
---|
[23089] | 356 | my $wait_filename = &util::filename_cat($gsdl_tmp_dir,"wait.txt");
|
---|
| 357 | open(WIN,"<$wait_filename");
|
---|
[23087] | 358 | my $wait = <WIN>;
|
---|
| 359 | close(WIN);
|
---|
| 360 |
|
---|
| 361 | $wait = ($wait+1) %6;
|
---|
| 362 | my $wait_dots = "." x ($wait+1);
|
---|
| 363 |
|
---|
[23089] | 364 | open(WOUT,">$wait_filename");
|
---|
[23087] | 365 | print WOUT "$wait\n";
|
---|
| 366 | close(WOUT);
|
---|
| 367 |
|
---|
[23137] | 368 | return $wait_dots;
|
---|
| 369 | }
|
---|
[23087] | 370 |
|
---|
[23149] | 371 |
|
---|
[23137] | 372 | sub check_status_single_file
|
---|
| 373 | {
|
---|
| 374 | my ($filename) = @_;
|
---|
[23149] | 375 |
|
---|
| 376 | my $monitor_filename = get_progress_filename($filename);
|
---|
[23137] | 377 |
|
---|
[23087] | 378 | if (! -f $monitor_filename ) {
|
---|
[23137] | 379 | return "";
|
---|
[23087] | 380 | }
|
---|
[23137] | 381 |
|
---|
[23087] | 382 | open my $PROGRESS, '<', $monitor_filename or die $!;
|
---|
| 383 | my $s = do { local $/; <$PROGRESS> };
|
---|
| 384 | close ($PROGRESS);
|
---|
| 385 |
|
---|
[23137] | 386 | my $fgwidth = int($s * 1.5);
|
---|
| 387 | my $bgwidth = 150 - $fgwidth;
|
---|
| 388 |
|
---|
| 389 | my $fgcol = "background-color:#dddd00;";
|
---|
| 390 | my $bgcol = "background-color:#008000;";
|
---|
[23087] | 391 |
|
---|
[23137] | 392 | my $style_base = "height:10px; float:left;";
|
---|
| 393 |
|
---|
| 394 | my $r = "";
|
---|
[23149] | 395 | $r .= "<div>$filename:</div>";
|
---|
| 396 | $r .= "<nobr>";
|
---|
[23137] | 397 | $r .= "<div style=\"width: ${fgwidth}px; $fgcol $style_base\"></div>";
|
---|
| 398 | $r .= "<div style=\"width: ${bgwidth}px; $bgcol $style_base\"></div>";
|
---|
| 399 | $r .= "<div style=\"float:left; width: 80px\"> $s%</div>";
|
---|
[23149] | 400 | $r .= "</nobr>";
|
---|
| 401 | $r .= "<br />";
|
---|
[23137] | 402 |
|
---|
[23087] | 403 | return $r;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 |
|
---|
[23137] | 407 | sub check_status_all
|
---|
| 408 | {
|
---|
[23149] | 409 | my $file_central = get_file_central_filename();
|
---|
[23087] | 410 |
|
---|
[23137] | 411 | my $html = "";
|
---|
[23087] | 412 |
|
---|
[23149] | 413 | my $fc_list = read_file_central();
|
---|
[23137] | 414 |
|
---|
[23149] | 415 | foreach my $f (@$fc_list) {
|
---|
| 416 | $html .= check_status_single_file($f);
|
---|
[23137] | 417 | }
|
---|
| 418 |
|
---|
| 419 | return $html;
|
---|
| 420 |
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[23087] | 423 |
|
---|
[23137] | 424 | # Accessed from HTML web page through the majic of perlAjax
|
---|
| 425 |
|
---|
| 426 | sub check_status
|
---|
| 427 | {
|
---|
| 428 |
|
---|
| 429 | my $wait_dots = inc_wait_dots();
|
---|
| 430 | my $html = "$wait_dots<br>";
|
---|
| 431 |
|
---|
| 432 | my $filename = $gsdl_cgi->param('uploadedfile');
|
---|
[23149] | 433 |
|
---|
| 434 | if ((defined $filename) && ($filename ne "")) {
|
---|
[23137] | 435 | $html .= check_status_single_file($filename);
|
---|
| 436 | }
|
---|
| 437 | else {
|
---|
| 438 | $html .= check_status_all();
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | return $html;
|
---|
| 442 | }
|
---|