]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoDownload.pm
Restore translation support in the Perl modules
[deb/packages.git] / lib / Packages / DoDownload.pm
1 package Packages::DoDownload;
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use CGI ();
8 use DB_File;
9 use Benchmark ':hireswallclock';
10 use Exporter;
11
12 use Deb::Versions;
13 use Packages::I18N::Locale;
14 use Packages::Search qw( :all );
15 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @SECTIONS @ARCHITECTURES );
16 use Packages::CGI;
17 use Packages::DB;
18
19 our @ISA = qw( Exporter );
20 our @EXPORT = qw( do_download );
21
22
23 sub do_download {
24     my ($params, $opts, $page_content) = @_;
25     my $cat = $opts->{cat};
26
27     if ($params->{errors}{package}) {
28         fatal_error( $cat->g( "package not valid or not specified" ) );
29     }
30     if ($params->{errors}{suite}) {
31         fatal_error( $cat->g( "suite not valid or not specified" ) );
32     }
33     if ($params->{errors}{arch}) {
34         fatal_error( $cat->g( "architecture not valid or not specified" ) );
35     }
36     if (@{$opts->{suite}} > 1) {
37         fatal_error( $cat->g( "more than one suite specified for download (%s)",
38                               "@{$opts->{suite}}" ) );
39     }
40     if (@{$opts->{arch}} > 1) {
41         fatal_error( $cat->g( "more than one architecture specified for download (%s)",
42                               "@{$opts->{arch}}" ) );
43     }
44
45     $opts->{h_sections} = { map { $_ => 1 } @SECTIONS };
46     my $pkg = $opts->{package};
47     my $suite = $opts->{suite}[0];
48     my $arch = $opts->{arch}[0] ||'';
49
50     our (%packages_all);
51     my (@results);
52     my ($final_result, $filename, $directory, @file_components, $archive) = ("")x5;
53
54     my $st0 = new Benchmark;
55     unless (@Packages::CGI::fatal_errors) {
56         tie %packages_all, 'DB_File', "$DBDIR/packages_all_$suite.db",
57         O_RDONLY, 0666, $DB_BTREE
58             or die "couldn't tie DB $DBDIR/packages_all_$suite.db: $!";
59         
60         read_entry( \%packages, $pkg, \@results, $opts );
61
62         @results = grep { $_->[7] ne 'v' } @results;
63         unless (@results) {
64 #           fatal_error( _g( "No such package." )."<br>".
65 #                        sprintf( _g( '<a href="%s">Search for the package</a>' ), "$SEARCH_URL/$pkg" ) );
66         } else {
67             my $final_result = shift @results;
68             foreach (@results) {
69                 if (version_cmp( $_->[7], $final_result->[7] ) > 0) {
70                     $final_result = $_;
71                 }
72             }
73             
74             debug( "final_result=@$final_result", 1 );
75             $archive = $final_result->[1];
76             my %data = split /\000/, $packages_all{"$pkg $arch $final_result->[7]"};
77             if (!%data && $arch ne 'all' && $final_result->[3] eq 'all') {
78                 %data = split /\000/, $packages_all{"$pkg all $final_result->[7]"};
79                 $arch = 'all';
80                 debug( "choosing arch 'all' instead of requested arch $arch", 1 );
81 #               fatal_error( _g( "No such package." )."<br>".
82 #                            sprintf( _g( '<a href="%s">Search for the package</a>' ), "$SEARCH_URL/$pkg" ) ) unless %data;
83             }
84             @file_components = split('/', $data{filename});
85             $filename = pop(@file_components);
86             $directory = join( '/', @file_components).'/';
87
88             $page_content->{archive} = $archive;
89             $page_content->{suite} = $suite;
90             $page_content->{pkg} = $pkg;
91             my $pkgsize = floor(($data{size}/102.4)+0.5)/10;
92             if ($pkgsize < 1024) {
93                 $page_content->{pkgsize} = sprintf( '%.1f', $pkgsize );
94                 $page_content->{pkgsize_unit} = $cat->g( 'kByte' );
95             } else {
96                 $page_content->{pkgsize} = sprintf( '%.1f', floor(($data{size}/(102.4*102.4))+0.5)/100 );
97                 $page_content->{pkgsize_unit} = $cat->g( 'MByte' );
98             }
99             $page_content->{architecture} = $arch;
100             foreach (keys %data) {
101                 $page_content->{$_} = $data{$_};
102             }
103             $page_content->{filename} = { file => $filename,
104                                           directory => $directory,
105                                           full => $data{filename},
106                                       };
107
108         }
109     }
110         
111 }
112
113 1;