From 6ae1c677b9b5d25a61cebb6a84ed8c5ba9caa0df Mon Sep 17 00:00:00 2001 From: Alex Bligh Date: Sat, 1 Sep 2012 14:30:43 +0100 Subject: [PATCH] Add file time/speed; make URLs configurable in Wordpress --- README | 6 ++++++ ambdownload.php | 18 +++++++++++++----- download.pl | 8 +++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README b/README index f35f6d8..f1f67b3 100644 --- a/README +++ b/README @@ -60,6 +60,12 @@ page. For instance, if you wished to make a page redirect to download 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. diff --git a/ambdownload.php b/ambdownload.php index c32d715..139bd64 100644 --- a/ambdownload.php +++ b/ambdownload.php @@ -10,14 +10,17 @@ Author URI: http://blog.alex.org.uk class ambdownload { - function getDownloadLink($user, $file="default") + function getDownloadLink($user, $link, $error, $file="default") { + if (empty($user) && !empty($error)) + { + return ($error); + } $time = time(); $secret = rtrim(file_get_contents("/etc/apache2/download.secret")); $id = $user; $hash = hash("sha256",$time.":".$id.":".$file.":".$secret); - $link = "http://server.example.com/download?"; - return $link.sprintf("id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash); + return $link.sprintf("?id=%s&file=%s&time=%s&hash=%s",urlencode($id),urlencode($file),$time,$hash); } function downloadRedirect() @@ -25,11 +28,16 @@ class ambdownload global $post; if ((is_single() || is_singular() || is_page())) { + /* Tag of file to download, e.g. 'default' */ $download_file = get_post_meta($post->ID, 'download_file', true); - if ($download_file) { + /* EG 'http://server.example.com/download' */ + $download_url = get_post_meta($post->ID, 'download_url', true); + /* EG URL where redirected if no username exists */ + $download_error = get_post_meta($post->ID, 'download_error', true); + if ($download_file && $download_url) { global $current_user; get_currentuserinfo(); - wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_file)); + wp_redirect(ambdownload::getDownloadLink($current_user->user_email, $download_url, $download_error, $download_file)); exit; } } diff --git a/download.pl b/download.pl index af11039..859d5d8 100755 --- a/download.pl +++ b/download.pl @@ -7,12 +7,14 @@ use POSIX qw(strftime); 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"; @@ -140,6 +142,9 @@ else { openlog; my $file = decodeparams; + my $sb = stat($file); + my $size = $sb->size; + my $t0 = [gettimeofday]; lprintf("STARTING\n"); $SIG{INT} = \&caughtsignal; $SIG{QUIT} = \&caughtsignal; @@ -148,7 +153,8 @@ else $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); -- 1.7.10.4