From 0c138700b21341b2fd79e53722605cb3e87fb9d2 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 25 Sep 2015 08:50:17 +0200 Subject: [PATCH] Bug 14893: Separate temporary storage per instance in Upload.pm MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit To make life easier when multiple instances are uploading temporary files to Koha, this patch adds the database name to the upload subfolder in your /tmp folder. Note: Although multiple instances could share the same subfolder for temporary storage (hashvalue is based on a timestamp too), it will be better to separate them for efficient housekeeping (removing older or partial files with a cronjob etc.) Since multiple instances come with separate permissions, keeping them in separate folders will be much simpler. Permanent storage is not affected by this patch. The location of permanent storage is ruled by the upload_path in each config file. Sharing that space is not recommended too. Although it may not be strictly necessary yet to remove files from the old temp storage folder (before the 3.22 release), the accompanying db rev performs that housekeeping task. Test plan: [1] Do not yet apply this patch. Upload a temporary file (use the tools/upload.pl script without selecting a category). [2] Check /tmp/koha_upload. [3] Apply this patch. Run the db rev with web installer. [4] Upload another temporary file. [5] Check /tmp for folder [your_database]_upload. [6] Check that /tmp/koha_upload is gone and the associated records too. [7] Run the adjusted t/db../Upload.t Followed test plan. Could not do steps before applying patch [1]-[2] (I suppose a local permission problem). After applying patch and updating db file appears in /tmp/[database] as expected. Signed-off-by: Marc Véron Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com --- Koha/Upload.pm | 9 +++++++-- .../data/mysql/atomicupdate/14893_cleanup.perl | 16 ++++++++++++++++ t/db_dependent/Upload.t | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/14893_cleanup.perl diff --git a/Koha/Upload.pm b/Koha/Upload.pm index 90f93e796f..9da26377d0 100644 --- a/Koha/Upload.pm +++ b/Koha/Upload.pm @@ -252,8 +252,13 @@ sub _init { $params->{tmp} = $params->{temp} if !exists $params->{tmp}; $self->{temporary} = $params->{tmp}? 1: 0; #default false - $self->{category} = $params->{tmp}? KOHA_UPLOAD: - ( $params->{category} || KOHA_UPLOAD ); + if( $params->{tmp} ) { + my $db = C4::Context->config('database'); + $self->{category} = KOHA_UPLOAD; + $self->{category} =~ s/koha/$db/; + } else { + $self->{category} = $params->{category} || KOHA_UPLOAD; + } $self->{files} = {}; $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv; diff --git a/installer/data/mysql/atomicupdate/14893_cleanup.perl b/installer/data/mysql/atomicupdate/14893_cleanup.perl new file mode 100644 index 0000000000..e0426df850 --- /dev/null +++ b/installer/data/mysql/atomicupdate/14893_cleanup.perl @@ -0,0 +1,16 @@ +# This perl snippet is run from within updatedatabase.pl +# Remove all temporary files from the obsolete koha_upload +# Bug 14893 replaces /tmp/koha_upload by /tmp/[db_name]_upload +# Permanent storage is not affected + +use File::Path qw[remove_tree]; # perl core module +use File::Spec; + +my $dbh= C4::Context->dbh; +$dbh->do(q| + DELETE FROM uploaded_files + WHERE COALESCE(permanent,0)=0 AND dir='koha_upload' +|); + +my $tmp= File::Spec->tmpdir.'/koha_upload'; +remove_tree( $tmp ) if -d $tmp; diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index c73675d7e1..1a273d56b6 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -125,7 +125,7 @@ sub test03 { my $cgi= $upl->cgi; is( $upl->count, 1, 'Upload 3 includes one temporary file' ); my $r = $upl->get({ id => $upl->result }); - is( $r->{uploadcategorycode}, 'koha_upload', 'Check category temp file' ); + is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' ); } sub test04 { # Fail on a file already there -- 2.39.5