From d05827102e914b20a4c3e694fa641a99a37b5f93 Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Thu, 1 May 2014 20:20:28 +0200 Subject: [PATCH] Fix #27 --- README.md | 2 ++ lib/Mojolicious/Command/cron/cleanfiles.pm | 12 ++++++++++++ lutim.conf.template | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 7a27881..b6b1d18 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ The `lutim.conf.template` is self-documented but here is the options that you ca * default\_delay: what is the default time limit for files? Valid values are 0, 1, 7, 30 and 365; * max\_delay: if defined, the images will be deleted after that delay (in days), even if they were uploaded with "no delay" (or value superior to max\_delay) option and a warning message will be displayed on homepage; * always\_encrypt: if set to 1, all images will be encrypted. +* delete\_no\_longer\_viewed\_files: if set, the images which have not been viewed since `delete\_no\_longer\_viewed\_files` days will be deleted by the `script/lutim cron cleanfiles` command ## Usage @@ -147,6 +148,7 @@ carton exec script/lutim cron cleanbdd ### Delete expired files To automatically delete files which availability delay is over (when you choose that your image will be deleted after 24h / one week / etc.) +If `delete\_no\_longer\_viewed\_files`, the files not viewed since `delete\_no\_longer\_viewed\_files` days will be deleted too. ```shell carton exec script/lutim cron cleanfiles ``` diff --git a/lib/Mojolicious/Command/cron/cleanfiles.pm b/lib/Mojolicious/Command/cron/cleanfiles.pm index 5e092d8..1190cf1 100644 --- a/lib/Mojolicious/Command/cron/cleanfiles.pm +++ b/lib/Mojolicious/Command/cron/cleanfiles.pm @@ -16,6 +16,18 @@ sub run { $image->update(enabled => 0); unlink $image->path(); } + + my $config = $c->app->plugin('Config'); + + if (defined($config->{delete_no_longer_viewed_files}) && $config->{delete_no_longer_viewed_files} > 0) { + $time = time() - $config->{delete_no_longer_viewed_files} * 86400; + @images = LutimModel::Lutim->select('WHERE enabled = 1 AND last_access_at < ?', $time); + + for my $image (@images) { + $image->update(enabled => 0); + unlink $image->path(); + } + } } =encoding utf8 diff --git a/lutim.conf.template b/lutim.conf.template index 6d6ad28..0a9bbf5 100644 --- a/lutim.conf.template +++ b/lutim.conf.template @@ -119,4 +119,9 @@ # please, see readme # optional, default is 'warn' #policy_when_full => 'warn', + + # images which are not viewed since delete_no_longer_viewed_files days will be deleted by the cron cleanfiles task + # if delete_no_longer_viewed_files is not set, the no longer viewed files will NOT be deleted + # optional, no default + #delete_no_longer_viewed_files => 90 };