source: main/trunk/greenstone2/cgi-bin/talkback-progressbar.pl@ 23183

Last change on this file since 23183 was 23183, checked in by davidb, 14 years ago

With PATH information correctly resolved in gsdlCGI.pm, correct version of incremental-rebuild.pl used.

  • Property svn:executable set to *
File size: 9.6 KB
Line 
1#!/usr/bin/perl -w
2
3use strict;
4
5use CGI::Carp qw(fatalsToBrowser);
6use CGI;
7
8my $gsdl_cgi;
9my $gsdl_home;
10my $gsdl_tmp_dir;
11
12my $debugging_enabled = 0; # if 1, if enabled deleting files will not happen
13
14BEGIN {
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
29sub 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
44sub 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
51sub 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
74sub 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
99sub 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
126sub write_file_central
127{
128 my ($fc_list) = @_;
129
130 my $fc_filename = get_file_central_filename();
131
132
133 if (open(FCOUT,">$fc_filename")) {
134
135 foreach my $f (@$fc_list) {
136 print FCOUT "$f\n";
137 }
138
139 close(FCOUT);
140
141
142 # Ensure it can be edited by owner of Greenstone install (if needed)
143 chmod(0777,$fc_filename);
144 }
145 else {
146 print STDERR "Error: Failed to write out $fc_filename\n";
147 print STDERR "$!\n";
148 }
149}
150
151sub monitor_upload
152{
153 my ($uploading_file, $buffer, $bytes_read, $data) = @_;
154
155 $bytes_read ||= 0;
156
157 my $progress_filename = get_progress_filename($uploading_file);
158
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 }
164
165 open(COUNTER, ">$progress_filename");
166
167 my $per = 0;
168 if ($ENV{CONTENT_LENGTH} > 0) {
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
176 #select(undef, undef, undef, 0.01);
177 #select(undef, undef, undef, 0.1);
178}
179
180
181
182sub 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
205sub remove_progress_file
206{
207 my ($uploaded_file) = @_;
208
209 my $progress_filename = get_progress_filename($uploaded_file);
210
211 unlink($progress_filename)
212 unless $debugging_enabled;
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);
217}
218
219
220sub 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
250 `chmod -R a+rw $import_dir/HASH*`;
251 unlink($zip_filename) unless $debugging_enabled;
252 }
253}
254
255
256sub 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 }
278}
279
280sub 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
292 $gsdl_home = $gsdl_config->get_gsdl_home();
293 $gsdl_tmp_dir = &util::get_toplevel_tmp_dir();
294
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
310 my $uploaded_file = $gsdl_cgi->param('uploadedfile');
311 my $full_filename = &util::filename_cat($gsdl_tmp_dir,$uploaded_file);
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);
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);
322 rebuild_collection($collect);
323 }
324
325 print $gsdl_cgi->header();
326 print $done_html;
327
328 remove_progress_file($uploaded_file);
329 }
330 else {
331
332 my $upload_html_form;
333
334 #my $oid = $gsdl_cgi->param('oid');
335 #my $collect = $gsdl_cgi->param('collect');
336 my $uniq_file = $gsdl_cgi->param('uploadedfile');
337
338 #my $uniq_file = "$collect-$oid-doc.zip";
339 # Light-weight (hidden) form with progress bar
340
341 $upload_html_form
342 = &talkback::generate_upload_form_progressbar($uniq_file);
343
344 print $perlAjax->build_html($gsdl_cgi, $upload_html_form);
345 }
346}
347
348
349main();
350
351
352#=====
353
354sub inc_wait_dots
355{
356 my $wait_filename = &util::filename_cat($gsdl_tmp_dir,"wait.txt");
357 open(WIN,"<$wait_filename");
358 my $wait = <WIN>;
359 close(WIN);
360
361 $wait = ($wait+1) %6;
362 my $wait_dots = "." x ($wait+1);
363
364 open(WOUT,">$wait_filename");
365 print WOUT "$wait\n";
366 close(WOUT);
367
368 return $wait_dots;
369}
370
371
372sub check_status_single_file
373{
374 my ($filename) = @_;
375
376 my $monitor_filename = get_progress_filename($filename);
377
378 if (! -f $monitor_filename ) {
379 return "";
380 }
381
382 open my $PROGRESS, '<', $monitor_filename or die $!;
383 my $s = do { local $/; <$PROGRESS> };
384 close ($PROGRESS);
385
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;";
391
392 my $style_base = "height:10px; float:left;";
393
394 my $r = "";
395 $r .= "<div>$filename:</div>";
396 $r .= "<nobr>";
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\">&nbsp;$s%</div>";
400 $r .= "</nobr>";
401 $r .= "<br />";
402
403 return $r;
404}
405
406
407sub check_status_all
408{
409 my $file_central = get_file_central_filename();
410
411 my $html = "";
412
413 my $fc_list = read_file_central();
414
415 foreach my $f (@$fc_list) {
416 $html .= check_status_single_file($f);
417 }
418
419 return $html;
420
421}
422
423
424# Accessed from HTML web page through the majic of perlAjax
425
426sub 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');
433
434 if ((defined $filename) && ($filename ne "")) {
435 $html .= check_status_single_file($filename);
436 }
437 else {
438 $html .= check_status_all();
439 }
440
441 return $html;
442}
Note: See TracBrowser for help on using the repository browser.