Show download.html syntax highlighted
% # vim:set syntax=mason:
<%args>
$file => ''
$password => ''
$download => ''
</%args>
<%init>
# look up file
my %control;
my $message;
my $cipher;
my $buffer;
$password =~ s/\"//g;
my $dir = $config->get('large_file_dir', '/tmp');
my $openssl = $config->get('openssl', '/usr/bin/openssl');
my $fn = $dir . "/" . $file;
eval {
if(!open DATA, "<$fn.rec") {
$message = "The file could not be found. This system only caches files for a limited time. The file may have expired, or the URL you used may have been corrupted.";
die "Download failure";
}
while(<DATA>) {
m/^(\w+):\s(.*)$/;
$control{$1} = $2;
}
close DATA;
$control{downloads}++;
open DATA, ">$fn.rec";
for my $k (sort keys %control) {
print DATA "$k: $control{$k}\n";
}
close DATA;
if($control{downloads} > $config->get('large_file_max_downloads', 21)) {
$message = "The download count for this file has been exceeded. It has been removed.";
unlink "$fn";
# Leave the .rec file for timeout, so that the proper message will
# still be generated.
}
# check the password
my $password_hash = md5_hex($password);
if($control{password} ne $password_hash) {
$message = "Password incorrect.";
die "Bad password";
}
# send the file
$r->header_out('Content-Disposition' => qq(attachment; filename=$control{filename}));
if($download =~ m/force/) { # Safari is broken...octet-stream goes to window
$r->content_type("image/pnm");
} else {
$r->content_type("application/octet-stream");
}
$r->send_http_header;
if(!open(DATA, "nice -20 $openssl enc -d -cast5-cbc -k '$password' -in $fn 2>/dev/null|")) {
$message = "The file was unreadable. Contact support.";
die "Unable to read file.";
}
while (read(DATA,$buffer,8192)) {
$m->print($buffer);
$m->flush_buffer;
}
# use Crypt::CBC to decrypt, then send
#$cipher = Crypt::CBC->new( -key => $password,
#-cipher => 'CAST5',
#-salt => 1,
#);
#$cipher->start('decrypt');
#if(!open(DATA,"<$fn")) {
# $message = "The file was unreadable. Contact support.";
# die "Unable to read file.";
#}
# FIXME: More efficient! This SUCKS CPU
#while (read(DATA,$buffer,1024)) {
# $m->print($cipher->crypt($buffer));
# $m->flush_buffer;
#}
#$m->print($cipher->finish);
close DATA;
};
if($@) {
$m->redirect(build_url('/filevault.html', {message => $message }));
}
</%init>
<%once>
use AlphaMail::HTMLHelper qw(build_url);
use Digest::MD5 qw(md5_hex);
use Crypt::CBC;
</%once>
See more files for this project here