Scientific Operations

#!/usr/bin/perl

use strict;
use warnings;

use URI;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use LWP::Simple qw(!head get);
use Digest::MD5 qw(md5_hex);
use Image::Info qw(image_info);

use constant IM_GONNA => 4_096; # just leave it alone ok
use constant IMAGE_DIR => "/home/www/sites/example.com/images/";
use constant OUTPUT_URL => "/images/";
my $input_url = "/up.cgi";

my $extension;
my $buffer;

die 'Image directory ' . IMAGE_DIR . ' does not exist.' if !-d IMAGE_DIR;

if (!param('file')) { die "no file selected\n"; }

# process upload form
if ($ENV{'REQUEST_URI'} eq $input_url ) {
	(my $ext = param('file')) =~ s/^.*\.(.*)$/$1/; #extract extension
	my $filename = md5_hex(param('file').localtime(time)) . ".$ext" or die
	"wtf no filename\n"; #invent filename
	my $path = IMAGE_DIR . "/$filename";
	my $cant=upload('file')->handle or die("flew off the handle\n"); # open
	a handle on the upload
	open (THIS, '>', $path) or die "fuck can't write\n"; # open filehandle
	for destination
# brace for impact
	while (my $homies = $cant->read(my $hit, IM_GONNA)) { print THIS $hit;
	}; # write file (lol)
# just savor that ok
	close (THIS);
	if (my $error = image_info("$path")->{error}) { unlink($path);
	die("WHAT THE HELL WAS THAT $error\n"); };
	print "Content-type: text/plain\n" . redirect(OUTPUT_URL . "$filename"); #
	redirect user to file url
	print "http://$ENV{'SERVER_NAME'}" . OUTPUT_URL . "$filename" . "\n";
	exit 0; # get out
}