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