Merge remote-tracking branch 'origin/new/bug_6720'
[koha.git] / tools / upload-cover-image.pl
blob9fe1ad43197a661f1ec23d1c4af223187c0a16e9
1 #!/usr/bin/perl
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
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.
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.
23 =head1 NAME
25 upload-cover-image.pl - Script for handling uploading of both single and bulk coverimages and importing them into the database.
27 =head1 SYNOPSIS
29 upload-cover-image.pl
31 =head1 DESCRIPTION
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.
38 =cut
40 use strict;
41 use warnings;
43 use File::Temp;
44 use CGI;
45 use GD;
46 use C4::Context;
47 use C4::Auth;
48 use C4::Output;
49 use C4::Images;
50 use C4::UploadedFile;
51 use C4::Log;
53 my $debug = 1;
55 my $input = new CGI;
57 my $fileID = $input->param('uploadedfileid');
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60 template_name => "tools/upload-images.tmpl",
61 query => $input,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => { tools => 'upload_cover_images' },
65 debug => 0,
69 my $filetype = $input->param('filetype');
70 my $biblionumber = $input->param('biblionumber');
71 my $uploadfilename = $input->param('uploadfile');
72 my $replace = !C4::Context->preference("AllowMultipleCovers")
73 || $input->param('replace');
74 my $op = $input->param('op');
75 my %cookies = parse CGI::Cookie($cookie);
76 my $sessionID = $cookies{'CGISESSID'}->value;
78 my $error;
80 $template->{VARS}->{'filetype'} = $filetype;
81 $template->{VARS}->{'biblionumber'} = $biblionumber;
83 my $total = 0;
85 if ($fileID) {
86 my $uploaded_file = C4::UploadedFile->fetch( $sessionID, $fileID );
87 if ( $filetype eq 'image' ) {
88 my $fh = $uploaded_file->fh();
89 my $srcimage = GD::Image->new($fh);
90 if ( defined $srcimage ) {
91 my $dberror = PutImage( $biblionumber, $srcimage, $replace );
92 if ($dberror) {
93 $error = 'DBERR';
95 else {
96 $total = 1;
99 else {
100 $error = 'OPNIMG';
102 undef $srcimage;
104 else {
105 my $filename = $uploaded_file->filename();
106 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
107 unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) {
108 $error = 'UZIPFAIL';
110 else {
111 my @directories;
112 push @directories, "$dirname";
113 foreach my $recursive_dir (@directories) {
114 my $dir;
115 opendir $dir, $recursive_dir;
116 while ( my $entry = readdir $dir ) {
117 push @directories, "$recursive_dir/$entry"
118 if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
120 closedir $dir;
122 foreach my $dir (@directories) {
123 my $file;
124 if ( -e "$dir/idlink.txt" ) {
125 $file = "$dir/idlink.txt";
127 elsif ( -e "$dir/datalink.txt" ) {
128 $file = "$dir/datalink.txt";
130 else {
131 next;
133 if ( open( FILE, $file ) ) {
134 while ( my $line = <FILE> ) {
135 my $delim =
136 ( $line =~ /\t/ ) ? "\t"
137 : ( $line =~ /,/ ) ? ","
138 : "";
140 #$debug and warn "Delimeter is \'$delim\'";
141 unless ( $delim eq "," || $delim eq "\t" ) {
142 warn
143 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
144 $error = 'DELERR';
146 else {
147 ( $biblionumber, $filename ) = split $delim, $line, 2;
148 $biblionumber =~
149 s/[\"\r\n]//g; # remove offensive characters
150 $filename =~ s/[\"\r\n]//g;
151 $filename =~ s/^\s+//;
152 $filename =~ s/\s+$//;
153 if (C4::Context->preference("CataloguingLog")) {
154 logaction('CATALOGUING', 'MODIFY', $biblionumber, "cover image: $filename");
156 my $srcimage = GD::Image->new("$dir/$filename");
157 if ( defined $srcimage ) {
158 $total++;
159 my $dberror =
160 PutImage( $biblionumber, $srcimage,
161 $replace );
162 if ($dberror) {
163 $error = 'DBERR';
166 else {
167 $error = 'OPNIMG';
169 undef $srcimage;
172 close(FILE);
174 else {
175 $error = 'OPNLINK';
180 $template->{VARS}->{'total'} = $total;
181 $template->{VARS}->{'uploadimage'} = 1;
182 $template->{VARS}->{'error'} = $error;
183 $template->{VARS}->{'biblionumber'} = $biblionumber;
186 output_html_with_http_headers $input, $cookie, $template->output;
188 exit 0;
190 =head1 AUTHORS
192 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
193 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
194 Bible College.
196 =cut