1 package OpenILS::WWW::Vandelay;
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);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
19 use OpenSRF::EX qw(:try);
20 use OpenSRF::Utils::Cache;
22 use OpenSRF::AppSession;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenSRF::Utils::Logger qw/$logger/;
29 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
32 use Digest::MD5 qw/md5_hex/;
33 use OpenSRF::Utils::SettingsClient;
35 use UNIVERSAL::require;
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;
42 # set the bootstrap config and template include directory when
43 # this module is loaded
53 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
54 return Apache2::Const::OK;
61 my $auth = $cgi->param('ses') || $cgi->cookie('ses');
63 unless(verify_login($auth)) {
64 $logger->error("authentication failed on vandelay record import: $auth");
65 return Apache2::Const::FORBIDDEN;
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') || '';
73 $logger->debug("purpose = $purpose, infile = $infile, bib_source = $bib_source");
75 my $conf = OpenSRF::Utils::SettingsClient->new;
76 my $dir = $conf->config_value(
77 apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
80 $logger->error("We need some place to store our MARC files");
81 return Apache2::Const::FORBIDDEN;
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";
89 unless(open(OUTFILE, ">$outfile")) {
90 $logger->error("unable to open MARC file [$outfile] for writing: $@");
91 return Apache2::Const::FORBIDDEN;
94 while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
95 $total_bytes += $bytes;
96 if($total_bytes >= $MAX_FILE_SIZE) {
99 $logger->error("import exceeded upload size: $MAX_FILE_SIZE");
100 return Apache2::Const::FORBIDDEN;
107 OpenSRF::Utils::Cache->new->put_cache(
108 'vandelay_import_spool_' . $data_fingerprint,
109 { purpose => $purpose,
111 bib_source => $bib_source,
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;
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']);
131 $logger->error("spool_jacket: authentication failed on jacket image import: $auth");
132 print '"session not found"';
133 return Apache2::Const::OK;
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;
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;
149 $logger->debug("spool_jacket: PNG compression set to $compression_level");
151 my $max_jacket_size = OpenILS::Application::AppUtils->ou_ancestor_setting_value($user->ws_ou, 'opac.cover_upload_max_file_size') || $MAX_JACKET_SIZE;
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;
162 $logger->debug("infile = $infile, bib_record = $bib_record");
164 my $conf = OpenSRF::Utils::SettingsClient->new;
165 my $dir = $conf->config_value(
166 apps => 'open-ils.vandelay' => app_settings => databases => 'jackets');
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;
174 if($infile and -e $infile) {
175 my $memcache = OpenSRF::Utils::Cache->new('global');
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";
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;
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;
198 print OUTFILE_LARGE $buf;
201 close(OUTFILE_LARGE);
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"); };
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;
220 my ($image_width, $image_height) = $image->getBounds();
222 #### resizing for small
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;
227 my $width_ratio = $target_width / $image_width;
228 my $height_ratio = $target_height / $image_height;
230 my $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
232 my ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
234 my $new_image = $image->copyScaleInterpolated($new_width, $new_height);
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;
241 print OUTFILE_SMALL $new_image->png($compression_level);
242 close(OUTFILE_SMALL);
244 #### resizing for medium
247 $target_height = 200;
249 $width_ratio = $target_width / $image_width;
250 $height_ratio = $target_height / $image_height;
252 $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
254 ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
256 $new_image = $image->copyScaleInterpolated($new_width, $new_height);
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;
263 print OUTFILE_MEDIUM $new_image->png($compression_level);
264 close(OUTFILE_MEDIUM);
266 #### resizing for large
269 $target_height = 787;
271 $width_ratio = $target_width / $image_width;
272 $height_ratio = $target_height / $image_height;
274 $best_ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
276 ($new_width, $new_height) = ($image_width * $best_ratio, $image_height * $best_ratio);
278 $new_image = $image->copyScaleInterpolated($new_width, $new_height);
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;
285 print OUTFILE_LARGE $new_image->png($compression_level);
286 close(OUTFILE_LARGE);
288 #### renaming temp files to final images
290 rename "$outfile_small.temp", $outfile_small;
291 rename "$outfile_medium.temp", $outfile_medium;
292 rename "$outfile_large.temp", $outfile_large;
294 #### clearing memcache
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);
303 $logger->error("spool_jacket: image not uploaded? check form action and encoding");
304 print '"upload error"';
305 return Apache2::Const::OK;
308 $logger->info("spool_jacket: uploaded jacket file for record $bib_record");
309 $r->content_type('text/plain; charset=utf-8');
311 return Apache2::Const::OK;
315 my $auth_token = shift;
316 return undef unless $auth_token;
318 my $user = OpenSRF::AppSession
319 ->create("open-ils.auth")
320 ->request( "open-ils.auth.session.retrieve", $auth_token )
323 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
327 return $user if ref($user);
331 sub verify_permission { # FIXME: could refactor these verify_ subs in WWW/
332 my ($token, $user, $org_unit, $permissions) = @_;
334 my $failures = OpenSRF::AppSession
335 ->create('open-ils.actor')
336 ->request('open-ils.actor.user.perm.check', $token, $user->id, $org_unit, $permissions)
339 return !scalar(@$failures);