]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Vandelay.pm
mod_perl expects child_init return values
[working/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 FORBIDDEN :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
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::Utils::Cache;
20 use OpenSRF::System;
21 use OpenSRF::AppSession;
22 use XML::LibXML;
23
24 use OpenILS::Utils::Fieldmapper;
25 use OpenSRF::Utils::Logger qw/$logger/;
26
27 use MARC::Record;
28 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
29
30 use MIME::Base64;
31 use Digest::MD5 qw/md5_hex/;
32 use OpenSRF::Utils::SettingsClient;
33
34 use UNIVERSAL::require;
35
36 our @formats = qw/USMARC UNIMARC XML BRE/;
37 my $MAX_FILE_SIZE = 10737418240; #10G
38 my $FILE_READ_SIZE = 4096;
39
40 # set the bootstrap config and template include directory when
41 # this module is loaded
42 my $bootstrap;
43
44 sub import {
45         my $self = shift;
46         $bootstrap = shift;
47 }
48
49
50 sub child_init {
51         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
52         return Apache2::Const::OK;
53 }
54
55 sub spool_marc {
56         my $r = shift;
57         my $cgi = new CGI;
58
59         my $auth = $cgi->param('ses') || $cgi->cookie('ses');
60
61         unless(verify_login($auth)) {
62         $logger->error("authentication failed on vandelay record import: $auth");
63             return Apache2::Const::FORBIDDEN;
64     }
65
66     my $data_fingerprint = '';
67         my $purpose = $cgi->param('purpose') || '';
68         my $infile = $cgi->param('marc_upload') || '';
69     my $bib_source = $cgi->param('bib_source') || '';
70
71     $logger->debug("purpose = $purpose, infile = $infile, bib_source = $bib_source");
72
73         my $conf = OpenSRF::Utils::SettingsClient->new;
74         my $dir = $conf->config_value(
75         apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
76
77     unless(-w $dir) {
78         $logger->error("We need some place to store our MARC files");
79             return Apache2::Const::FORBIDDEN;
80     }
81
82     if($infile and -e $infile) {
83         my ($total_bytes, $buf, $bytes) = (0);
84             $data_fingerprint = md5_hex(time."$$".rand());
85         my $outfile = "$dir/$data_fingerprint.mrc";
86
87         unless(open(OUTFILE, ">$outfile")) {
88             $logger->error("unable to open MARC file [$outfile] for writing: $@");
89                 return Apache2::Const::FORBIDDEN;
90         }
91
92         while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
93             $total_bytes += $bytes;
94             if($total_bytes >= $MAX_FILE_SIZE) {
95                 close(OUTFILE);
96                 unlink $outfile;
97                 $logger->error("import exceeded upload size: $MAX_FILE_SIZE");
98                     return Apache2::Const::FORBIDDEN;
99             }
100             print OUTFILE $buf;
101         }
102
103         close(OUTFILE);
104
105             OpenSRF::Utils::Cache->new->put_cache(
106                     'vandelay_import_spool_' . $data_fingerprint,
107                     {   purpose => $purpose, 
108                 path => $outfile,
109                 bib_source => $bib_source,
110             }
111             );
112     }
113
114     $logger->info("uploaded MARC batch with key $data_fingerprint");
115     $r->content_type('text/plain; charset=utf-8');
116         print "$data_fingerprint";
117         return Apache2::Const::OK;
118 }
119
120 sub verify_login {
121         my $auth_token = shift;
122         return undef unless $auth_token;
123
124         my $user = OpenSRF::AppSession
125                 ->create("open-ils.auth")
126                 ->request( "open-ils.auth.session.retrieve", $auth_token )
127                 ->gather(1);
128
129         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
130                 return undef;
131         }
132
133         return $user if ref($user);
134         return undef;
135 }
136
137 1;