Invalidate cache on deletion.
[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                                 }
41                         } elsif ($key =~ /^del-(\d+)$/ && $apr->param($key) eq 'on') {
42                                 my $id = $1;
43                                 {
44
45                                         eval {
46                                                 $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE event=( SELECT event FROM images WHERE id=? )',
47                                                         undef, $id);
48                                                 $dbh->do('INSERT INTO deleted_images SELECT * FROM images WHERE id=?',
49                                                                 undef, $id);
50                                                 $dbh->do('DELETE FROM images WHERE id=?',
51                                                                 undef, $id);
52                                         };
53                                         if ($@) {
54 # Some error occurred, rollback and bomb out
55                                                 $dbh->rollback;
56                                                 dberror($r, "Transaction aborted because $@");
57                                         }
58                                 }
59                                 $r->print("    <p>Deleted image `$id'.</p>\n");
60                         }
61                 }
62         }
63
64         Sesse::pr0n::Common::footer($r);
65
66         return Apache2::Const::OK;
67 }
68
69 1;
70
71