1 package Sesse::pr0n::Rotate;
5 use Sesse::pr0n::Common qw(error dberror);
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;
17 # FIXME: People can rotate and delete across vhosts using this interface.
18 # We should add some sanity checks.
22 Sesse::pr0n::Common::header($r, "Rotation/deletion results");
25 # Enable transactions and error raising temporarily
26 local $dbh->{RaiseError} = 1;
28 my @params = $apr->param();
31 local $dbh->{AutoCommit} = 0;
34 if ($key =~ /^rot-(\d+)-(90|180|270)$/ && $apr->param($key) eq 'on') {
35 my ($id, $rotval) = ($1,$2);
36 my $fname = Sesse::pr0n::Common::get_disk_location($r, $id);
37 push @to_purge, Sesse::pr0n::Common::get_all_cache_urls($r, $dbh, $id);
38 (my $tmpfname = $fname) =~ s/\.jpg$/-tmp.jpg/;
40 system("/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && /bin/mv '$tmpfname' '$fname'") == 0
41 or error($r, "Rotation of $id [/usr/bin/jpegtran -rotate $rotval -copy all < '$fname' > '$tmpfname' && /bin/mv '$tmpfname' '$fname'] failed: $!.");
42 $r->print(" <p>Rotated image ID `$id' by $rotval degrees.</p>\n");
44 if ($rotval == 90 || $rotval == 270) {
45 my $q = $dbh->do('UPDATE images SET height=width,width=height WHERE id=?', undef, $id)
46 or dberror($r, "Size clear of $id failed");
47 $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE (vhost,event)=( SELECT vhost,event FROM images WHERE id=? )',
49 or dberror($r, "Cache invalidation at $id failed");
51 } elsif ($key =~ /^del-(\d+)$/ && $apr->param($key) eq 'on') {
53 push @to_purge, Sesse::pr0n::Common::get_all_cache_urls($r, $dbh, $id);
57 $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE (vhost,event)=( SELECT vhost,event FROM images WHERE id=? )',
59 $dbh->do('INSERT INTO deleted_images SELECT * FROM images WHERE id=?',
61 $dbh->do('DELETE FROM exif_info WHERE image=?',
63 $dbh->do('DELETE FROM images WHERE id=?',
67 # Some error occurred, rollback and bomb out
69 dberror($r, "Transaction aborted because $@");
72 $r->print(" <p>Deleted image `$id'.</p>\n");
77 my $event = $apr->param('event');
78 $dbh->do('UPDATE last_picture_cache SET last_update=CURRENT_TIMESTAMP WHERE vhost=? AND event=?', undef, $r->get_server_name, $event)
79 or dberror($r, "Cache invalidation failed");
81 push @to_purge, "/$event/";
82 push @to_purge, "/+all/";
83 Sesse::pr0n::Common::purge_cache($r, @to_purge);
85 Sesse::pr0n::Common::footer($r);
87 return Apache2::Const::OK;