to the value 'myfile'. You will need to enable this module once you have
installed it.
+You should set the following meta keys:
+ download_file - the tag of the file (see above)
+ download_url - the base URL of the perl script, e.g
+ http://server.example.com/download/download.pl
+ download_error - if set, redirects to this page if a user is not set
+
Note, to avoid having to muck around with Wordpress, myfile could be a symlink,
and the script will correctly name the downloaded file as per the target of the
symlink.
\r
class ambdownload\r
{\r
- function getDownloadLink($user, $file="default")\r
+ function getDownloadLink($user, $link, $error, $file="default")\r
{\r
+ if (empty($user) && !empty($error))\r
+ {\r
+ return ($error);\r
+ }\r
$time = time();\r
$secret = rtrim(file_get_contents("/etc/apache2/download.secret"));\r
$id = $user;\r
$hash = hash("sha256",$time.":".$id.":".$file.":".$secret);\r
- $link = "http://server.example.com/download?";\r
- return $link.sprintf("id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
+ return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash);\r
}\r
\r
function downloadRedirect()\r
global $post;\r
if ((is_single() || is_singular() || is_page()))\r
{\r
+ /* Tag of file to download, e.g. 'default' */\r
$download_file = get_post_meta($post->ID, 'download_file', true); \r
- if ($download_file) { \r
+ /* EG 'http://server.example.com/download' */\r
+ $download_url = get_post_meta($post->ID, 'download_url', true); \r
+ /* EG URL where redirected if no username exists */\r
+ $download_error = get_post_meta($post->ID, 'download_error', true); \r
+ if ($download_file && $download_url) { \r
global $current_user;\r
get_currentuserinfo();\r
- wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_file));\r
+ wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file));\r
exit;\r
}\r
}\r
use URI::Escape;
use File::Copy qw( copy );
use File::Basename;
+use File::stat;
use Digest::SHA qw(sha256_hex sha1);
use MIME::Base64;
use File::Spec;
use CGI;
use HTML::Entities;
use IO::Handle;
+use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
my $log;
my $transaction="unknown";
{
openlog;
my $file = decodeparams;
+ my $sb = stat($file);
+ my $size = $sb->size;
+ my $t0 = [gettimeofday];
lprintf("STARTING\n");
$SIG{INT} = \&caughtsignal;
$SIG{QUIT} = \&caughtsignal;
$SIG{KILL} = \&caughtsignal;
$SIG{TERM} = \&caughtsignal;
sendfile($file);
- lprintf("SUCCESS\n");
+ my $elapsed = tv_interval ( $t0, [gettimeofday]);
+ lprintf("SUCCESS %d bytes %.3f MB/s\n", $size, $size/(1000000.0*(($elapsed<0.001)?0.001:$elapsed)));
closelog;
exit(0);