1 package OpenILS::WWW::Vandelay;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND FORBIDDEN :log);
8 use APR::Const -compile => qw(:error SUCCESS);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::Utils::Cache;
21 use OpenSRF::AppSession;
24 use OpenILS::Utils::Fieldmapper;
25 use OpenSRF::Utils::Logger qw/$logger/;
31 use Digest::MD5 qw/md5_hex/;
32 use OpenSRF::Utils::SettingsClient;
34 use UNIVERSAL::require;
36 our @formats = qw/USMARC UNIMARC XML BRE/;
37 my $MAX_FILE_SIZE = 10737418240; #10G
38 my $FILE_READ_SIZE = 4096;
40 # set the bootstrap config and template include directory when
41 # this module is loaded
51 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
58 my $auth = $cgi->param('ses') || $cgi->cookie('ses');
60 unless(verify_login($auth)) {
61 $logger->error("authentication failed on vandelay record import: $auth");
62 return Apache2::Const::FORBIDDEN;
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 $ordering_agency = $cgi->param('ordering_agency') || '';
73 my $create_assets = $cgi->param('create_assets') || '';
75 $logger->debug("purpose = $purpose, infile = $infile, bib_source = $bib_source ".
76 "provider = $provider, picklist = $picklist, create_po = $create_po, ordering_agency = $ordering_agency");
78 my $conf = OpenSRF::Utils::SettingsClient->new;
79 my $dir = $conf->config_value(
80 apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
83 $logger->error("We need some place to store our MARC files");
84 return Apache2::Const::FORBIDDEN;
87 if($infile and -e $infile) {
88 my ($total_bytes, $buf, $bytes) = (0);
89 $data_fingerprint = md5_hex(time."$$".rand());
90 my $outfile = "$dir/$data_fingerprint.mrc";
92 unless(open(OUTFILE, ">$outfile")) {
93 $logger->error("unable to open MARC file [$outfile] for writing: $@");
94 return Apache2::Const::FORBIDDEN;
97 while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
98 $total_bytes += $bytes;
99 if($total_bytes >= $MAX_FILE_SIZE) {
102 $logger->error("import exceeded upload size: $MAX_FILE_SIZE");
103 return Apache2::Const::FORBIDDEN;
110 OpenSRF::Utils::Cache->new->put_cache(
111 'vandelay_import_spool_' . $data_fingerprint,
112 { purpose => $purpose,
114 bib_source => $bib_source,
115 provider => $provider,
116 picklist => $picklist,
117 create_po => $create_po,
118 create_assets => $create_assets,
119 ordering_agency => $ordering_agency
124 $logger->info("uploaded MARC batch with key $data_fingerprint");
125 $r->content_type('text/plain; charset=utf-8');
126 print "$data_fingerprint";
127 return Apache2::Const::OK;
131 my $auth_token = shift;
132 return undef unless $auth_token;
134 my $user = OpenSRF::AppSession
135 ->create("open-ils.auth")
136 ->request( "open-ils.auth.session.retrieve", $auth_token )
139 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
143 return $user if ref($user);