Make sure rotation and selection invalidate the listing cache.
[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         Sesse::pr0n::Common::header($r, "Rotation/deletion results");
18
19         {
20                 # Enable transactions and error raising temporarily
21                 local $dbh->{AutoCommit} = 0;
22                 local $dbh->{RaiseError} = 1;
23
24                 my @params = $apr->param();
25                 my $key;
26                 for $key (@params) {
27                         # Rotation
28                         if ($key =~ /^rot-(\d+)-(90|180|270)$/ && $apr->param($key) eq 'on') {
29                                 my ($id, $rotval) = ($1,$2);
30                                 my $fname = Sesse::pr0n::Common::get_disk_location($r, $id);
31                                 (my $tmpfname = $fname) =~ s/\.jpg$/-tmp.jpg/;
32
33                                 system("/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && mv '$tmpfname' '$fname'") == 0
34                                         or error($r, "Rotation of $id [/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && mv '$tmpfname' '$fname'] failed: $!.");
35                                 $r->print("    <p>Rotated image ID `$id' by $rotval degrees.</p>\n");
36
37                                 if ($rotval == 90 || $rotval == 270) {
38                                         my $q = $dbh->do('UPDATE images SET height=width,width=height WHERE id=?', undef, $id)
39                                                 or dberror($r, "Size clear of $id failed");
40                                         $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE id=( SELECT event FROM images WHERE id=? )',
41                                                 undef, $id)
42                                                 or dberror($r, "Cache invalidation at $id failed");
43                                 }
44                         } elsif ($key =~ /^del-(\d+)$/ && $apr->param($key) eq 'on') {
45                                 my $id = $1;
46                                 {
47
48                                         eval {
49                                                 $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE id=( SELECT event FROM images WHERE id=? )',
50                                                         undef, $id);
51                                                 $dbh->do('INSERT INTO deleted_images SELECT * FROM images WHERE id=?',
52                                                         undef, $id);
53                                                 $dbh->do('DELETE FROM images WHERE id=?',
54                                                         undef, $id);
55                                         };
56                                         if ($@) {
57 # Some error occurred, rollback and bomb out
58                                                 $dbh->rollback;
59                                                 dberror($r, "Transaction aborted because $@");
60                                         }
61                                 }
62                                 $r->print("    <p>Deleted image `$id'.</p>\n");
63                         }
64                 }
65         }
66         
67         my $event = $apr->param('event');
68         $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE id=?', undef, $event)
69                 or dberror($r, "Cache invalidation failed");
70
71         Sesse::pr0n::Common::footer($r);
72
73         return Apache2::Const::OK;
74 }
75
76 1;
77
78