Changeset 23149


Ignore:
Timestamp:
2010-10-18T00:24:44+13:00 (11 years ago)
Author:
davidb
Message:

Progressbar code upgraded to be able to monitor multiple upload files at once

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/cgi-bin/talkback-progressbar.pl

    r23140 r23149  
    2525
    2626
     27sub get_progress_filename
     28{
     29    my ($uploaded_file) = @_;
     30
     31    my $progress_file = $uploaded_file;
     32
     33    $progress_file =~ s{^(.*)\/}{};
     34    $progress_file =~ s/\.*?$//;
     35    $progress_file .= "-progress.txt";
     36
     37    my $progress_filename = &util::filename_cat($gsdl_tmp_dir, $progress_file);
     38
     39    return $progress_filename;
     40}
     41
     42sub get_file_central_filename
     43{
     44    my $file_central = &util::filename_cat($gsdl_tmp_dir,"file-central.txt");
     45
     46    return $file_central;
     47}
     48
     49sub read_file_central
     50{
     51    my $fc_filename = get_file_central_filename();
     52
     53    my @fc_list;
     54
     55    if (open(FCIN,"<$fc_filename")) {
     56   
     57    my $fc_list_str = do { local $/; <FCIN> };
     58    @fc_list = split(/\n/,$fc_list_str);
     59
     60    close(FCIN);
     61    }
     62    else {
     63    # OK to have no file-central.txt to start with
     64    # return empty list
     65    @fc_list = ();
     66    }
     67
     68    return \@fc_list;
     69   
     70}
     71
     72sub remove_from_file_central
     73{
     74    my ($filename,$fc_list) = @_;
     75
     76    my @new_fc_list = ();
     77
     78    my $removed = 0;
     79
     80    foreach my $f (@$fc_list) {
     81
     82    if ($f ne $filename) {
     83        push(@new_fc_list,$f);
     84    }
     85    else {
     86        $removed = 1;
     87    }
     88    }
     89
     90    if (!$removed) {
     91    print STDERR "Warning: Failed to locate '$filename' in file-central.txt\n";
     92    }
     93
     94    return \@new_fc_list;
     95}
     96
     97sub add_to_file_central
     98{
     99    my ($filename,$fc_list) = @_;
     100
     101    my @new_fc_list = @$fc_list;
     102
     103    my $duplicate = 0;
     104    foreach my $f (@new_fc_list) {
     105
     106    if ($f eq $filename) {
     107        $duplicate = 1;
     108    }
     109    }
     110
     111    if (!$duplicate) {
     112    push(@new_fc_list,$filename);
     113    }
     114    else {
     115    print STDERR "Warning: Ingoring request to add duplicate entry:\n";
     116    print STDERR "         '$filename' into file-central.txt\n"
     117    }
     118
     119    return \@new_fc_list;
     120}
     121
     122
     123
     124sub write_file_central
     125{
     126    my ($fc_list) = @_;
     127
     128    my $fc_filename = get_file_central_filename();
     129
     130    if (open(FCOUT,">$fc_filename")) {
     131   
     132    foreach my $f (@$fc_list) {
     133        print FCOUT "$f\n";
     134    }
     135
     136    close(FCOUT);
     137    }
     138    else {
     139    print STDERR "Error: Failed to write out $fc_filename\n";
     140    print STDERR "$!\n";
     141    }
     142}
     143
    27144sub monitor_upload
    28145{           
    29     my ($filename, $buffer, $bytes_read, $data) = @_;
     146    my ($uploading_file, $buffer, $bytes_read, $data) = @_;
    30147   
    31148    $bytes_read ||= 0;
    32 
    33     my $full_filename = &util::filename_cat($gsdl_tmp_dir,
    34                         "$filename-progress.txt");
    35      
    36     open(COUNTER, ">$full_filename");
     149   
     150    my $progress_filename = get_progress_filename($uploading_file);
     151
     152    if (! -f $progress_filename) {
     153    my $fc_list = read_file_central();
     154    $fc_list = add_to_file_central($uploading_file,$fc_list);
     155    write_file_central($fc_list);
     156    }
     157   
     158    open(COUNTER, ">$progress_filename");
    37159   
    38160    my $per = 0;
     
    46168    # Sleep for 10 msecs
    47169    select(undef, undef, undef, 0.01);
     170    #select(undef, undef, undef, 0.1);
    48171}
    49172
     
    75198sub remove_progress_file
    76199{
    77     my ($file) = @_;
    78 
    79     $file =~ s{^(.*)\/}{};
    80 
    81     my $progress_filename = &util::filename_cat($gsdl_tmp_dir,
    82                         "$file-progress.txt");
     200    my ($uploaded_file) = @_;
     201
     202    my $progress_filename = get_progress_filename($uploaded_file);
    83203
    84204    if (!unlink($progress_filename)) {
    85205    print STDERR "Warning: Failed to delete $progress_filename\n";
    86206    }
     207
     208    my $fc_list = read_file_central();
     209    $fc_list = remove_from_file_central($uploaded_file,$fc_list);
     210    write_file_central($fc_list);
    87211}
    88212
     
    151275main();
    152276
     277
    153278#=====
    154 
    155 
    156279
    157280sub inc_wait_dots
     
    172295}
    173296
     297
    174298sub check_status_single_file
    175299{
    176300    my ($filename) = @_;
    177     $filename =~ s{^(.*)\/}{};
    178 
    179     my $monitor_filename = &util::filename_cat($gsdl_tmp_dir,
    180                            "$filename-progress.txt");
     301   
     302    my $monitor_filename = get_progress_filename($filename);
    181303
    182304    if (! -f  $monitor_filename ) {
     
    197319
    198320    my $r = "";
    199     $r .= "<div style=\"float:left;\">$filename:</div>";
     321    $r .= "<div>$filename:</div>";
     322    $r .= "<nobr>";
    200323    $r .= "<div style=\"width: ${fgwidth}px; $fgcol $style_base\"></div>";
    201324    $r .= "<div style=\"width: ${bgwidth}px; $bgcol $style_base\"></div>";
    202325    $r .= "<div style=\"float:left; width: 80px\">&nbsp;$s%</div>";
    203     $r .= "<br>";
     326    $r .= "</nobr>";
     327    $r .= "<br />";
    204328
    205329    return $r;
     
    209333sub check_status_all
    210334{
    211     my $file_central = &util::filename_cat($gsdl_tmp_dir,"file-central.txt");
     335    my $file_central = get_file_central_filename();
    212336
    213337    my $html = "";
    214338
    215     if (open my $FC, '<', $file_central) {
    216     # Read entire file in all at once
    217     my $file_list_str = do { local $/; <$FC> };
    218     my @file_list = split(/\n/,$file_list_str);
    219 
    220     foreach my $f (@file_list) {
    221         $html .= check_status_single_file($f);
    222     }
    223 
    224     close ($FC);
    225     }
    226     else {
    227     # error
    228     $html = "Failed to open $file_central: $!\n";
     339    my $fc_list = read_file_central();
     340
     341    foreach my $f (@$fc_list) {
     342    $html .= check_status_single_file($f);
    229343    }
    230344
     
    243357
    244358    my $filename = $gsdl_cgi->param('uploadedfile');
    245     if (defined $filename) {
     359
     360    if ((defined $filename) && ($filename ne "")) {
    246361    $html .= check_status_single_file($filename);
    247362    }
Note: See TracChangeset for help on using the changeset viewer.