Add a mechanism for invalidating the front-end cache, if any. Currently it spits...
[pr0n] / perl / Sesse / pr0n / Rotate.pm
1 package Sesse::pr0n::Rotate;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use Apache2::Request;
7
8 sub handler {
9         my $r = shift;
10         my $apr = Apache2::Request->new($r);
11         my $dbh = Sesse::pr0n::Common::get_dbh();
12         my ($user, $takenby) = Sesse::pr0n::Common::check_access($r);
13         if (!defined($user)) {
14                 return Apache2::Const::OK;
15         }
16
17         # FIXME: People can rotate and delete across vhosts using this interface.
18         # We should add some sanity checks.
19
20         my @to_purge = ();
21
22         Sesse::pr0n::Common::header($r, "Rotation/deletion results");
23
24         {
25                 # Enable transactions and error raising temporarily
26                 local $dbh->{AutoCommit} = 0;
27                 local $dbh->{RaiseError} = 1;
28
29                 my @params = $apr->param();
30                 my $key;
31                 for $key (@params) {
32                         # Rotation
33                         if ($key =~ /^rot-(\d+)-(90|180|270)$/ && $apr->param($key) eq 'on') {
34                                 my ($id, $rotval) = ($1,$2);
35                                 my $fname = Sesse::pr0n::Common::get_disk_location($r, $id);
36                                 push @to_purge, Sesse::pr0n::Common::get_all_cache_urls($r, $dbh, $id);
37                                 (my $tmpfname = $fname) =~ s/\.jpg$/-tmp.jpg/;
38
39                                 system("/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && mv '$tmpfname' '$fname'") == 0
40                                         or error($r, "Rotation of $id [/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && mv '$tmpfname' '$fname'] failed: $!.");
41                                 $r->print("    <p>Rotated image ID `$id' by $rotval degrees.</p>\n");
42
43                                 if ($rotval == 90 || $rotval == 270) {
44                                         my $q = $dbh->do('UPDATE images SET height=width,width=height WHERE id=?', undef, $id)
45                                                 or dberror($r, "Size clear of $id failed");
46                                         $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE (vhost,event)=( SELECT vhost,event FROM images WHERE id=? )',
47                                                 undef, $id)
48                                                 or dberror($r, "Cache invalidation at $id failed");
49                                 }
50                         } elsif ($key =~ /^del-(\d+)$/ && $apr->param($key) eq 'on') {
51                                 my $id = $1;
52                                 push @to_purge, Sesse::pr0n::Common::get_all_cache_urls($r, $dbh, $id);
53                                 {
54
55                                         eval {
56                                                 $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE (vhost,event)=( SELECT vhost,event FROM images WHERE id=? )',
57                                                         undef, $id);
58                                                 $dbh->do('INSERT INTO deleted_images SELECT * FROM images WHERE id=?',
59                                                         undef, $id);
60                                                 $dbh->do('DELETE FROM exif_info WHERE image=?',
61                                                         undef, $id);
62                                                 $dbh->do('DELETE FROM images WHERE id=?',
63                                                         undef, $id);
64                                         };
65                                         if ($@) {
66 # Some error occurred, rollback and bomb out
67                                                 $dbh->rollback;
68                                                 dberror($r, "Transaction aborted because $@");
69                                         }
70                                 }
71                                 $r->print("    <p>Deleted image `$id'.</p>\n");
72                         }
73                 }
74         }
75         
76         my $event = $apr->param('event');
77         $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE vhost=? AND event=?', undef, $r->get_server_name, $event)
78                 or dberror($r, "Cache invalidation failed");
79
80         push @to_purge, "/$event/";
81         push @to_purge, "/+all/";
82         Sesse::pr0n::Common::purge_cache($r, @to_purge);
83
84         Sesse::pr0n::Common::footer($r);
85
86         return Apache2::Const::OK;
87 }
88
89 1;
90
91