From 841e8c4436be3a1be9e577d85937efc7af946c8f Mon Sep 17 00:00:00 2001
From: Luc Didry <luc@didry.org>
Date: Oct 03 2015 23:49:57 +0000
Subject: Use helper for max_delay


---

diff --git a/lib/Lufi.pm b/lib/Lufi.pm
index ae158bf..e64e469 100644
--- a/lib/Lufi.pm
+++ b/lib/Lufi.pm
@@ -115,10 +115,22 @@ sub startup {
     );
 
     $self->helper(
+        max_delay => sub {
+            my $c = shift;
+
+            return $c->config('max_delay') if ($c->config('max_delay') >= 0);
+
+            warn "max_delay set to a negative value. Default to 0.";
+            return 0;
+        }
+    );
+
+    $self->helper(
         is_selected => sub {
             my $c   = shift;
             my $num = shift;
 
+            return ($num == $c->max_delay)     ? 'selected="selected"' : '' if ($c->max_delay && !$c->default_delay);
             return ($num == $c->default_delay) ? 'selected="selected"' : '';
         }
     );
diff --git a/lib/Lufi/Controller/Files.pm b/lib/Lufi/Controller/Files.pm
index 52f8d09..a0562f6 100644
--- a/lib/Lufi/Controller/Files.pm
+++ b/lib/Lufi/Controller/Files.pm
@@ -54,7 +54,7 @@ sub upload {
                     }
                     # If the file size is lower than the lowest configured size or if there is no delay_for_size setting, we choose the configured max delay
                     unless (defined $delay) {
-                        $delay = ($json->{delay} <= $c->config('max_delay') || $c->config('max_delay') == 0) ? $json->{delay} : $c->config('max_delay');
+                        $delay = ($json->{delay} <= $c->max_delay || $c->max_delay == 0) ? $json->{delay} : $c->max_delay;
                     }
 
                     $f = Lufi::File->new(
diff --git a/templates/delays.html.ep b/templates/delays.html.ep
index bab31ed..0afefd7 100644
--- a/templates/delays.html.ep
+++ b/templates/delays.html.ep
@@ -9,7 +9,7 @@
         <p><%= l('This server sets limitations according to the file size. The expiration delay of your file will be the minimum between what you choose and the following limitations:') %></p>
         <ul>
         % my $delays   = config('delay_for_size');
-        % $delays->{0} = config('max_delay');
+        % $delays->{0} = max_delay;
         % my @keys     = sort keys %{$delays};
         % my $i        = 0;
         % for my $key (@keys) {
diff --git a/templates/index.html.ep b/templates/index.html.ep
index 1189487..1bc08cf 100644
--- a/templates/index.html.ep
+++ b/templates/index.html.ep
@@ -20,16 +20,16 @@
             <select id="delete-day" class="form-control">
 % for my $delay (qw/0 1 7 30 365/) {
 %   my $text = ($delay == 7 || $delay == 30) ? l('%1 days', $delay) : $d{'delay_'.$delay};
-%   if (config('max_delay')) {
+%   if (max_delay) {
 %       if ($delay) {
-%           if ($delay < config('max_delay')) {
+%           if ($delay < max_delay) {
                 <option value="<%= $delay %>" <%== is_selected($delay) %>><%= $text %></option>
-%           } elsif ($delay == config('max_delay')) {
+%           } elsif ($delay == max_delay) {
                 <option value="<%= $delay %>" <%== is_selected($delay) %>><%= $text %></option>
 %               last;
 %           } else {
 %               my $text = ($delay == 1) ? l('24 hours') : l('%1 days', $delay);
-                <option value="<%= config('max_delay') %>" <%== is_selected(config('max_delay')) %>><%= l('%1 days', config('max_delay')) %></option>
+                <option value="<%= max_delay %>" <%== is_selected(max_delay) %>><%= l('%1 days', max_delay) %></option>
 %               last;
 %           }
 %       }