]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Vandelay.pm
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / Vandelay.pm
1 package OpenILS::WWW::Vandelay;
2 use strict;
3 use warnings;
4 use bytes;
5
6 use Apache2::Log;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND AUTH_REQUIRED FORBIDDEN HTTP_UNAUTHORIZED HTTP_REQUEST_ENTITY_TOO_LARGE HTTP_INTERNAL_SERVER_ERROR :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use APR::Table;
10
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
14 use CGI;
15 use Data::Dumper;
16 use Text::CSV;
17 use GD;
18
19 use OpenSRF::EX qw(:try);
20 use OpenSRF::Utils::Cache;
21 use OpenSRF::System;
22 use OpenSRF::AppSession;
23 use XML::LibXML;
24
25 use OpenILS::Utils::Fieldmapper;
26 use OpenSRF::Utils::Logger qw/$logger/;
27
28 use MARC::Record;
29 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
30
31 use MIME::Base64;
32 use Digest::MD5 qw/md5_hex/;
33 use OpenSRF::Utils::SettingsClient;
34
35 use UNIVERSAL::require;
36
37 our @formats = qw/USMARC UNIMARC XML BRE/;
38 my $MAX_FILE_SIZE = 10737418240; #10G
39 my $MAX_JACKET_SIZE = 10737418240; #10G
40 my $FILE_READ_SIZE = 4096;
41
42 # set the bootstrap config and template include directory when
43 # this module is loaded
44 my $bootstrap;
45
46 sub import {
47         my $self = shift;
48         $bootstrap = shift;
49 }
50
51
52 sub child_init {
53         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
54         return Apache2::Const::OK;
55 }
56
57 sub spool_marc {
58     my $r = shift;
59     my $cgi = new CGI;
60
61     my $auth = $cgi->param('ses') || $cgi->cookie('ses');
62
63     unless(verify_login($auth)) {
64         $logger->error("authentication failed on vandelay record import: $auth");
65         return Apache2::Const::FORBIDDEN;
66     }
67
68     my $data_fingerprint = '';
69     my $purpose = $cgi->param('purpose') || '';
70     my $infile = $cgi->param('marc_upload') || '';
71     my $bib_source = $cgi->param('bib_source') || '';
72
73     $logger->debug("purpose = $purpose, infile = $infile, bib_source = $bib_source");
74
75     my $conf = OpenSRF::Utils::SettingsClient->new;
76     my $dir = $conf->config_value(
77         apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
78
79     unless(-w $dir) {
80         $logger->error("We need some place to store our MARC files");
81         return Apache2::Const::FORBIDDEN;
82     }
83
84     if($infile and -e $infile) {
85         my ($total_bytes, $buf, $bytes) = (0);
86         $data_fingerprint = md5_hex(time."$$".rand());
87         my $outfile = "$dir/$data_fingerprint.mrc";
88
89         unless(open(OUTFILE, ">$outfile")) {
90             $logger->error("unable to open MARC file [$outfile] for writing: $@");
91             return Apache2::Const::FORBIDDEN;
92         }
93
94         while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
95             $total_bytes += $bytes;
96             if($total_bytes >= $MAX_FILE_SIZE) {
97                 close(OUTFILE);
98                 unlink $outfile;
99                 $logger->error("import exceeded upload size: $MAX_FILE_SIZE");
100                 return Apache2::Const::FORBIDDEN;
101             }
102             print OUTFILE $buf;
103         }
104
105         close(OUTFILE);
106
107         OpenSRF::Utils::Cache->new->put_cache(
108             'vandelay_import_spool_' . $data_fingerprint,
109             {   purpose => $purpose, 
110                 path => $outfile,
111                 bib_source => $bib_source,
112             }
113         );
114     }
115
116     $logger->info("uploaded MARC batch with key $data_fingerprint");
117     $r->content_type('text/plain; charset=utf-8');
118     print "$data_fingerprint";
119     return Apache2::Const::OK;
120 }
121
122 sub spool_jacket {
123     my $r = shift;
124     my $cgi = new CGI;
125
126     my $auth = $cgi->param('ses') || $cgi->cookie('ses');
127     my $user = verify_login($auth);
128     my $perm_check = verify_permission($auth, $user, $user->ws_ou, ['UPLOAD_COVER_IMAGE']);
129
130     unless($user) {
131         $logger->error("spool_jacket: authentication failed on jacket image import: $auth");
132         print '"session not found"';
133         return Apache2::Const::OK;
134     }
135     unless($perm_check) {
136         $logger->error("spool_jacket: authorization failed on jacket image import: $auth");
137         print '"permission denied"';
138         return Apache2::Const::OK;
139     }
140
141     my $ses = OpenSRF::AppSession->create('open-ils.cstore');
142     my $compression_flag = $ses->request( 'open-ils.cstore.direct.config.global_flag.retrieve', 'opac.cover_upload_compression' )->gather(1);
143     my $compression_level = ($compression_flag && OpenILS::Application::AppUtils->is_true($compression_flag->enabled)) ? $compression_flag->value : -1;
144     if ($compression_level < -1 || $compression_level > 9) {
145         $r->content_type('text/plain; charset=utf-8');
146         print '"invalid compression level"';
147         return Apache2::Const::OK;
148     }
149     $logger->debug("spool_jacket: PNG compression set to $compression_level");
150
151     my $max_jacket_size = OpenILS::Application::AppUtils->ou_ancestor_setting_value($user->ws_ou, 'opac.cover_upload_max_file_size') || $MAX_JACKET_SIZE;
152
153     my $infile = $cgi->param('jacket_upload') || '';
154     my $bib_record = $cgi->param('bib_record') || '';
155     unless ($bib_record =~ /^-?\d+$/) {
156         $logger->error("spool_jacket: passed bib_record = $bib_record");
157         $r->content_type('text/plain; charset=utf-8');
158         print '"bib not found"';
159         return Apache2::Const::OK;
160     }
161
162     $logger->debug("infile = $infile, bib_record = $bib_record");
163
164     my $conf = OpenSRF::Utils::SettingsClient->new;
165     my $dir = $conf->config_value(
166         apps => 'open-ils.vandelay' => app_settings => databases => 'jackets');
167
168     unless(-w $dir) { # FIXME: good or bad idea to fallback to /openils/var/web/opac/extracs/ac if opensrf.xml is not updated?
169         $logger->error("spool_jacket: We need some place to store our jacket files");
170         print '"jacket location not configured"';
171         return Apache2::Const::OK;
172     }
173
174     if($infile and -e $infile) {
175         my $memcache = OpenSRF::Utils::Cache->new('global');
176
177         my ($total_bytes, $buf, $bytes) = (0);
178         my $outfile_large = "$dir/jacket/large/r/$bib_record";
179         my $outfile_medium = "$dir/jacket/medium/r/$bib_record";
180         my $outfile_small = "$dir/jacket/small/r/$bib_record";
181
182         unless(open(OUTFILE_LARGE, ">$outfile_large.temp")) {
183             $logger->error("spool_jacket: unable to open jacket file [$outfile_large.temp] for writing: $@");
184             return Apache2::Const::FORBIDDEN;
185             print '"unable to open file for writing"';
186             return Apache2::Const::OK;
187         }
188
189         while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
190             $total_bytes += $bytes;
191             if($total_bytes >= $max_jacket_size) {
192                 close(OUTFILE_LARGE);
193                 unlink $outfile_large . ".temp";
194                 $logger->error("spool_jacket: import exceeded upload size: $max_jacket_size");
195                 print '"file too large"';
196                 return Apache2::Const::OK;
197             }
198             print OUTFILE_LARGE $buf;
199         }
200
201         close(OUTFILE_LARGE);
202
203         my $image;
204         eval { $image = GD::Image->newFromPng("$outfile_large.temp"); };
205         eval { $image = $image || GD::Image->newFromJpeg("$outfile_large.temp"); };
206         eval { $image = $image || GD::Image->newFromGif("$outfile_large.temp"); };
207         eval { $image = $image || GD::Image->newFromXpm("$outfile_large.temp"); };
208         eval { $image = $image || GD::Image->newFromWBMP("$outfile_large.temp"); };
209         eval { $image = $image || GD::Image->newFromXbm("$outfile_large.temp"); };
210         eval { $image = $image || GD::Image->newFromGd("$outfile_large.temp"); };
211         eval { $image = $image || GD::Image->newFromGd2("$outfile_large.temp"); };
212         unless ($image) {
213             unlink $outfile_large . ".temp";
214             $logger->error("spool_jacket: unable to parse $outfile_large.temp");
215             $r->content_type('text/plain; charset=utf-8');
216             print '"parse error"';
217             return Apache2::Const::OK;
218         }
219
220         my ($image_width, $image_height) = $image->getBounds();
221
222         #### resizing for small
223
224         my $target_width = 55; # FIXME: get these from settings, but for now, using customer desired width and aspect ratio observed from OpenLibrary
225         my $target_height = 91;
226
227         my $width_ratio = $target_width / $image_width;
228         my $height_ratio = $target_height / $image_height;
229
230         my $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
231
232         my ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
233
234         my $new_image = $image->copyScaleInterpolated($new_width, $new_height);
235
236         unless(open(OUTFILE_SMALL, ">$outfile_small.temp")) {
237             $logger->error("spool_jacket: unable to open jacket file [$outfile_small.temp] for writing: $@");
238             print '"unable to open file for writing"';
239             return Apache2::Const::OK;
240         }
241         print OUTFILE_SMALL $new_image->png($compression_level);
242         close(OUTFILE_SMALL);
243
244         #### resizing for medium
245
246         $target_width = 120;
247         $target_height = 200;
248
249         $width_ratio = $target_width / $image_width;
250         $height_ratio = $target_height / $image_height;
251
252         $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
253
254         ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
255
256         $new_image = $image->copyScaleInterpolated($new_width, $new_height);
257
258         unless(open(OUTFILE_MEDIUM, ">$outfile_medium.temp")) {
259             $logger->error("spool_jacket: unable to open jacket file [$outfile_medium.temp] for writing: $@");
260             print '"unable to open file for writing"';
261             return Apache2::Const::OK;
262         }
263         print OUTFILE_MEDIUM $new_image->png($compression_level);
264         close(OUTFILE_MEDIUM);
265
266         #### resizing for large
267
268         $target_width = 475;
269         $target_height = 787;
270
271         $width_ratio = $target_width / $image_width;
272         $height_ratio = $target_height / $image_height;
273
274         $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
275
276         ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
277
278         $new_image = $image->copyScaleInterpolated($new_width, $new_height);
279
280         unless(open(OUTFILE_LARGE, ">$outfile_large.temp")) {
281             $logger->error("spool_jacket: unable to open jacket file [$outfile_large.temp] for writing: $@");
282             print "'unable to open file for writing'\n";
283             return Apache2::Const::OK;
284         }
285         print OUTFILE_LARGE $new_image->png($compression_level);
286         close(OUTFILE_LARGE);
287
288         #### renaming temp files to final images
289
290         rename "$outfile_small.temp", $outfile_small;
291         rename "$outfile_medium.temp", $outfile_medium;
292         rename "$outfile_large.temp", $outfile_large;
293
294         #### clearing memcache
295
296         my @jacket_sizes = ('large','medium','small');
297         foreach my $size (@jacket_sizes) {
298             my $key = "ac.jacket.$size.record_$bib_record";
299             $memcache->delete_cache($key);
300         }
301
302     } else {
303         $logger->error("spool_jacket: image not uploaded? check form action and encoding");
304         print '"upload error"';
305         return Apache2::Const::OK;
306     }
307
308     $logger->info("spool_jacket: uploaded jacket file for record $bib_record");
309     $r->content_type('text/plain; charset=utf-8');
310     print "1";
311     return Apache2::Const::OK;
312 }
313
314 sub verify_login {
315         my $auth_token = shift;
316         return undef unless $auth_token;
317
318         my $user = OpenSRF::AppSession
319                 ->create("open-ils.auth")
320                 ->request( "open-ils.auth.session.retrieve", $auth_token )
321                 ->gather(1);
322
323         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
324                 return undef;
325         }
326
327         return $user if ref($user);
328         return undef;
329 }
330
331 sub verify_permission { # FIXME: could refactor these verify_ subs in WWW/
332     my ($token, $user, $org_unit, $permissions) = @_;
333
334     my $failures = OpenSRF::AppSession
335         ->create('open-ils.actor')
336         ->request('open-ils.actor.user.perm.check', $token, $user->id, $org_unit, $permissions)
337         ->gather(1);
338
339     return !scalar(@$failures);
340 }
341
342 1;