]> git.sesse.net Git - pr0n/blob - perl/update-image-cache.pl
Fix an (irrelevant) confusion about addEventListener.
[pr0n] / perl / update-image-cache.pl
1 #! /usr/bin/perl
2
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.
7
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.
12
13 use lib qw(.);
14 use DBI;
15 use strict;
16 use warnings;
17
18 use Sesse::pr0n::Config;
19 eval {
20         require Sesse::pr0n::Config_local;
21 };
22
23 # Hack :-)
24 package Apache2::ServerUtil;
25 sub server {
26         return bless {};
27 }
28 sub log_error {
29         print STDERR $_[1], "\n";
30 }
31
32 package FakeApacheReq;
33 sub dir_config {
34         my $key = $_[1];
35         my %config = (
36                 ImageBase => '../',
37                 OverloadMode => 'off',
38                 OverloadEnableThreshold => '100000.0',
39         );
40         return $config{$key};
41 }
42 sub log {
43         return bless {};
44 }
45 sub info {
46         print STDERR $_[1], "\n";
47 }
48 sub warn {
49         print STDERR $_[1], "\n";
50 }
51 sub error {
52         print STDERR $_[1], "\n";
53 }
54 sub logger {
55 }
56 package main;
57 use Sesse::pr0n::Common;
58
59 sub byres {
60         my ($a, $b) = @_;
61         if ($a == -1 && $b != -1) {
62                 return -1;
63         }       
64         if ($a != -1 && $b == -1) {
65                 return 1;
66         }
67         return ($a <=> $b);
68 }
69
70 sub sort_res {
71         my (@res) = @_;
72         my @sr = sort { ($a->[0] != $b->[0]) ? (byres($a->[0], $b->[0])) : (byres($a->[1], $b->[1])) } @res;
73         my @ret = ();
74         for my $r (@sr) {
75                 push @ret, @$r;
76         }
77         return @ret;
78 }
79         
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'`;
83 chomp $threshold;
84 my $regen_mipmaps = 0;
85 my $core_id = $ARGV[0] // 0;
86 my $num_cores = $ARGV[1] // 1;
87
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;
92
93 my $r = bless {}, 'FakeApacheReq';
94
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);
97
98 while (my $ref = $q->fetchrow_hashref) {
99         my $id = $ref->{'id'};
100         my $dir = POSIX::floor($id / 256);
101
102         my @files = glob("../cache/$dir/$id-*.jpg");
103         if (!$regen_mipmaps) {
104                 @files = grep { !/mipmap/ } @files;
105         }
106         my @noboxres = ();
107         my $any_old = 0;
108         for my $c (@files) {
109                 my $mtime = (stat($c))[9];
110                 if ($mtime < $threshold) {
111                         $any_old = 1;
112                 }
113                 if ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
114                         push @noboxres, [$1, $2];
115                 }
116         }
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));
121         }
122         
123         my @newfiles = glob("../cache/$dir/$id-*.jpg");
124         my %a = map { $_ => 1 } @files;
125         my %b = map { $_ => 1 } @newfiles;
126
127         for my $f (@files) {
128                 if (!exists($b{$f})) {
129                         print STDERR "Garbage-collected $f\n";
130                 }
131         }
132 }
133