]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Image.pm
Fix text fading.
[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 (nobox/ is for compatibility with legacy URLs).
17         my ($event,$filename,$xres,$yres,$dpr);
18         my $infobox = 0;
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 = 1 if (defined($2) && $2 eq 'box/');
23         } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)(?:\@(\d+(?:\.\d+)?))?/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) {
24                 $event = $1;
25                 $filename = $6;
26                 $xres = $2;
27                 $yres = $3;
28                 $dpr = $4;
29                 $infobox = 1 if (defined($5) && $5 eq 'box/');
30         } elsif ($r->path_info =~ m#^/([a-zA-Z0-9-]+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) {
31                 $event = $1;
32                 $filename = $3;
33                 $xres = -1;
34                 $yres = -1;
35                 $infobox = 1 if (defined($2) && $2 eq 'box/');
36         }
37         $dpr //= 1;
38
39         my ($id, $dbwidth, $dbheight);
40         #if ($event eq 'single' && $filename =~ /^(\d+)\.jpeg$/) {
41         #       $id = $1;
42         #} else {
43         
44         # Look it up in the database
45         my $ref = $dbh->selectrow_hashref('SELECT id,width,height FROM images WHERE event=? AND vhost=? AND filename=?',
46                 undef, $event, Sesse::pr0n::Common::get_server_name($r), $filename);
47         return error($r, "Could not find $event/$filename", 404, "File not found") unless (defined($ref));
48
49         $id = $ref->{'id'};
50         $dbwidth = $ref->{'width'};
51         $dbheight = $ref->{'height'};
52
53         # Scale if we need to do so
54         my ($fname, $mime_type);
55         if ($infobox) {
56                 ($fname, $mime_type) = Sesse::pr0n::Common::ensure_infobox_cached($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres);
57         } else {
58                 ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres);
59         }
60
61         # Output the image to the user
62         my $res = Plack::Response->new(200);
63
64         if (!defined($mime_type)) {
65                 $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
66         }
67         $res->content_type($mime_type);
68         
69         my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
70                 or return error($r, "stat of $fname: $!");
71                 
72         $res->content_length($size);
73         Sesse::pr0n::Common::set_last_modified($res, $mtime);
74
75         # # If the client can use cache, by all means do so
76         #if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
77         #       return $rc;
78         #}
79
80         $res->content(IO::File::WithPath->new($fname));
81         return $res;
82 }
83
84 1;
85
86