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 return Apache2::Const::FORBIDDEN unless verify_login($auth);
62 my $data_fingerprint = '';
63 my $purpose = $cgi->param('purpose');
64 my $infile = $cgi->param('marc_upload');
66 my $conf = OpenSRF::Utils::SettingsClient->new;
67 my $dir = $conf->config_value(
68 apps => 'open-ils.vandelay' => app_settings => databases => 'importer');
71 $logger->error("We need some place to store our MARC files");
72 return Apache2::Const::FORBIDDEN;
75 if($infile and -e $infile) {
76 my ($total_bytes, $buf, $bytes) = (0);
77 $data_fingerprint = md5_hex(time."$$".rand());
78 my $outfile = "$dir/$data_fingerprint.mrc";
80 unless(open(OUTFILE, ">$outfile")) {
81 $logger->error("unable to open MARC file for writing: $@");
82 return Apache2::Const::FORBIDDEN;
85 while($bytes = sysread($infile, $buf, $FILE_READ_SIZE)) {
86 $total_bytes += $bytes;
87 if($total_bytes >= $MAX_FILE_SIZE) {
90 return Apache2::Const::FORBIDDEN;
97 OpenSRF::Utils::Cache->new->put_cache(
98 'vandelay_import_spool_' . $data_fingerprint,
99 { purpose => $purpose, path => $outfile }
103 $r->content_type('text/plain; charset=utf-8');
104 print "$data_fingerprint";
105 return Apache2::Const::OK;
109 my $auth_token = shift;
110 return undef unless $auth_token;
112 my $user = OpenSRF::AppSession
113 ->create("open-ils.auth")
114 ->request( "open-ils.auth.session.retrieve", $auth_token )
117 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
121 return $user if ref($user);