Hi ajbrun,
I have put a quick script together. I was hoping to use a Bzip2 compression module but had problems with Compress::Bzip2.
So i simply saved each file and the called bzip2 through the system. You must note that to be safe you should check the filename thoroughly (I put in a test that the filename is in the format 'abc.ext'.
also note the vars for the location of the files and the redirection URL
Let me know how you get on and if you need further help!
Code:
#!/usr/bin/perl
use CGI;
use File::Basename;
use strict;
# set redirect URL here
my $redirect_url = 'http://redirect-url';
# change directory to repository
chdir('/path/to/repository/');
# create CGI object
my $cgi = CGI->new();
foreach my $param ($cgi->param()) {
if (my $upload = $cgi->upload($param)) {
# get file name
my $filename = $cgi->param($param);
if ($filename =~ /\\/) {
fileparse_set_fstype('MSWin32');
}
$filename = basename($filename);
# CHECK FILE NAME
# use regexes to make sure filename won't cause security problems when handed to bzip
die unless ($filename =~ /^\w+\.\w+$/);
# continue if file exists
next if (-e $filename . '.bz2');
# save file
open(FILE, '>', $filename);
while (my $bytes_read = read($upload, my $buffer, 1024)) {
print FILE $buffer;
}
close(FILE);
# zip
system("/bin/bzip2 -qz '$filename'");
}
}
print $cgi->redirect($redirect_url);