3 # A small hack to recalculate all existing thumbnails (including mipmaps
4 # if you so desire), for use when we change something in the scaling/encoding
5 # pipeline. You may want to run it piece by piece if you don't want huge
6 # incremental backups, though.
8 # Run as www-data, e.g. with "sudo -u www-data ./update-image-cache.pl".
9 # You can also give it arguments if you want to use multiple threads, with
10 # something like "update-image-cache.pl 0 4" to run as core 0 of four cores.
11 # Remember to adjust $threshold first in any case.
18 use Sesse::pr0n::Config;
20 require Sesse::pr0n::Config_local;
24 package Apache2::ServerUtil;
29 print STDERR $_[1], "\n";
32 package FakeApacheReq;
37 OverloadMode => 'off',
38 OverloadEnableThreshold => '100000.0',
46 print STDERR $_[1], "\n";
49 print STDERR $_[1], "\n";
52 print STDERR $_[1], "\n";
57 use Sesse::pr0n::Common;
61 if ($a == -1 && $b != -1) {
64 if ($a != -1 && $b == -1) {
72 my @sr = sort { ($a->[0] != $b->[0]) ? (byres($a->[0], $b->[0])) : (byres($a->[1], $b->[1])) } @res;
80 # Don't regenerate thumbnails that were made after this. Set this to approximately
81 # when you upgraded pr0n to the version with the new image processing code.
82 my $threshold = `date +%s -d '2009-10-24 11:30'`;
84 my $regen_mipmaps = 0;
85 my $core_id = $ARGV[0] // 0;
86 my $num_cores = $ARGV[1] // 1;
88 my $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
89 $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
90 or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
91 $dbh->{RaiseError} = 1;
93 my $r = bless {}, 'FakeApacheReq';
95 my $q = $dbh->prepare('SELECT id,filename,width,height FROM images WHERE id % ? = ? ORDER BY id DESC');
96 $q->execute($num_cores, $core_id);
98 while (my $ref = $q->fetchrow_hashref) {
99 my $id = $ref->{'id'};
100 my $dir = POSIX::floor($id / 256);
102 my @files = glob("../cache/$dir/$id-*.jpg");
103 if (!$regen_mipmaps) {
104 @files = grep { !/mipmap/ } @files;
109 my $mtime = (stat($c))[9];
110 if ($mtime < $threshold) {
113 if ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
114 push @noboxres, [$1, $2];
117 next unless $any_old;
118 unlink (@files) or die "Could not delete: $!";
119 if (scalar @noboxres > 0) {
120 Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, sort_res(@noboxres));
123 my @newfiles = glob("../cache/$dir/$id-*.jpg");
124 my %a = map { $_ => 1 } @files;
125 my %b = map { $_ => 1 } @newfiles;
128 if (!exists($b{$f})) {
129 print STDERR "Garbage-collected $f\n";