diff options
Diffstat (limited to 'bin/rotate-photos')
-rwxr-xr-x | bin/rotate-photos | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/bin/rotate-photos b/bin/rotate-photos index faf2748e6..31a60ff6c 100755 --- a/bin/rotate-photos +++ b/bin/rotate-photos @@ -16,6 +16,8 @@ use FindBin; use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../commonlib/perllib"; +use Digest::SHA1 qw(sha1_hex); + use Utils; use mySociety::Config; use mySociety::DBHandle qw(dbh select_all); @@ -36,17 +38,27 @@ my $r = select_all("select id, photo from problem where service='iPhone'"); foreach (@$r) { my $id = $_->{id}; my $photo = $_->{photo}; - my ($fh, $filename) = mySociety::TempFiles::named_tempfile('.jpeg'); - print $fh $photo; - close $fh; - my $out = `jhead -autorot $filename`; - if ($out) { - open(FP, $filename) or die $!; - $photo = join('', <FP>); - close FP; - Utils::workaround_pg_bytea("update problem set photo=? where id=?", 1, $photo, $id); - dbh()->commit(); + + if (length($photo) == 40) { + # If photo field contains a hash + my $filename = mySociety::Config::get('UPLOAD_DIR') . "$photo.jpeg"; + `jhead -autorot $filename`; + } else { + my ($fh, $filename) = mySociety::TempFiles::named_tempfile('.jpeg'); + print $fh $photo; + close $fh; + my $out = `jhead -autorot $filename`; + if ($out) { + open(FP, $filename) or die $!; + $photo = join('', <FP>); + close FP; + my $fileid = sha1_hex($photo); + rename $filename, mySociety::Config::get('UPLOAD_DIR') . "$fileid.jpeg"; + dbh()->do('UPDATE problem SET photo=? WHERE id=?', {}, $fileid, $id); + dbh()->commit(); + } else { + unlink $filename; + } } - unlink $filename; } |