Show download.html syntax highlighted
% # vim:set syntax=mason:
<%args>
$file => ''
$password => ''
$download => ''
</%args>
<%init>
my $logger = Log::Log4perl->get_logger('alphamail');
# 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 $sandbox_dir = $config->get('sandbox');
my $sandbox = $config->get('sandbox_util');
my $base = $config->get('base_uri');
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.
die "Limit exceeded";
}
# check the password
my $password_hash = md5_hex($password);
if($control{password} ne $password_hash) {
$message = "Password incorrect.";
die "Bad password";
}
unlink "$sandbox_dir/tmp/$file" if(-f "$sandbox_dir/tmp/$file");
link $fn, "$sandbox_dir/tmp/$file" or $logger->error("Could not create link from $fn to $sandbox_dir/tmp/$file");
if(!open(DATA, "$sandbox $openssl enc -d -cast5-cbc -k '$password' -in '/tmp/$file' |")) {
$message = "The file was unreadable. Contact support.";
die "Unable to read file.";
}
# send the file
if($download =~ m/force/) { # Safari is broken...octet-stream goes to window
$r->content_type("application/unknown");
} else {
$r->content_type("application/octet-stream");
}
$r->headers_out()->set('Content-Disposition' => qq(attachment; filename="$control{filename}"));
while (read(DATA,$buffer,8192)) {
$m->print($buffer);
$m->flush_buffer;
}
close DATA;
unlink "$sandbox_dir/tmp/$file";
};
if($@) {
$m->redirect(build_url($base . '/filevault.html', {file => $file, message => $message }));
}
</%init>
<%once>
use AlphaMail::HTMLHelper qw(build_url);
use Digest::MD5 qw(md5_hex);
</%once>
See more files for this project here