file size human readable, redirect after removal of files
authorheiko
Wed, 11 May 2011 10:36:49 +0200
changeset 10 1e7562ad30da
parent 9 d2c21ec59758
child 11 189e63c5b36f
file size human readable, redirect after removal of files
upload.pl
--- a/upload.pl	Wed May 11 09:52:40 2011 +0200
+++ b/upload.pl	Wed May 11 10:36:49 2011 +0200
@@ -20,6 +20,7 @@
 #   Alias	/d	    /home/ud/XXX/d/
 # gesetzt werden.
 
+use 5.010;
 use strict;
 use warnings;
 use CGI qw(:all *table);
@@ -28,21 +29,32 @@
 use IO::File;
 use File::Basename;
 use Digest::SHA1 qw(sha1_hex);
+use Digest::MD5 qw(md5_hex);
 
-my $DIR      = "d";
+my $DIR      = "d/{view}";
 my $DIR_URI = "/$DIR";
 
+sub human($);
+
 delete @ENV{grep /PATH/, keys %ENV};
 $ENV{PATH} = "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin";
 
--d $DIR
-  or mkdir $DIR => 0750
-  or die "Can't mkdir $DIR: $!\n";
+$_ = dirname $DIR;
+-d or mkdir $_ => 0750
+   or die "Can't mkdir $_: $!\n";
 
 MAIN: {
-    print header(-charset => "UTF-8"), start_html, h1 "View: $ENV{REMOTE_USER}";
+
+    # per view we have an own directory
 
-    # print Dump;
+    $ENV{REMOTE_USER} =~ /(.*)/;
+    $_ = md5_hex($1);
+    $DIR =~ s/{view}/$_/g;
+    $DIR_URI =~ s/{view}/$_/g;
+    -d $DIR
+	or mkdir $DIR => 0750
+	or die "Can't mkdir $DIR: $!\n";
+    
 
     if (param("delete") =~ /([a-z\d]+-\d+)/i) {
         my $dir = $1;
@@ -51,8 +63,18 @@
               or die "Can't unlink $DIR/$dir/*: $!\n";
             rmdir "$DIR/$dir" or die "Can't rmdir $DIR/$dir: $!\n";
         }
+	print redirect(-uri => url(-path_info => 1));
+	exit 0;
     }
 
+    print header(-charset => "UTF-8"), 
+	start_html(-title => "Up&Down"), 
+	h1 "Ansicht: $ENV{REMOTE_USER}";
+
+
+    # print Dump;
+
+
     if (length(my $file = param("upload"))) {
 	my $days = param("expires");
         my $expires;
@@ -94,9 +116,11 @@
 			print p <<__;
 			Der gültige Download-Link ist die Link-Adresse, die sich hinter
 			dem Datei-Namen verbirgt. (Firefox: Rechte Maustaste, Link-Location).
+			Nach Ablauf des <a href="http://de.wikipedia.org/wiki/Mindesthaltbarkeitsdatum">MHD</a>
+			wird die Datei automatisch gelöscht.
 __
 
-			print start_table, Tr(th { align => "left" }, [qw/name size date expires/]);
+			print start_table, Tr(th { align => "left" }, [qw/Name Größe Hochgeladen Löschung/]);
 
 			foreach (map { /(.*)/ } sort { -M $a <=> -M $b } glob "$DIR/*-*/*") {
 				my ($file, $dir) = fileparse($_);
@@ -115,7 +139,7 @@
 
 				print Tr(
 					td(a { href => "$DIR_URI/$dir/$file" }, $file),
-					td({ align => "right" }, (stat $_)[7]),
+					td({ align => "right" }, human((stat $_)[7])),
 					td(scalar localtime +(stat $_)[9]),
 					td(scalar localtime ${expires}),
 					td(a({ href => "?delete=$dir" }, "remove"))
@@ -135,5 +159,22 @@
       end_table,
       end_multipart_form;
 
-    print end_html;
+    print hr,
+	div({-align => "right"}, 
+		a({-href => "https://keller.schlittermann.de/hg/anon-upload/"} => "Scripting"),
+		" &copy; 2010,2011 ", 
+		a({-href => "http://www.schlittermann.de/"} => "Heiko Schlittermann")),
+	end_html;
 }
+
+sub human($) {
+	my $_ = shift;
+	my @units = qw(B K M G T);
+	while (length int > 3 and @units) {
+		$_ = sprintf "%.1f" => $_/1024;
+		shift @units;
+	}
+	croak "filesize is too big (can't convert to human readable number"
+		if !@units;
+	return "$_$units[0]";
+}