Bug 14893: Separate temporary storage per instance in Upload.pm
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 25 Sep 2015 06:50:17 +0000 (08:50 +0200)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Fri, 29 Jan 2016 01:17:55 +0000 (01:17 +0000)
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 <veron@veron.ch>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
Koha/Upload.pm
installer/data/mysql/atomicupdate/14893_cleanup.perl [new file with mode: 0644]
t/db_dependent/Upload.t

index 90f93e7..9da2637 100644 (file)
@@ -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 (file)
index 0000000..e0426df
--- /dev/null
@@ -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;
index c73675d..1a273d5 100644 (file)
@@ -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