]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Image.pm
Lock the number of pictures per page at 250.
[pr0n] / perl / Sesse / pr0n / Image.pm
1 package Sesse::pr0n::Image;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use POSIX;
7
8 sub handler {
9         my $r = shift;
10         my $dbh = Sesse::pr0n::Common::get_dbh();
11
12 #       if ($r->connection->remote_ip() eq '80.212.251.227') {
13 #               die "Har du lest FAQen?";
14 #       }
15
16         # Find the event and file name
17         my ($event,$filename,$xres,$yres,$dpr);
18         my $infobox = 'both';
19         if ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/original/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) {
20                 $event = $1;
21                 $filename = $3;
22                 $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/');
23                 $infobox = 'box' if (defined($2) && $2 eq 'box/');
24         } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)(?:\@(\d+(?:\.\d+)?))?/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) {
25                 $event = $1;
26                 $filename = $6;
27                 $xres = $2;
28                 $yres = $3;
29                 $dpr = $4;
30                 $infobox = 'nobox' if (defined($5) && $5 eq 'nobox/');
31                 $infobox = 'box' if (defined($5) && $5 eq 'box/');
32         } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) {
33                 $event = $1;
34                 $filename = $3;
35                 $xres = -1;
36                 $yres = -1;
37                 $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/');
38                 $infobox = 'box' if (defined($2) && $2 eq 'box/');
39         }
40         $dpr //= 1;
41
42         my ($id, $dbwidth, $dbheight);
43         #if ($event eq 'single' && $filename =~ /^(\d+)\.jpeg$/) {
44         #       $id = $1;
45         #} else {
46         
47         # Look it up in the database
48         my $ref = $dbh->selectrow_hashref('SELECT id,width,height FROM images WHERE event=? AND vhost=? AND filename=?',
49                 undef, $event, Sesse::pr0n::Common::get_server_name($r), $filename);
50         return error($r, "Could not find $event/$filename", 404, "File not found") unless (defined($ref));
51
52         $id = $ref->{'id'};
53         $dbwidth = $ref->{'width'};
54         $dbheight = $ref->{'height'};
55
56         # Scale if we need to do so
57         my ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres);
58
59         # Output the image to the user
60         my $res = Plack::Response->new(200);
61
62         if (!defined($mime_type)) {
63                 $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
64         }
65         $res->content_type($mime_type);
66         
67         my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
68                 or return error($r, "stat of $fname: $!");
69                 
70         $res->content_length($size);
71         Sesse::pr0n::Common::set_last_modified($res, $mtime);
72
73         # # If the client can use cache, by all means do so
74         #if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
75         #       return $rc;
76         #}
77
78         $res->content(IO::File::WithPath->new($fname));
79         return $res;
80 }
81
82 1;
83
84