Add file time/speed; make URLs configurable in Wordpress
authorAlex Bligh <alex@alex.org.uk>
Sat, 1 Sep 2012 13:30:43 +0000 (14:30 +0100)
committerAlex Bligh <alex@alex.org.uk>
Sat, 1 Sep 2012 13:30:43 +0000 (14:30 +0100)
README
ambdownload.php
download.pl

diff --git a/README b/README
index f35f6d8..f1f67b3 100644 (file)
--- 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.
 
 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.
 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.
index c32d715..139bd64 100644 (file)
@@ -10,14 +10,17 @@ Author URI: http://blog.alex.org.uk
 \r
 class ambdownload\r
 {\r
 \r
 class ambdownload\r
 {\r
-       function getDownloadLink($user, $file="default")\r
+       function getDownloadLink($user, $link, $error, $file="default")\r
        {\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
                $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
        }\r
 \r
        function downloadRedirect()\r
@@ -25,11 +28,16 @@ class ambdownload
                global $post;\r
                if ((is_single() || is_singular() || is_page()))\r
                {\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
                        $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
                                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
                                exit;\r
                        }\r
                }\r
index af11039..859d5d8 100755 (executable)
@@ -7,12 +7,14 @@ use POSIX qw(strftime);
 use URI::Escape;
 use File::Copy qw( copy );
 use File::Basename;
 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 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";
 
 my $log;
 my $transaction="unknown";
@@ -140,6 +142,9 @@ else
 {
     openlog;
     my $file = decodeparams;
 {
     openlog;
     my $file = decodeparams;
+    my $sb = stat($file);
+    my $size = $sb->size;
+    my $t0 = [gettimeofday];
     lprintf("STARTING\n");
     $SIG{INT} = \&caughtsignal;
     $SIG{QUIT} = \&caughtsignal;
     lprintf("STARTING\n");
     $SIG{INT} = \&caughtsignal;
     $SIG{QUIT} = \&caughtsignal;
@@ -148,7 +153,8 @@ else
     $SIG{KILL} = \&caughtsignal;
     $SIG{TERM} = \&caughtsignal;
     sendfile($file);
     $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);
     closelog;
 
     exit(0);