]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoIndex.pm
Move common functions for template use to Packages::Template
[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
30     if ($params->{errors}{suite}) {
31         fatal_error( _g( "suite not valid or not specified" ) );
32     }
33     if (@{$opts->{suite}} > 1) {
34         fatal_error( sprintf( _g( "more than one suite specified for show_static (%s)" ), "@{$opts->{suite}}" ) );
35     }
36     if (@{$opts->{subsection}} > 1) {
37         fatal_error( sprintf( _g( "more than one suite specified for show_static (%s)" ), "@{$opts->{suite}}" ) );
38     }
39
40     my $wwwdir = "$TOPDIR/www";
41     my $path = "";
42     $path .= "source/" if $opts->{source};
43     $path .= "$opts->{suite}[0]/";
44     $path .= "$opts->{archive}[0]/" if @{$opts->{archive}} == 1;
45     $path .= "$opts->{subsection}[0]/" if @{$opts->{subsection}};
46     $path .= "$opts->{priority}[0]/" if @{$opts->{priority}};
47
48     #FIXME: ugly hack
49     if ($opts->{lang} ne 'en' and !-f "$wwwdir/$path$file.$opts->{lang}.$opts->{format}") {
50         $opts->{lang} = 'en';
51     }
52     $path .= "$file.$opts->{lang}.$opts->{format}";
53
54     unless (@Packages::CGI::fatal_errors) {
55         my $buffer;
56         if (open( INDEX, '<', "$wwwdir/$path" )) {
57             my %headers;
58             $headers{'-charset'} = get_charset( $opts->{lang} );
59             $headers{'-type'} = get_mime( $opts->{format}, 'text/plain' );
60             $headers{'-content-encoding'} = $encoding{$opts->{format}} if exists $encoding{$opts->{format}};
61             my ($size,$mtime) = (stat("$wwwdir/$path"))[7,9];
62             $headers{'-content-length'} = $size;
63             $headers{'-last-modified'} = gmtime($mtime);
64             print header( %headers );
65
66             binmode INDEX;
67             while (read INDEX, $buffer, 4096) {
68                 print $buffer;
69             }
70             close INDEX;
71             exit;
72         } else {
73             fatal_error( sprintf( _g( "couldn't read index file %s: %s" ),
74                                   $path, $! ) );
75         }
76     }
77 }
78
79 1;
80