|
Line 0
Link Here
|
|
|
1 |
package Koha::Upload; |
| 2 |
|
| 3 |
# Copyright 2015 Rijksmuseum |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
Koha::Upload - Facilitate file upload |
| 23 |
|
| 24 |
=head1 SYNOPSIS |
| 25 |
|
| 26 |
use Koha::Upload; |
| 27 |
|
| 28 |
=head1 DESCRIPTION |
| 29 |
|
| 30 |
This class |
| 31 |
|
| 32 |
=head1 METHODS |
| 33 |
|
| 34 |
=head2 new |
| 35 |
|
| 36 |
Create object (via Class::Accessor). |
| 37 |
|
| 38 |
=head1 PROPERTIES |
| 39 |
|
| 40 |
=head2 ??? |
| 41 |
|
| 42 |
??? |
| 43 |
|
| 44 |
=head1 ADDITIONAL COMMENTS |
| 45 |
|
| 46 |
=cut |
| 47 |
|
| 48 |
use constant KOHA_UPLOAD => 'koha_upload'; |
| 49 |
use constant BYTES_DIGEST => 2048; |
| 50 |
|
| 51 |
use Modern::Perl; |
| 52 |
use CGI; # no utf8 flag, since it may interfere with binary uploads |
| 53 |
use Digest::MD5; |
| 54 |
use File::Spec; |
| 55 |
use IO::File; |
| 56 |
use Time::HiRes; |
| 57 |
|
| 58 |
use base qw(Class::Accessor); |
| 59 |
|
| 60 |
use C4::Context; |
| 61 |
|
| 62 |
__PACKAGE__->mk_ro_accessors( qw| name |
| 63 |
|); |
| 64 |
|
| 65 |
=head2 new |
| 66 |
|
| 67 |
Returns new object based on Class::Accessor. |
| 68 |
|
| 69 |
=cut |
| 70 |
|
| 71 |
sub new { |
| 72 |
my ( $class, $params ) = @_; |
| 73 |
my $self = $class->SUPER::new(); |
| 74 |
$self->_init( $params ); |
| 75 |
return $self; |
| 76 |
} |
| 77 |
|
| 78 |
=head2 cgi |
| 79 |
|
| 80 |
Returns new object based on Class::Accessor. |
| 81 |
|
| 82 |
=cut |
| 83 |
|
| 84 |
sub cgi { |
| 85 |
my ( $self ) = @_; |
| 86 |
|
| 87 |
# Next call handles the actual upload via CGI hook. |
| 88 |
# The third parameter (0) below means: no CGI temporary storage. |
| 89 |
# Cancelling an upload will make CGI abort the script; no problem, |
| 90 |
# the file(s) without db entry will be removed later. |
| 91 |
my $query = CGI::->new( sub { $self->_hook(@_); }, {}, 0 ); |
| 92 |
if( $query ) { |
| 93 |
$self->_done; |
| 94 |
return $query; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
=head2 result |
| 99 |
|
| 100 |
Returns new object based on Class::Accessor. |
| 101 |
|
| 102 |
=cut |
| 103 |
|
| 104 |
sub result { |
| 105 |
my ( $self ) = @_; |
| 106 |
my @a = map { $self->{files}->{$_}->{id} } keys $self->{files}; |
| 107 |
return join ',', @a; |
| 108 |
} |
| 109 |
|
| 110 |
=head2 get |
| 111 |
|
| 112 |
Returns array |
| 113 |
optional parameter: filehandle => 1 (name, path only by default) |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
sub get { |
| 118 |
my ( $self, $params ) = @_; |
| 119 |
my $temp= $self->_lookup( $params ); |
| 120 |
my ( @rv, $res ); |
| 121 |
foreach my $r ( @$temp ) { |
| 122 |
$res->{name} = $r->{filename}; |
| 123 |
$res->{path}= $self->_full_fname($r); |
| 124 |
$res->{fh} = IO::File->new( $res->{path}, "r" ) |
| 125 |
if $params->{filehandle}; |
| 126 |
push @rv, $res; |
| 127 |
last if !wantarray; |
| 128 |
} |
| 129 |
return wantarray? @rv: $res; |
| 130 |
} |
| 131 |
|
| 132 |
sub DESTROY { |
| 133 |
} |
| 134 |
|
| 135 |
# ************** INTERNAL ROUTINES ******************************************** |
| 136 |
|
| 137 |
sub _init { |
| 138 |
my ( $self, $params ) = @_; |
| 139 |
|
| 140 |
$self->{rootdir} = C4::Context->config('upload_path'); |
| 141 |
$self->{tmpdir} = File::Spec->tmpdir; |
| 142 |
if( exists $params->{tmp} || exists $params->{temp} || |
| 143 |
!$params->{category} ) { |
| 144 |
$self->{temporary} = 1; |
| 145 |
$self->{category} = undef; |
| 146 |
} else { |
| 147 |
$self->{category} = $params->{category}; |
| 148 |
} |
| 149 |
|
| 150 |
$self->{errors} = []; |
| 151 |
$self->{files} = {}; |
| 152 |
$self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv; |
| 153 |
} |
| 154 |
|
| 155 |
sub _fh { |
| 156 |
my ( $self, $filename ) = @_; |
| 157 |
if( $self->{files}->{$filename} ) { |
| 158 |
return $self->{files}->{$filename}->{fh}; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
sub _create_file { |
| 163 |
my ( $self, $filename ) = @_; |
| 164 |
my $fh; |
| 165 |
if( $self->{files}->{$filename} && |
| 166 |
$self->{files}->{$filename}->{errcode} ) { |
| 167 |
#skip |
| 168 |
} elsif( $self->{temporary} && !$self->{tmpdir} ) { |
| 169 |
$self->{files}->{$filename}->{errcode} = 2; |
| 170 |
} else { |
| 171 |
my $dir = $self->_dir; |
| 172 |
my $fn = $self->{files}->{$filename}->{hash}. '_'. $filename; |
| 173 |
$fh = IO::File->new( "$dir/$fn", "w"); |
| 174 |
if( $fh ) { |
| 175 |
$fh->binmode; |
| 176 |
$self->{files}->{$filename}->{fh}= $fh; |
| 177 |
} else { |
| 178 |
$self->{files}->{$filename}->{errcode} = 1; |
| 179 |
# push @{$self->{errors}}, [ 1, $filename ]; |
| 180 |
} |
| 181 |
} |
| 182 |
return $fh; |
| 183 |
} |
| 184 |
|
| 185 |
sub _dir { |
| 186 |
my ( $self ) = @_; |
| 187 |
my $dir = $self->{temporary}? $self->{tmpdir}: $self->{rootdir}; |
| 188 |
$dir.= '/'. ( $self->{category}? $self->{category}: KOHA_UPLOAD ); |
| 189 |
mkdir $dir if !-d $dir; |
| 190 |
return $dir; |
| 191 |
} |
| 192 |
|
| 193 |
sub _full_fname { |
| 194 |
my ( $self, $rec ) = @_; |
| 195 |
if( ref $rec ) { |
| 196 |
return ( $rec->{category}? $self->{rootdir}: $self->{tmpdir} ). |
| 197 |
'/'. $rec->{dir}. '/'. $rec->{hashvalue}. '_'. $rec->{filename}; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
sub _hook { |
| 202 |
my ( $self, $filename, $buffer, $bytes_read, $data ) = @_; |
| 203 |
$self->_compute( $filename, $buffer ); |
| 204 |
my $fh = $self->_fh( $filename ) // $self->_create_file( $filename ); |
| 205 |
print $fh $buffer if $fh; |
| 206 |
} |
| 207 |
|
| 208 |
sub _done { |
| 209 |
my ( $self ) = @_; |
| 210 |
$self->{done} = 1; |
| 211 |
foreach my $f ( keys $self->{files} ) { |
| 212 |
my $fh = $self->_fh($f); |
| 213 |
$self->_register( $f, $fh? tell( $fh ): undef ) |
| 214 |
if !$self->{files}->{$f}->{errcode}; |
| 215 |
$fh->close if $fh; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
sub _register { |
| 220 |
my ( $self, $filename, $size ) = @_; |
| 221 |
my $dbh= C4::Context->dbh; |
| 222 |
my $sql= "INSERT INTO uploaded_files (hashvalue, filename, dir, filesize, |
| 223 |
owner, categorycode) VALUES (?,?,?,?,?,?)"; |
| 224 |
my @pars= ( $self->{files}->{$filename}->{hash}, |
| 225 |
$filename, |
| 226 |
$self->{temporary}? KOHA_UPLOAD: $self->{category}, |
| 227 |
$size, |
| 228 |
$self->{uid}, |
| 229 |
$self->{category}, |
| 230 |
); |
| 231 |
$dbh->do( $sql, undef, @pars ); |
| 232 |
my $i = $dbh->last_insert_id(undef, undef, 'uploaded_files', undef); |
| 233 |
$self->{files}->{$filename}->{id} = $i if $i; |
| 234 |
} |
| 235 |
|
| 236 |
sub _lookup { |
| 237 |
my ( $self, $params ) = @_; |
| 238 |
my $dbh = C4::Context->dbh; |
| 239 |
my $sql = 'SELECT id,hashvalue,filename,dir,categorycode '. |
| 240 |
'FROM uploaded_files '; |
| 241 |
if( $params->{id} ) { |
| 242 |
$sql.= "WHERE id=?"; |
| 243 |
} else { |
| 244 |
$sql.= "WHERE hashvalue=?"; |
| 245 |
} |
| 246 |
my $temp= $dbh->selectall_arrayref( $sql, { Slice => {} }, |
| 247 |
( $params->{id} // $params->{hashvalue} // 0 ) ); |
| 248 |
return $temp; |
| 249 |
} |
| 250 |
|
| 251 |
sub _compute { |
| 252 |
# Computes hash value when sub hook feeds the first block |
| 253 |
# For temporary files, the id is made unique with time |
| 254 |
my ( $self, $name, $block ) = @_; |
| 255 |
if( !$self->{files}->{$name}->{hash} ) { |
| 256 |
my $h = Digest::MD5::md5_hex( $name. ( $self->{uid} // '0' ). |
| 257 |
( $self->{temporary}? Time::HiRes::time(): '' ). |
| 258 |
( $self->{category} // 'tmp' ). substr($block,0,BYTES_DIGEST) ); |
| 259 |
$self->{files}->{$name}->{hash} = $h; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
=head1 AUTHOR |
| 264 |
|
| 265 |
Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands |
| 266 |
|
| 267 |
=cut |
| 268 |
|
| 269 |
1; |