3 # Copyright 2007 LibLime, Galen Charlton
4 # Copyright 2011-2012 BibLibre
5 # Copyright 2015 Rijksmuseum
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 Koha::Upload - Facilitate file uploads (temporary and permanent)
30 # add an upload (see tools/upload-file.pl)
31 # the public flag allows retrieval via OPAC
32 my $upload = Koha::Upload->new( public => 1, category => 'A' );
33 my $cgi = $upload->cgi;
34 # Do something with $upload->count, $upload->result or $upload->err
36 # get some upload records (in staff)
37 # Note: use the public flag for OPAC
38 my @uploads = Koha::Upload->new->get( term => $term );
39 $template->param( uploads => \@uploads );
42 my $rec = Koha::Upload->new->get({ id => $id, filehandle => 1 });
44 my @hdr = Koha::Upload->httpheaders( $rec->{name} );
45 print Encode::encode_utf8( $input->header( @hdr ) );
46 while( <$fh> ) { print $_; }
50 my ( $fn ) = Koha::Upload->new->delete({ id => $id });
54 This module is a refactored version of C4::UploadedFile but adds on top
55 of that the new functions from report 6874 (Upload plugin in editor).
56 That report added module UploadedFiles.pm. This module contains the
57 functionality of both.
63 use constant KOHA_UPLOAD
=> 'koha_upload';
64 use constant BYTES_DIGEST
=> 2048;
67 use CGI
; # no utf8 flag, since it may interfere with binary uploads
74 use base
qw(Class::Accessor);
79 __PACKAGE__
->mk_ro_accessors( qw
|| );
83 Returns new object based on Class::Accessor.
84 Use tmp or temp flag for temporary storage.
85 Use public flag to mark uploads as available in OPAC.
86 The category parameter is only useful for permanent storage.
91 my ( $class, $params ) = @_;
92 my $self = $class->SUPER::new
();
93 $self->_init( $params );
99 Returns CGI object. The CGI hook is used to store the uploaded files.
106 # Next call handles the actual upload via CGI hook.
107 # The third parameter (0) below means: no CGI temporary storage.
108 # Cancelling an upload will make CGI abort the script; no problem,
109 # the file(s) without db entry will be removed later.
110 my $query = CGI
::->new( sub { $self->_hook(@_); }, {}, 0 );
119 Returns number of uploaded files without errors
125 return scalar grep { !exists $self->{files
}->{$_}->{errcode
} } keys %{ $self->{files
} };
130 Returns a string of id's for each successful upload separated by commas.
136 my @a = map { $self->{files
}->{$_}->{id
} }
137 grep { !exists $self->{files
}->{$_}->{errcode
} }
138 keys %{ $self->{files
} };
139 return @a?
( join ',', @a ): undef;
144 Returns hash with errors in format { file => err, ... }
145 Undefined if there are no errors.
152 foreach my $f ( keys %{ $self->{files
} } ) {
153 my $e = $self->{files
}->{$f}->{errcode
};
154 $err->{ $f } = $e if $e;
161 Returns arrayref of uploaded records (hash) or one uploaded record.
162 You can pass id => $id or hashvalue => $hash or term => $term.
163 Optional parameter filehandle => 1 returns you a filehandle too.
168 my ( $self, $params ) = @_;
169 my $temp= $self->_lookup( $params );
171 foreach my $r ( @
$temp ) {
173 foreach( qw
[id hashvalue filesize uploadcategorycode public permanent
] ) {
174 $res->{$_} = $r->{$_};
176 $res->{name
} = $r->{filename
};
177 $res->{path
} = $self->_full_fname($r);
178 if( $res->{path
} && -r
$res->{path
} ) {
179 if( $params->{filehandle
} ) {
180 my $fh = IO
::File
->new( $res->{path
}, "r" );
186 $self->{files
}->{ $r->{filename
} }->{errcode
}=5; #not readable
190 return wantarray?
@rv: $res;
195 Returns array of deleted filenames or undef.
196 Since it now only accepts id as parameter, you should not expect more
202 my ( $self, $params ) = @_;
203 return if !$params->{id
};
205 my $temp = $self->_lookup({ id
=> $params->{id
} });
207 my $d = $self->_delete( $_ );
218 getCategories returns a list of upload category codes and names
224 my $cats = C4
::Koha
::GetAuthorisedValues
('UPLOAD');
225 [ map {{ code
=> $_->{authorised_value
}, name
=> $_->{lib
} }} @
$cats ];
230 httpheaders returns http headers for a retrievable upload
231 Will be extended by report 14282
236 my ( $class, $name ) = @_;
238 '-type' => 'application/octet-stream',
239 '-attachment' => $name,
243 =head1 INTERNAL ROUTINES
248 my ( $self, $params ) = @_;
250 $self->{rootdir
} = C4
::Context
->config('upload_path');
251 $self->{tmpdir
} = File
::Spec
->tmpdir;
253 $params->{tmp
} = $params->{temp
} if !exists $params->{tmp
};
254 $self->{temporary
} = $params->{tmp
}?
1: 0; #default false
255 $self->{category
} = $params->{tmp
}? KOHA_UPLOAD
:
256 ( $params->{category
} || KOHA_UPLOAD
);
259 $self->{uid
} = C4
::Context
->userenv->{number
} if C4
::Context
->userenv;
260 $self->{public
} = $params->{public
}?
1: undef;
264 my ( $self, $filename ) = @_;
265 if( $self->{files
}->{$filename} ) {
266 return $self->{files
}->{$filename}->{fh
};
271 my ( $self, $filename ) = @_;
273 if( $self->{files
}->{$filename} &&
274 $self->{files
}->{$filename}->{errcode
} ) {
276 } elsif( !$self->{temporary
} && !$self->{rootdir
} ) {
277 $self->{files
}->{$filename}->{errcode
} = 3; #no rootdir
278 } elsif( $self->{temporary
} && !$self->{tmpdir
} ) {
279 $self->{files
}->{$filename}->{errcode
} = 4; #no tempdir
281 my $dir = $self->_dir;
282 my $fn = $self->{files
}->{$filename}->{hash
}. '_'. $filename;
283 if( -e
"$dir/$fn" && @
{ $self->_lookup({
284 hashvalue
=> $self->{files
}->{$filename}->{hash
} }) } ) {
285 # if the file exists and it is registered, then set error
286 $self->{files
}->{$filename}->{errcode
} = 1; #already exists
289 $fh = IO
::File
->new( "$dir/$fn", "w");
292 $self->{files
}->{$filename}->{fh
}= $fh;
294 $self->{files
}->{$filename}->{errcode
} = 2; #not writable
302 my $dir = $self->{temporary
}?
$self->{tmpdir
}: $self->{rootdir
};
303 $dir.= '/'. $self->{category
};
304 mkdir $dir if !-d
$dir;
309 my ( $self, $rec ) = @_;
312 $p = File
::Spec
->catfile(
313 $rec->{permanent
}?
$self->{rootdir
}: $self->{tmpdir
},
315 $rec->{hashvalue
}. '_'. $rec->{filename
}
322 my ( $self, $filename, $buffer, $bytes_read, $data ) = @_;
323 $filename= Encode
::decode_utf8
( $filename ); # UTF8 chars in filename
324 $self->_compute( $filename, $buffer );
325 my $fh = $self->_fh( $filename ) // $self->_create_file( $filename );
326 print $fh $buffer if $fh;
332 foreach my $f ( keys %{ $self->{files
} } ) {
333 my $fh = $self->_fh($f);
334 $self->_register( $f, $fh?
tell( $fh ): undef )
335 if !$self->{files
}->{$f}->{errcode
};
341 my ( $self, $filename, $size ) = @_;
342 my $dbh= C4
::Context
->dbh;
343 my $sql= 'INSERT INTO uploaded_files (hashvalue, filename, dir, filesize,
344 owner, uploadcategorycode, public, permanent) VALUES (?,?,?,?,?,?,?,?)';
346 $self->{files
}->{$filename}->{hash
},
353 $self->{temporary
}?
0: 1,
355 $dbh->do( $sql, undef, @pars );
356 my $i = $dbh->last_insert_id(undef, undef, 'uploaded_files', undef);
357 $self->{files
}->{$filename}->{id
} = $i if $i;
361 my ( $self, $params ) = @_;
362 my $dbh = C4
::Context
->dbh;
364 SELECT id
,hashvalue
,filename
,dir
,filesize
,uploadcategorycode
,public
,permanent
368 if( $params->{id
} ) {
369 return [] if $params->{id
} !~ /^\d+(,\d+)*$/;
370 $sql.= 'WHERE id IN ('.$params->{id
}.')';
372 } elsif( $params->{hashvalue
} ) {
373 $sql.= 'WHERE hashvalue=?';
374 @pars = ( $params->{hashvalue
} );
375 } elsif( $params->{term
} ) {
376 $sql.= 'WHERE (filename LIKE ? OR hashvalue LIKE ?)';
377 @pars = ( '%'.$params->{term
}.'%', '%'.$params->{term
}.'%' );
381 $sql.= $self->{public
}?
' AND public=1': '';
382 $sql.= ' ORDER BY id';
383 my $temp= $dbh->selectall_arrayref( $sql, { Slice
=> {} }, @pars );
388 my ( $self, $rec ) = @_;
389 my $dbh = C4
::Context
->dbh;
390 my $sql = 'DELETE FROM uploaded_files WHERE id=?';
391 my $file = $self->_full_fname($rec);
392 if( !-e
$file ) { # we will just delete the record
393 # TODO Should we add a trace here for the missing file?
394 $dbh->do( $sql, undef, ( $rec->{id
} ) );
395 return $rec->{filename
};
396 } elsif( unlink($file) ) {
397 $dbh->do( $sql, undef, ( $rec->{id
} ) );
398 return $rec->{filename
};
400 $self->{files
}->{ $rec->{filename
} }->{errcode
} = 7;
401 #NOTE: errcode=6 is used to report successful delete (see template)
406 # Computes hash value when sub hook feeds the first block
407 # For temporary files, the id is made unique with time
408 my ( $self, $name, $block ) = @_;
409 if( !$self->{files
}->{$name}->{hash
} ) {
410 my $str = $name. ( $self->{uid
} // '0' ).
411 ( $self->{temporary
}? Time
::HiRes
::time(): '' ).
412 $self->{category
}. substr( $block, 0, BYTES_DIGEST
);
413 # since Digest cannot handle wide chars, we need to encode here
414 # there could be a wide char in the filename or the category
415 my $h = Digest
::MD5
::md5_hex
( Encode
::encode_utf8
( $str ) );
416 $self->{files
}->{$name}->{hash
} = $h;
422 Koha Development Team
423 Larger parts from Galen Charlton, Julian Maurice and Marcel de Rooy