]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Image.pm
Fix an (irrelevant) confusion about addEventListener.
[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,render_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         if (defined($xres) && defined($yres) && defined($ref->{'render_id'}) && !$infobox) {
50                 # We have a render, we're not asked for the original, and we do not have infobox.
51                 $ref = $dbh->selectrow_hashref('SELECT id,filename,width,height FROM images WHERE id=?', 
52                         undef, $ref->{'render_id'});
53                 return error($r, "Could not find render of $event/$filename", 404, "File not found") unless (defined($ref));
54                 $filename = $ref->{'filename'};
55         }
56
57         $id = $ref->{'id'};
58         $dbwidth = $ref->{'width'};
59         $dbheight = $ref->{'height'};
60
61         # Scale if we need to do so
62         my ($fname, $mime_type);
63         if ($infobox) {
64                 ($fname, $mime_type) = Sesse::pr0n::Common::ensure_infobox_cached($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres);
65         } else {
66                 ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres);
67         }
68
69         # Output the image to the user
70         my $res = Plack::Response->new(200);
71
72         if (!defined($mime_type)) {
73                 $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
74         }
75         $res->content_type($mime_type);
76         
77         my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
78                 or return error($r, "stat of $fname: $!");
79                 
80         $res->content_length($size);
81         Sesse::pr0n::Common::set_last_modified($res, $mtime);
82
83         # # If the client can use cache, by all means do so
84         #if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
85         #       return $rc;
86         #}
87
88         $res->content(IO::File::WithPath->new($fname));
89         return $res;
90 }
91
92 1;
93
94