]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoIndex.pm
Translation support for remaining scripts
[deb/packages.git] / lib / Packages / DoIndex.pm
1 package Packages::DoIndex;
2
3 use strict;
4 use warnings;
5
6 use CGI qw( :cgi );
7 use Exporter;
8
9 use Deb::Versions;
10 use Packages::Config qw( $TOPDIR );
11 use Packages::I18N::Locale;
12 use Packages::CGI;
13
14 our @ISA = qw( Exporter );
15 our @EXPORT = qw( do_index do_allpackages );
16
17 sub do_index {
18     return send_file( 'index', @_ );
19 }
20 sub do_allpackages {
21     return send_file( 'allpackages', @_ );
22 }
23
24 my %encoding = (
25                 'txt.gz' => 'x-gzip',
26                 );
27 sub send_file {
28     my ($file, $params, $opts) = @_;
29     my $cat = $opts->{cat};
30
31     if ($params->{errors}{suite}) {
32         fatal_error( $cat->g( "suite not valid or not specified" ) );
33     }
34     if (@{$opts->{suite}} > 1) {
35         fatal_error( $cat->g( "more than one suite specified for show_static (%s)",
36                               "@{$opts->{suite}}" ) );
37     }
38     if (@{$opts->{subsection}} > 1) {
39         fatal_error( $cat->g( "more than one subsection specified for show_static (%s)",
40                               "@{$opts->{suite}}" ) );
41     }
42
43     my $wwwdir = "$TOPDIR/www";
44     my $path = "";
45     $path .= "source/" if $opts->{source};
46     $path .= "$opts->{suite}[0]/";
47     $path .= "$opts->{archive}[0]/" if @{$opts->{archive}} == 1;
48     $path .= "$opts->{subsection}[0]/" if @{$opts->{subsection}};
49     $path .= "$opts->{priority}[0]/" if @{$opts->{priority}};
50
51     #FIXME: ugly hack
52     if ($opts->{lang} ne 'en' and !-f "$wwwdir/$path$file.$opts->{lang}.$opts->{format}") {
53         $opts->{lang} = 'en';
54     }
55     $path .= "$file.$opts->{lang}.$opts->{format}";
56
57     unless (@Packages::CGI::fatal_errors) {
58         my $buffer;
59         if (open( INDEX, '<', "$wwwdir/$path" )) {
60             my %headers;
61             $headers{'-charset'} = 'UTF-8';
62             $headers{'-type'} = get_mime( $opts->{format}, 'text/plain' );
63             $headers{'-content-encoding'} = $encoding{$opts->{format}} if exists $encoding{$opts->{format}};
64             my ($size,$mtime) = (stat("$wwwdir/$path"))[7,9];
65             $headers{'-content-length'} = $size;
66             $headers{'-last-modified'} = gmtime($mtime);
67             print header( %headers );
68
69             binmode INDEX;
70             while (read INDEX, $buffer, 4096) {
71                 print $buffer;
72             }
73             close INDEX;
74             exit;
75         } else {
76             fatal_error( $cat->g( "couldn't read index file %s: %s",
77                                   $path, $! ) );
78         }
79     }
80 }
81
82 1;
83