3 # Warning: This is sort of outdated now. :-/ What really should be done is make
4 # update_image_info() includeable from outside mod_perl, so we don't have
5 # to duplicate the code in here.
15 use Sesse::pr0n::Config;
17 require Sesse::pr0n::Config_local;
20 my $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
21 $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
22 or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
23 $dbh->{RaiseError} = 1;
25 my $q = $dbh->prepare('SELECT id FROM images WHERE id NOT IN ( SELECT DISTINCT image FROM exif_info ) ORDER BY id');
28 while (my $ref = $q->fetchrow_hashref) {
29 my $id = $ref->{'id'};
31 # Copied almost verbatim from Sesse::pr0n::Common::update_image_info
32 my $info = Image::ExifTool::ImageInfo(get_disk_location($id));
33 my $width = $info->{'ImageWidth'} || -1;
34 my $height = $info->{'ImageHeight'} || -1;
37 if (defined($info->{'DateTimeOriginal'})) {
38 # Parse the date and time over to ISO format
39 if ($info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)(?:\+\d\d:\d\d)?$/ && $1 > 1990) {
40 $datetime = "$1-$2-$3 $4:$5:$6";
45 local $dbh->{AutoCommit} = 0;
47 $dbh->do('UPDATE images SET width=?, height=?, date=? WHERE id=?',
48 undef, $width, $height, $datetime, $id)
49 or die "Couldn't update width/height in SQL: $!";
51 $dbh->do('DELETE FROM exif_info WHERE image=?',
53 or die "Couldn't delete old EXIF information in SQL: $!";
55 my $q = $dbh->prepare('INSERT INTO exif_info (image,tag,value) VALUES (?,?,?)')
56 or die "Couldn't prepare inserting EXIF information: $!";
58 for my $key (keys %$info) {
59 next if ref $info->{$key};
60 $q->execute($id, $key, guess_charset($info->{$key}))
61 or die "Couldn't insert EXIF information in database: $!";
64 # update the last_picture cache as well (this should of course be done
65 # via a trigger, but this is less complicated :-) )
66 $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE event=(SELECT event FROM images WHERE id=?)',
67 undef, $datetime, $id)
68 or die "Couldn't update last_picture in SQL: $!";
71 print "Updated $id.\n";
74 sub get_disk_location {
76 my $dir = POSIX::floor($id / 256);
77 return "/srv/pr0n.sesse.net/images/$dir/$id.jpg";
85 $decoded = Encode::decode("utf-8", $text, Encode::FB_CROAK);
88 $decoded = Encode::decode("iso8859-1", $text);