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

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

CGI scripts need to know the fromCollect and toCollect

  • Property svn:executable set to *
File size: 8.9 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 = 1; # if 1, debugging is enabled and 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 if (open(FCOUT,">$fc_filename")) {
133
134 foreach my $f (@$fc_list) {
135 print FCOUT "$f\n";
136 }
137
138 close(FCOUT);
139 }
140 else {
141 print STDERR "Error: Failed to write out $fc_filename\n";
142 print STDERR "$!\n";
143 }
144}
145
146sub monitor_upload
147{
148 my ($uploading_file, $buffer, $bytes_read, $data) = @_;
149
150 $bytes_read ||= 0;
151
152 my $progress_filename = get_progress_filename($uploading_file);
153
154 if (! -f $progress_filename) {
155 my $fc_list = read_file_central();
156 $fc_list = add_to_file_central($uploading_file,$fc_list);
157 write_file_central($fc_list);
158 }
159
160 open(COUNTER, ">$progress_filename");
161
162 my $per = 0;
163 if ($ENV{CONTENT_LENGTH} > 0) {
164 $per = int(($bytes_read * 100) / $ENV{CONTENT_LENGTH});
165 }
166 print COUNTER $per;
167 close(COUNTER);
168
169 # Useful debug to slow down a 'fast' upload
170 # Sleep for 10 msecs
171 # select(undef, undef, undef, 0.01);
172 #select(undef, undef, undef, 0.1);
173}
174
175
176
177sub upload_file {
178
179 my ($gsdl_cgi,$full_filename) = @_;
180
181 my $fh = $gsdl_cgi->upload('uploadedfile');
182 my $filename = $gsdl_cgi->param('uploadedfile');
183
184 return '' if ! $filename;
185
186 open (OUTFILE, '>' . $full_filename)
187 || die("Can't write to $full_filename: $!");
188
189 binmode(OUTFILE);
190
191 while (my $bytesread = read($fh, my $buffer, 1024)) {
192 print OUTFILE $buffer;
193 }
194
195 close (OUTFILE);
196 chmod(0666, $full_filename);
197
198}
199
200sub remove_progress_file
201{
202 my ($uploaded_file) = @_;
203
204 my $progress_filename = get_progress_filename($uploaded_file);
205
206 unlink($progress_filename)
207 unless $debugging_enabled;
208
209 my $fc_list = read_file_central();
210 $fc_list = remove_from_file_central($uploaded_file,$fc_list);
211 write_file_central($fc_list);
212}
213
214
215sub unzip_archives_doc
216{
217 my ($gsdl_cgi,$gsdl_home,$collect_home,$collect,$zip_filename) = @_;
218
219 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
220
221 my $import_dir = &util::filename_cat($collect_home,$collect,"import");
222
223 # Unzip $zip_filename in the collection's import folder
224 my $java = $gsdl_cgi->get_java_path();
225 my $jar_dir= &util::filename_cat($gsdl_home, "bin", "java");
226 my $java_classpath = &util::filename_cat($jar_dir,"GLIServer.jar");
227
228 my $java_args = "\"$zip_filename\" \"$import_dir\"";
229
230 $ENV{'LANG'} = $lang_env;
231 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
232
233 my $java_output = `$java_command`;
234 my $java_status = $?;
235 if ($java_status > 0) {
236 my $report = "Java failed: $java_command\n--\n";
237 $report .= "$java_output\n";
238 $report .= "Exit status: " . ($java_status / 256) . "\n";
239 $report .= $gsdl_cgi->check_java_home();
240
241 $gsdl_cgi->generate_error($report);
242 }
243 else {
244 # Remove the zip file once we have unzipped it, since it is an intermediate file only
245 unlink($zip_filename) unless $debugging_enabled;
246 }
247
248# print STDOUT "Content-type:text/plain\n\n";
249# print STDOUT "cmd = $java_command\n";
250# print STDOUT "**** $java_output\n";
251
252}
253
254
255sub main {
256
257 # gsdlCGI->prenew() constructs a 'lite' version of the object where the
258 # GSDL environment has been setup
259 #
260 # This is needed because the main call the gsdlCGI->new takes an
261 # initializer rountine -- monitor_upload() -- as a parameter, AND THAT
262 # routine (which is called before the constructor is finished) needs to
263 # know the tmp directory to use to write out the progress file.
264
265 my $gsdl_config = gsdlCGI->prenew();
266
267 $gsdl_home = $gsdl_config->get_gsdl_home();
268 $gsdl_tmp_dir = &util::get_toplevel_tmp_dir();
269
270 require talkback;
271
272 # Use the initializer mechanism so a 'callback' routine can monitor
273 # the progress of how much data has been uploaded
274
275 $gsdl_cgi = gsdlCGI->new(\&monitor_upload);
276
277
278 require CGI::Ajax;
279
280 my $perlAjax = new CGI::Ajax('check_status' => \&check_status);
281
282
283 if ($gsdl_cgi->param('process')) {
284
285 my $uploaded_file = $gsdl_cgi->param('uploadedfile');
286 my $full_filename = &util::filename_cat($gsdl_tmp_dir,$uploaded_file);
287
288 my $done_html = &talkback::generate_done_html($full_filename);
289
290 if ($gsdl_cgi->param('yes_upload')) {
291 upload_file($gsdl_cgi,$full_filename);
292
293 my $collect = $gsdl_cgi->param('toCollect');
294 my $collect_home = &util::filename_cat($gsdl_home,"collect");
295
296 unzip_archives_doc($gsdl_cgi,$gsdl_home,$collect_home,$collect,$full_filename);
297 }
298
299 print $gsdl_cgi->header();
300 print $done_html;
301
302 remove_progress_file($uploaded_file);
303 }
304 else {
305
306 my $upload_html_form;
307
308 #my $oid = $gsdl_cgi->param('oid');
309 #my $collect = $gsdl_cgi->param('collect');
310 my $uniq_file = $gsdl_cgi->param('uploadedfile');
311
312 #my $uniq_file = "$collect-$oid-doc.zip";
313 # Light-weight (hidden) form with progress bar
314
315 $upload_html_form
316 = &talkback::generate_upload_form_progressbar($uniq_file);
317
318 print $perlAjax->build_html($gsdl_cgi, $upload_html_form);
319 }
320}
321
322
323main();
324
325
326#=====
327
328sub inc_wait_dots
329{
330 my $wait_filename = &util::filename_cat($gsdl_tmp_dir,"wait.txt");
331 open(WIN,"<$wait_filename");
332 my $wait = <WIN>;
333 close(WIN);
334
335 $wait = ($wait+1) %6;
336 my $wait_dots = "." x ($wait+1);
337
338 open(WOUT,">$wait_filename");
339 print WOUT "$wait\n";
340 close(WOUT);
341
342 return $wait_dots;
343}
344
345
346sub check_status_single_file
347{
348 my ($filename) = @_;
349
350 my $monitor_filename = get_progress_filename($filename);
351
352 if (! -f $monitor_filename ) {
353 return "";
354 }
355
356 open my $PROGRESS, '<', $monitor_filename or die $!;
357 my $s = do { local $/; <$PROGRESS> };
358 close ($PROGRESS);
359
360 my $fgwidth = int($s * 1.5);
361 my $bgwidth = 150 - $fgwidth;
362
363 my $fgcol = "background-color:#dddd00;";
364 my $bgcol = "background-color:#008000;";
365
366 my $style_base = "height:10px; float:left;";
367
368 my $r = "";
369 $r .= "<div>$filename:</div>";
370 $r .= "<nobr>";
371 $r .= "<div style=\"width: ${fgwidth}px; $fgcol $style_base\"></div>";
372 $r .= "<div style=\"width: ${bgwidth}px; $bgcol $style_base\"></div>";
373 $r .= "<div style=\"float:left; width: 80px\">&nbsp;$s%</div>";
374 $r .= "</nobr>";
375 $r .= "<br />";
376
377 return $r;
378}
379
380
381sub check_status_all
382{
383 my $file_central = get_file_central_filename();
384
385 my $html = "";
386
387 my $fc_list = read_file_central();
388
389 foreach my $f (@$fc_list) {
390 $html .= check_status_single_file($f);
391 }
392
393 return $html;
394
395}
396
397
398# Accessed from HTML web page through the majic of perlAjax
399
400sub check_status
401{
402
403 my $wait_dots = inc_wait_dots();
404 my $html = "$wait_dots<br>";
405
406 my $filename = $gsdl_cgi->param('uploadedfile');
407
408 if ((defined $filename) && ($filename ne "")) {
409 $html .= check_status_single_file($filename);
410 }
411 else {
412 $html .= check_status_all();
413 }
414
415 return $html;
416}
Note: See TracBrowser for help on using the repository browser.