diff options
author | Hrvoje Hodak <me@tribly.de> | 2018-01-26 15:09:29 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2018-01-31 17:05:35 +0100 |
commit | 1c015d938ed4f31ffa647bcbcaaa8fd1ecf72c9f (patch) | |
tree | 04d4616e0a3860d39bc8ed05d65eb83df0606208 | |
parent | 283b4a4a1d8246c180a5be75558aa88847a1d6c5 (diff) |
Proper display of expiration duration for less than a day
-rw-r--r-- | application/controllers/Main.php | 2 | ||||
-rw-r--r-- | application/helpers/filebin_helper.php | 26 | ||||
-rw-r--r-- | application/test/tests/test_filebin_helper.php | 45 | ||||
-rw-r--r-- | application/views/file/upload_form.php | 2 |
4 files changed, 73 insertions, 2 deletions
diff --git a/application/controllers/Main.php b/application/controllers/Main.php index 59213de1b..f48596b56 100644 --- a/application/controllers/Main.php +++ b/application/controllers/Main.php @@ -435,7 +435,7 @@ class Main extends MY_Controller { $this->data['title'] .= ' - Upload'; $this->data['small_upload_size'] = $this->config->item('small_upload_size'); $this->data['max_upload_size'] = $this->config->item('upload_max_size'); - $this->data['upload_max_age'] = $this->config->item('upload_max_age')/60/60/24; + $this->data['upload_max_age'] = $this->config->item('upload_max_age'); $this->data['username'] = $this->muser->get_username(); diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index ffecec98d..b89fe9bb3 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -1,5 +1,31 @@ <?php +function expiration_duration($duration) +{ + $total = $duration; + $days = floor($total / 86400); + $total -= $days * 86400; + $hours = floor($total / 3600); + $total -= $hours * 3600; + $minutes = floor($total / 60); + $seconds = $total - $minutes * 60; + $times = array($days, $hours, $minutes, $seconds); + $suffixes = array(' day', ' hour', ' minute', ' second'); + $expiration = array(); + + for ($i = 0; $i < count($suffixes); $i++) { + if ($times[$i] != 0) { + $duration = $times[$i].$suffixes[$i]; + if ($times[$i] > 1) { + $duration .= "s"; + } + array_push($expiration, $duration); + } + } + + return join(", ", $expiration); +} + function format_bytes($size) { $suffixes = array('B', 'KiB', 'MiB', 'GiB', 'TiB' , 'PiB' , 'EiB', 'ZiB', 'YiB'); diff --git a/application/test/tests/test_filebin_helper.php b/application/test/tests/test_filebin_helper.php index 2069f2953..fb5df9f84 100644 --- a/application/test/tests/test_filebin_helper.php +++ b/application/test/tests/test_filebin_helper.php @@ -24,6 +24,51 @@ class test_filebin_helper extends \test\Test { { } + public function test_expiration_duration() + { + $this->t->is(expiration_duration(60*60*24*2), "2 days", "2 days"); + $this->t->is(expiration_duration(60*60*24), "1 day", "1 day"); + $this->t->is(expiration_duration(60*60*2), "2 hours", "2 hours"); + $this->t->is(expiration_duration(60*60), "1 hour", "1 hour"); + $this->t->is(expiration_duration(60*2), "2 minutes", "2 minutes"); + $this->t->is(expiration_duration(60), "1 minute", "1 minute"); + $this->t->is(expiration_duration(59), "59 seconds", "59 seconds"); + $this->t->is(expiration_duration(1), "1 second", "1 second"); + + $this->t->is(expiration_duration(60*60*24 + 60*60 + 60), "1 day, 1 hour, 1 minute", "1 day, 1 hour, 1 minute"); + $this->t->is(expiration_duration(60*60*24 + 60*60 + 120), "1 day, 1 hour, 2 minutes", "1 day, 1 hour, 2 minutes"); + $this->t->is(expiration_duration(60*60*24 + 60*60*2 + 60), "1 day, 2 hours, 1 minute", "1 day, 2 hours, 1 minute"); + $this->t->is(expiration_duration(60*60*24 + 60*60*2 + 120), "1 day, 2 hours, 2 minutes", "1 day, 2 hours, 2 minutes"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60 + 60), "2 days, 1 hour, 1 minute", "2 days, 1 hour, 1 minute"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60 + 120), "2 days, 1 hour, 2 minutes", "2 days, 1 hour, 2 minutes"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60*2 + 60), "2 days, 2 hours, 1 minute", "2 days, 2 hours, 1 minute"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60*2 + 120), "2 days, 2 hours, 2 minutes", "2 days, 2 hours, 2 minutes"); + + $this->t->is(expiration_duration(60*60*24 + 60*60), "1 day, 1 hour", "1 day, 1 hour"); + $this->t->is(expiration_duration(60*60*24 + 60*60*2), "1 day, 2 hours", "1 day, 2 hours"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60), "2 days, 1 hour", "2 days, 1 hour"); + $this->t->is(expiration_duration(60*60*24*2 + 60*60*2), "2 days, 2 hours", "2 days, 2 hours"); + + $this->t->is(expiration_duration(60*60*24 + 60), "1 day, 1 minute", "1 day, 1 minute"); + $this->t->is(expiration_duration(60*60*24 + 120), "1 day, 2 minutes", "1 day, 2 minutes"); + $this->t->is(expiration_duration(60*60*24*2 + 60), "2 days, 1 minute", "2 days, 1 minute"); + $this->t->is(expiration_duration(60*60*2*24 + 120), "2 days, 2 minutes", "2 days, 2 minutes"); + + $this->t->is(expiration_duration(60*60 + 60), "1 hour, 1 minute", "1 hour, 1 minute"); + $this->t->is(expiration_duration(60*60 + 120), "1 hour, 2 minutes", "1 hour, 2 minutes"); + $this->t->is(expiration_duration(60*60*2 + 60), "2 hours, 1 minute", "2 hours, 1 minute"); + $this->t->is(expiration_duration(60*60*2 + 120), "2 hours, 2 minutes", "2 hours, 2 minutes"); + + $this->t->is(expiration_duration(61), "1 minute, 1 second", "1 minute, 1 second"); + $this->t->is(expiration_duration(62), "1 minute, 2 seconds", "1 minute, 2 seconds"); + $this->t->is(expiration_duration(121), "2 minutes, 1 second", "2 minutes, 1 second"); + $this->t->is(expiration_duration(122), "2 minutes, 2 seconds", "2 minutes, 2 seconds"); + + $this->t->is(expiration_duration(60*60*24 + 60*60*23 + 60*59), "1 day, 23 hours, 59 minutes", "1 day, 23 hours, 59 minutes"); + $this->t->is(expiration_duration(60*60*23 + 60*59), "23 hours, 59 minutes", "23 hours, 59 minutes"); + $this->t->is(expiration_duration(60*60*2 + 60*59), "2 hours, 59 minutes", "2 hours, 59 minutes"); + } + public function test_format_bytes() { $this->t->is(format_bytes(500), "500B", "500B"); diff --git a/application/views/file/upload_form.php b/application/views/file/upload_form.php index 66e855606..e002b25da 100644 --- a/application/views/file/upload_form.php +++ b/application/views/file/upload_form.php @@ -53,7 +53,7 @@ <p><button type="submit" id="upload_button" class="btn btn-primary">Upload/Paste it!</button></p> <p> Uploads/pastes are <?php if ($upload_max_age > 0) { - echo "deleted after ".$upload_max_age." days"; + echo "deleted after ".expiration_duration($upload_max_age); if ($small_upload_size > 0) { echo " unless they are smaller than ".format_bytes($small_upload_size); } |