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