]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Vandelay.pm
TPac: Multiple holds in staff client place-holds session
[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 }
53
54 sub spool_marc {
55         my $r = shift;
56         my $cgi = new CGI;
57
58         my $auth = $cgi->param('ses') || $cgi->cookie('ses');
59
60         unless(verify_login($auth)) {
61         $logger->error("authentication failed on vandelay record import: $auth");
62             return Apache2::Const::FORBIDDEN;
63     }
64
65     my $data_fingerprint = '';
66         my $purpose = $cgi->param('purpose') || '';
67         my $infile = $cgi->param('marc_upload') || '';
68     my $bib_source = $cgi->param('bib_source') || '';
69     my $provider = $cgi->param('provider') || '';
70     my $picklist = $cgi->param('picklist') || '';
71     my $create_po = $cgi->param('create_po') || '';
72     my $activate_po = $cgi->param('activate_po') || '';
73     my $ordering_agency = $cgi->param('ordering_agency') || '';
74     my $create_assets = $cgi->param('create_assets') || '';
75
76     $logger->debug("purpose = $purpose, infile = $infile, bib_source = $bib_source ".
77         "provider = $provider, picklist = $picklist, create_po = $create_po, ordering_agency = $ordering_agency");
78
79         my $conf = OpenSRF::Utils::SettingsClient->new;
80         my $dir = $conf->config_value(
81         apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
82
83     unless(-w $dir) {
84         $logger->error("We need some place to store our MARC files");
85             return Apache2::Const::FORBIDDEN;
86     }
87
88     if($infile and -e $infile) {
89         my ($total_bytes, $buf, $bytes) = (0);
90             $data_fingerprint = md5_hex(time."$$".rand());
91         my $outfile = "$dir/$data_fingerprint.mrc";
92
93         unless(open(OUTFILE, ">$outfile")) {
94             $logger->error("unable to open MARC file [$outfile] for writing: $@");
95                 return Apache2::Const::FORBIDDEN;
96         }
97
98         while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
99             $total_bytes += $bytes;
100             if($total_bytes >= $MAX_FILE_SIZE) {
101                 close(OUTFILE);
102                 unlink $outfile;
103                 $logger->error("import exceeded upload size: $MAX_FILE_SIZE");
104                     return Apache2::Const::FORBIDDEN;
105             }
106             print OUTFILE $buf;
107         }
108
109         close(OUTFILE);
110
111             OpenSRF::Utils::Cache->new->put_cache(
112                     'vandelay_import_spool_' . $data_fingerprint,
113                     {   purpose => $purpose, 
114                 path => $outfile,
115                 bib_source => $bib_source,
116                 provider => $provider,
117                 picklist => $picklist,
118                 create_po => $create_po,
119                 create_assets => $create_assets,
120                 ordering_agency => $ordering_agency
121             }
122             );
123     }
124
125     $logger->info("uploaded MARC batch with key $data_fingerprint");
126     $r->content_type('text/plain; charset=utf-8');
127         print "$data_fingerprint";
128         return Apache2::Const::OK;
129 }
130
131 sub verify_login {
132         my $auth_token = shift;
133         return undef unless $auth_token;
134
135         my $user = OpenSRF::AppSession
136                 ->create("open-ils.auth")
137                 ->request( "open-ils.auth.session.retrieve", $auth_token )
138                 ->gather(1);
139
140         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
141                 return undef;
142         }
143
144         return $user if ref($user);
145         return undef;
146 }
147
148 1;