blob: aa3f8ce618605b3ab61e8940c5619cef44fc89a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#!/usr/bin/perl -w -I../perllib -I../commonlib/perllib
# upload.cgi:
# Receiver of flash upload files
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: upload.cgi,v 1.2 2008-10-09 14:20:54 matthew Exp $
use strict;
use Standard -db;
use Error qw(:try);
use Image::Magick;
use mySociety::Random qw(random_bytes);
# Main code for index.cgi
sub main {
my $q = shift;
print $q->header(-type => 'text/plain');
my $out = ' ';
try {
my $fh = $q->upload('Filedata');
my $image;
if ($fh) {
$q->delete('photo'); # Can't check content/type when uploaded with Flash
$image = process_photo($fh);
my $name = unpack('H*', random_bytes(12, 1));
open FP, '>/data/vhost/matthew.bci.mysociety.org/photos/' . $name or throw Error::Simple('could not open file');
print FP $image;
close FP;
$out = $name;
};
} catch Error::Simple with {
my $e = shift;
};
print $out;
}
Page::do_fastcgi(\&main);
sub process_photo {
my $fh = shift;
my $photo = Image::Magick->new;
my $err = $photo->Read(file => \*$fh); # Mustn't be stringified
close $fh;
throw Error::Simple("read failed: $err") if "$err";
$err = $photo->Scale(geometry => "250x250>");
throw Error::Simple("resize failed: $err") if "$err";
my @blobs = $photo->ImageToBlob();
undef $photo;
$photo = $blobs[0];
return $photo;
}
|