Bug 32426: Throw InvalidUserid exception in Patron->store
[koha-ffzg.git] / Koha / Uploader.pm
index 7a02d11..8fbb06a 100644 (file)
@@ -6,18 +6,18 @@ package Koha::Uploader;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -36,8 +36,8 @@ Koha::Uploader - Facilitate file uploads (temporary and permanent)
     # Do something with $upload->count, $upload->result or $upload->err
 
     # get some upload records (in staff) via Koha::UploadedFiles
-    my @uploads1 = Koha::UploadedFiles->search({ filename => $name });
-    my @uploads2 = Koha::UploadedFiles->search_term({ term => $term });
+    my $uploads1 = Koha::UploadedFiles->search({ filename => $name });
+    my $uploads2 = Koha::UploadedFiles->search_term({ term => $term });
 
     # staff download (via Koha::UploadedFile[s])
     my $rec = Koha::UploadedFiles->find( $id );
@@ -58,14 +58,17 @@ Koha::Uploader - Facilitate file uploads (temporary and permanent)
 
 =cut
 
-use constant KOHA_UPLOAD => 'koha_upload';
+use constant KOHA_UPLOAD  => 'koha_upload';
 use constant BYTES_DIGEST => 2048;
+use constant ERR_EXISTS   => 'UPLERR_ALREADY_EXISTS';
+use constant ERR_PERMS    => 'UPLERR_CANNOT_WRITE';
+use constant ERR_ROOT     => 'UPLERR_NO_ROOT_DIR';
+use constant ERR_TEMP     => 'UPLERR_NO_TEMP_DIR';
 
 use Modern::Perl;
 use CGI; # no utf8 flag, since it may interfere with binary uploads
 use Digest::MD5;
 use Encode;
-use File::Spec;
 use IO::File;
 use Time::HiRes;
 
@@ -143,7 +146,7 @@ sub result {
 
 =head2 err
 
-    Returns hash with errors in format { file => err, ... }
+    Returns hashref with errors in format { file => { code => err }, ... }
     Undefined if there are no errors.
 
 =cut
@@ -189,7 +192,7 @@ sub _init {
     my ( $self, $params ) = @_;
 
     $self->{rootdir} = Koha::UploadedFile->permanent_directory;
-    $self->{tmpdir} = Koha::UploadedFile->temporary_directory;
+    $self->{tmpdir} = C4::Context::temporary_directory;
 
     $params->{tmp} = $params->{temp} if !exists $params->{tmp};
     $self->{temporary} = $params->{tmp}? 1: 0; #default false
@@ -220,9 +223,9 @@ sub _create_file {
             $self->{files}->{$filename}->{errcode} ) {
         #skip
     } elsif( !$self->{temporary} && !$self->{rootdir} ) {
-        $self->{files}->{$filename}->{errcode} = 3; #no rootdir
+        $self->{files}->{$filename}->{errcode} = ERR_ROOT; #no rootdir
     } elsif( $self->{temporary} && !$self->{tmpdir} ) {
-        $self->{files}->{$filename}->{errcode} = 4; #no tempdir
+        $self->{files}->{$filename}->{errcode} = ERR_TEMP; #no tempdir
     } else {
         my $dir = $self->_dir;
         my $hashval = $self->{files}->{$filename}->{hash};
@@ -235,7 +238,7 @@ sub _create_file {
             hashvalue          => $hashval,
             uploadcategorycode => $self->{category},
         })->count ) {
-            $self->{files}->{$filename}->{errcode} = 1; #already exists
+            $self->{files}->{$filename}->{errcode} = ERR_EXISTS;
             return;
         }
 
@@ -244,7 +247,7 @@ sub _create_file {
             $fh->binmode;
             $self->{files}->{$filename}->{fh}= $fh;
         } else {
-            $self->{files}->{$filename}->{errcode} = 2; #not writable
+            $self->{files}->{$filename}->{errcode} = ERR_PERMS;
         }
     }
     return $fh;