f70aefcb24e8c11b959d98ce989593c983cd4bf6
[koha_gimpoz] / tools / upload-cover-image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2011 C & P Bibliography Services
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 2 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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20 #
21 #
22 =head1 NAME
23
24 upload-cover-image.pl - Script for handling uploading of both single and bulk coverimages and importing them into the database.
25
26 =head1 SYNOPSIS
27
28 upload-cover-image.pl
29
30 =head1 DESCRIPTION
31
32 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.
33 Images will be resized into thumbnails of 140x200 pixels and larger images of
34 800x600 pixels. If the images that are uploaded are larger, they will be
35 resized, maintaining aspect ratio.
36
37 =cut
38
39
40 use strict;
41 use warnings;
42
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
52 my $debug = 1;
53
54 my $input = new CGI;
55
56 my $fileID=$input->param('uploadedfileid');
57 my ($template, $loggedinuser, $cookie)
58         = get_template_and_user({template_name => "tools/upload-images.tmpl",
59                                         query => $input,
60                                         type => "intranet",
61                                         authnotrequired => 0,
62                                         flagsrequired => { tools => 'upload_cover_images'},
63                                         debug => 0,
64                                         });
65
66 my $filetype            = $input->param('filetype');
67 my $biblionumber        = $input->param('biblionumber');
68 my $uploadfilename      = $input->param('uploadfile');
69 my $replace             = !C4::Context->preference("AllowMultipleCovers") || $input->param('replace');
70 my $op                  = $input->param('op');
71 my %cookies             = parse CGI::Cookie($cookie);
72 my $sessionID           = $cookies{'CGISESSID'}->value;
73
74 my $error;
75
76 $template->{VARS}->{'filetype'} = $filetype;
77 $template->{VARS}->{'biblionumber'} = $biblionumber;
78
79 my $total = 0;
80
81
82 if ($fileID) {
83     my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID);
84     if ($filetype eq 'image') {
85         my $fh = $uploaded_file->fh();
86         my $srcimage = GD::Image->new($fh);
87         if (defined $srcimage) {
88             my $dberror = PutImage($biblionumber, $srcimage, $replace);
89             if ($dberror) {
90                 $error = 'DBERR';
91             } else {
92                 $total = 1;
93             }
94         } else {
95             $error = 'OPNIMG';
96         }
97         undef $srcimage;
98     } else {
99         my $filename = $uploaded_file->filename();
100         my $dirname = File::Temp::tempdir( CLEANUP => 1);
101         unless (system("unzip", $filename,  '-d', $dirname) == 0) {
102             $error = 'UZIPFAIL';
103         } else {
104             my @directories;
105             push @directories, "$dirname";
106             foreach my $recursive_dir ( @directories ) {
107                 my $dir;
108                 opendir $dir, $recursive_dir;
109                 while ( my $entry = readdir $dir ) {
110                     push @directories, "$recursive_dir/$entry" if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
111                 }
112                 closedir $dir;
113             }
114             foreach my $dir ( @directories ) {
115                 my $file;
116                 if ( -e "$dir/idlink.txt" ) {
117                     $file = "$dir/idlink.txt";
118                 } elsif ( -e "$dir/datalink.txt" ) {
119                     $file = "$dir/datalink.txt";
120                 } else {
121                     next;
122                 }
123                 if (open (FILE, $file)) {
124                     while (my $line = <FILE>) {
125                         my $delim = ($line =~ /\t/) ? "\t" : ($line =~ /,/) ? "," : "";
126                         #$debug and warn "Delimeter is \'$delim\'";
127                         unless ( $delim eq "," || $delim eq "\t" ) {
128                             warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
129                             $error = 'DELERR';
130                         } else {
131                             ($biblionumber, $filename) = split $delim, $line;
132                             $biblionumber =~ s/[\"\r\n]//g;  # remove offensive characters
133                             $filename   =~ s/[\"\r\n\s]//g;
134                             my $srcimage = GD::Image->new("$dir/$filename");
135                             if (defined $srcimage) {
136                                 $total++;
137                                 my $dberror = PutImage($biblionumber, $srcimage, $replace);
138                                 if ($dberror) {
139                                     $error = 'DBERR';
140                                 }
141                             } else {
142                                 $error = 'OPNIMG';
143                             }
144                             undef $srcimage;
145                         }
146                     }
147                     close(FILE);
148                 } else {
149                     $error = 'OPNLINK';
150                 }
151             }
152         }
153     }
154     $template->{VARS}->{'total'} = $total;
155     $template->{VARS}->{'uploadimage'} = 1;
156     $template->{VARS}->{'error'} = $error;
157     $template->{VARS}->{'biblionumber'} = $biblionumber;
158 }
159
160 output_html_with_http_headers $input, $cookie, $template->output;
161
162 exit 0;
163
164 =head1 AUTHORS
165
166 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
167 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
168 Bible College.
169
170 =cut