once.pl
changeset 59 ade26fe6ecbb
parent 58 adf016ea4348
child 60 e3d6e4095a2c
--- a/once.pl	Wed Dec 23 00:26:33 2015 +0100
+++ b/once.pl	Thu Dec 24 00:31:53 2015 +0100
@@ -34,12 +34,14 @@
 use CGI::Carp qw(fatalsToBrowser);
 use CGI::Pretty;
 use IO::File;
+use FindBin qw($RealBin);
 use File::Basename;
 use File::Path qw(remove_tree make_path);
 use File::Spec::Functions;
 use File::MimeInfo qw(mimetype);
 use Cwd qw(getcwd realpath);
 use Digest::MD5 qw(md5_hex md5);
+use Template;
 use experimental qw(smartmatch lexical_topic);
 
 sub humanize;     # convert numbers to human readable format
@@ -179,11 +181,8 @@
         exit 0;
     }
 
-    print header(-charset => "UTF-8"),
-      start_html(-title => "once"),
-      h1 "Ansicht: $view";
+    # save the uploaded file
 
-    # calculate the file name for the uploaded file
     if (length(my $file = param('upload'))) {
         my $uuid = gen_uuid();
         my ($delete, $expires, $days) = do {
@@ -224,22 +223,25 @@
         }
 
     }
-    print hr;
+
+    # create the view
+    print header(-charset => "UTF-8");
+
+    my %tt = (view => $view);
+    my $tt = Template->new(
+        {
+            INCLUDE_PATH => =>
+              [map { catfile($RealBin, $_) } 'var', 'templates.override', 'templates']
+        }
+    ) or die $Template::ERROR;
 
     # List the current content
     if (my @files = map { deslash $_ } glob "$user_dir/*-*/*") {
 
-        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.
-__
+        foreach (map { /(.*)/ } sort { -M $a <=> -M $b } @files) {
 
-        print start_table,
-          Tr(th { align => "left" }, [qw/Name Größe Hochgeladen Löschung/]);
+            my %file;
 
-        foreach (map { /(.*)/ } sort { -M $a <=> -M $b } @files) {
             my ($file, $dir) = fileparse($_);
             $dir = substr $dir,
               length $ONCE_VAR;    # make it relative to $ONCE_VAR
@@ -266,17 +268,19 @@
                 $d = 'nur manuell';
             }
 
-            print Tr(
-                td(a { href => "$ENV{PATH_INFO}/$dir$file" }, $file),
-                td({ align => "right" }, humanize((stat $_)[7])),
-                td(scalar localtime +(stat $_)[9]),
-                td($d),
-                td(a({ href => "?delete=$dir" }, 'remove'))
-            );
+            $file{name}          = $file;
+            $file{link}          = "$ENV{PATH_INFO}/$dir$file";
+            $file{size}          = humanize -s $_;
+            $file{uploaded}      = (stat _)[9];
+            $file{removal}{type} = $d;
+            $file{removal}{link} = "?delete=$dir";
+
+            push @{ $tt{files} }, \%file;
         }
 
-        print end_table, hr;
+        $tt->process('inventory.html', \%tt) or die $tt->error();
         return 0;
+
     }
 
     print start_multipart_form, start_table,