aboutsummaryrefslogtreecommitdiffstats
path: root/web/upload.cgi
diff options
context:
space:
mode:
Diffstat (limited to 'web/upload.cgi')
-rwxr-xr-xweb/upload.cgi56
1 files changed, 56 insertions, 0 deletions
diff --git a/web/upload.cgi b/web/upload.cgi
new file mode 100755
index 000000000..bc42716f8
--- /dev/null
+++ b/web/upload.cgi
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -w -I../perllib -I../../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.1 2008-03-29 03:03:35 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));
+ 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;
+}
+