From 1c015d938ed4f31ffa647bcbcaaa8fd1ecf72c9f Mon Sep 17 00:00:00 2001 From: Hrvoje Hodak Date: Fri, 26 Jan 2018 15:09:29 +0100 Subject: Proper display of expiration duration for less than a day --- application/controllers/Main.php | 2 +- application/helpers/filebin_helper.php | 26 +++++++++++++++ application/test/tests/test_filebin_helper.php | 45 ++++++++++++++++++++++++++ application/views/file/upload_form.php | 2 +- 4 files changed, 73 insertions(+), 2 deletions(-) (limited to 'application') 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 @@ 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 @@

Uploads/pastes are 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); } -- cgit v1.2.3-24-g4f1b