3 # Copyright 2011 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 upload-cover-image.pl - Script for handling uploading of both single and bulk coverimages and importing them into the database.
33 This script is called and presents the user with an interface allowing him/her to upload a single cover image or bulk cover images via a zip file.
34 Images will be resized into thumbnails of 140x200 pixels and larger images of
35 800x600 pixels. If the images that are uploaded are larger, they will be
36 resized, maintaining aspect ratio.
46 use C4::Auth qw( get_template_and_user );
47 use C4::Output qw( output_html_with_http_headers );
49 use Koha::CoverImages;
51 use Koha::UploadedFiles;
52 use C4::Log qw( logaction );
56 my $fileID = $input->param('uploadedfileid');
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59 template_name => "tools/upload-images.tt",
62 flagsrequired => { tools => 'upload_local_cover_images' },
66 my $filetype = $input->param('filetype');
67 my $biblionumber = $input->param('biblionumber');
68 my $itemnumber = $input->param('itemnumber');
69 #my $uploadfilename = $input->param('uploadfile'); # obsolete?
70 my $replace = !C4::Context->preference("AllowMultipleCovers")
71 || $input->param('replace');
72 my $op = $input->param('op');
73 my %cookies = parse CGI::Cookie($cookie);
74 my $sessionID = $cookies{'CGISESSID'}->value;
83 $item = Koha::Items->find($itemnumber);
84 $biblionumber = $item->biblionumber;
85 $biblio = Koha::Biblios->find( $biblionumber );
86 $cover_images = $item->cover_images->as_list;
87 } elsif ( $biblionumber ){
88 $biblio = Koha::Biblios->find( $biblionumber );
89 $cover_images = $biblio->cover_images->as_list;
93 filetype => $filetype,
95 biblionumber => $biblionumber,
96 itemnumber => $itemnumber,
97 cover_images => $cover_images,
104 my $upload = Koha::UploadedFiles->find( $fileID );
105 if ( $filetype eq 'image' ) {
106 my $fh = $upload->file_handle;
107 my $srcimage = GD::Image->new($fh);
109 if ( defined $srcimage ) {
112 if ( $biblionumber ) {
113 Koha::Biblios->find($biblionumber)->cover_images->delete;
114 } elsif ( $itemnumber ) {
115 Koha::Items->find($itemnumber)->cover_images->delete;
119 Koha::CoverImage->new(
121 biblionumber => $biblionumber,
122 itemnumber => $itemnumber,
123 src_image => $srcimage
142 my $filename = $upload->full_path;
143 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
144 qx/unzip $filename -d $dirname/;
146 unless ( $exit_code == 0 ) {
151 push @directories, "$dirname";
152 foreach my $recursive_dir (@directories) {
154 opendir $dir, $recursive_dir;
155 while ( my $entry = readdir $dir ) {
156 push @directories, "$recursive_dir/$entry"
157 if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
161 foreach my $dir (@directories) {
163 if ( -e "$dir/idlink.txt" ) {
164 $file = "$dir/idlink.txt";
166 elsif ( -e "$dir/datalink.txt" ) {
167 $file = "$dir/datalink.txt";
172 if ( open( my $fh, '<', $file ) ) {
173 while ( my $line = <$fh> ) {
175 ( $line =~ /\t/ ) ? "\t"
176 : ( $line =~ /,/ ) ? ","
179 unless ( $delim eq "," || $delim eq "\t" ) {
181 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
185 ( $biblionumber, $filename ) = split $delim, $line, 2;
187 s/[\"\r\n]//g; # remove offensive characters
188 $filename =~ s/[\"\r\n]//g;
189 $filename =~ s/^\s+//;
190 $filename =~ s/\s+$//;
191 if (C4::Context->preference("CataloguingLog")) {
192 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
194 my $srcimage = GD::Image->new("$dir/$filename");
197 if ( defined $srcimage ) {
201 if ( $biblionumber ) {
202 $biblio = Koha::Biblios->find( $biblionumber );
203 $biblio->cover_images->delete;
204 } elsif ( $itemnumber ) {
205 $item = Koha::Items->find($itemnumber);
206 $item->cover_images->delete;
207 $biblio = Koha::Biblios->find( $item->{biblionumber} );
211 $biblio = Koha::Biblios->find( $biblionumber );
212 } elsif ( $itemnumber ){
213 $item = Koha::Items->find($itemnumber);
214 $biblio = Koha::Biblios->find( $item->{biblionumber} );
221 biblionumber => $biblionumber,
222 itemnumber => $itemnumber,
223 title => $biblio->title
226 Koha::CoverImage->new(
228 biblionumber => $biblionumber,
229 itemnumber => $itemnumber,
230 src_image => $srcimage
258 biblionumber => $biblionumber || Koha::Items->find($itemnumber)->biblionumber,
259 itemnumber => $itemnumber,
261 } elsif ( @results ){
268 print $input->redirect("/cgi-bin/koha/tools/upload-cover-image.pl?biblionumber=$biblionumber&itemnumber=$itemnumber");
272 output_html_with_http_headers $input, $cookie, $template->output;
278 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
279 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation