]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Vandelay.pm
2014e70cc453de5d9d83599c04087c62695171a0
[Evergreen.git] / Open-ILS / src / perlmods / 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;
29
30 use MIME::Base64;
31 use Digest::MD5 qw/md5_hex/;
32
33 use UNIVERSAL::require;
34
35 our @formats = qw/USMARC UNIMARC XML BRE/;
36
37 # set the bootstrap config and template include directory when
38 # this module is loaded
39 my $bootstrap;
40
41 sub import {
42         my $self = shift;
43         $bootstrap = shift;
44 }
45
46
47 sub child_init {
48         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
49 }
50
51 sub spool_marc {
52         my $r = shift;
53         my $cgi = new CGI;
54
55         my $auth = $cgi->param('ses') || $cgi->cookie('ses');
56
57         return Apache2::Const::FORBIDDEN unless verify_login($auth);
58
59     my $data_fingerprint = '';
60         my $purpose = $cgi->param('purpose');
61         my $file = $cgi->param('marc_upload');
62
63     if($file and -e $file) {
64
65             my $data = join '', (<$file>);
66             $data = encode_base64($data);
67     
68             $data_fingerprint = md5_hex($data);
69     
70             OpenSRF::Utils::Cache->new->put_cache(
71                     'vandelay_import_spool_' . $data_fingerprint,
72                     { purpose => $purpose, marc => $data }
73             );
74     }
75
76     $r->content_type('text/plain; charset=utf-8');
77         print "$data_fingerprint";
78         return Apache2::Const::OK;
79 }
80
81 sub verify_login {
82         my $auth_token = shift;
83         return undef unless $auth_token;
84
85         my $user = OpenSRF::AppSession
86                 ->create("open-ils.auth")
87                 ->request( "open-ils.auth.session.retrieve", $auth_token )
88                 ->gather(1);
89
90         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
91                 return undef;
92         }
93
94         return $user if ref($user);
95         return undef;
96 }
97
98 1;