]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline-upload.pl
allowing clients to create their own session names
[Evergreen.git] / Open-ILS / src / offline / offline-upload.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3
4 # --------------------------------------------------------------------
5 #  Uploads offline action files
6 #       pending files go into $base_dir/pending/<org>/<ws>.log
7 #       completed transactions go into 
8 #       $base_dir/archive/<org>/YYYMMDDHHMM/<ws>.log
9 # --------------------------------------------------------------------
10
11 our $U;
12 our $logger;
13 my $MAX_FILE_SIZE = 1000000000; # - roughly 1G upload file size max
14 require 'offline-lib.pl';
15
16
17 &execute();
18 # --------------------------------------------------------------------
19
20
21 # --------------------------------------------------------------------
22 # If the file is present, load it up, otherwise prompt with a very
23 # basic HTML upload form
24 # --------------------------------------------------------------------
25 sub execute {
26
27         if( &offline_cgi->param('file') ) { 
28
29                 &load_file(); 
30                 &handle_event(OpenILS::Event->new('SUCCESS', payload => &offline_seskey));
31
32         } else {
33                 &display_upload(); 
34         }
35 }
36
37
38 # --------------------------------------------------------------------
39 # Loads the POSTed file and writes the contents to disk
40 # --------------------------------------------------------------------
41 sub load_file() {
42
43         my $wsname      = &offline_cgi->param('ws');
44         my $filehandle = &offline_cgi->upload('file');
45
46         # make sure we have upload priveleges
47         my $evt = $U->check_perms(&offline_requestor->id, &offline_org, 'OFFLINE_UPLOAD');
48         handle_event($evt) if $evt;
49
50         my $output = &offline_pending_dir(1) . '/' . "$wsname.log";
51
52         &handle_event(OpenILS::Event->new('OFFLINE_SESSION_ACTIVE')) if( -e &offline_lock_file );
53         &handle_event(OpenILS::Event->new('OFFLINE_SESSION_FILE_EXISTS')) if( -e $output );
54
55         $logger->debug("offline: Writing log file $output");
56         my $numbytes = 0;
57         my $string = "";
58
59         my $cs = &offline_cgi->param('checksum');
60
61         open(FILE, ">$output");
62
63         while( <$filehandle> ) { 
64                 $numbytes += length "$_";
65                 $string .= "$_" if $cs;
66
67                 if( $numbytes > $MAX_FILE_SIZE ) {
68                         close(FILE);
69                         unlink($output);
70                         &handle_event('OFFLINE_FILE_ERROR');
71                 }
72
73                 print FILE; 
74         }
75         close(FILE);
76
77         if($cs) {
78                 my $md5 = md5_hex($string);
79                 $logger->debug("offline: received checksum $cs, our data shows $md5");
80                 &handle_event(OpenILS::Event->new('OFFLINE_CHECKSUM_FAILED')) if( $md5 ne $cs ) ;
81         }
82
83
84         # Append the metadata for this workstations upload
85         append_meta( {
86                 requestor       => &offline_requestor->id, 
87                 timestamp       => time, 
88                 workstation => $wsname,
89                 log                     => $output, 
90                 delta                   => &offline_time_delta}, 
91                 );
92 }
93
94
95 # --------------------------------------------------------------------
96 # Use this for testing manual uploads
97 # --------------------------------------------------------------------
98 sub display_upload {
99
100         my $ws = &offline_cgi->param('ws') || "";
101         my $cs = &offline_cgi->param('checksum') || "";
102         my $td = &offline_time_delta;
103         my $at = &offline_authtoken;
104         my $sk = &offline_seskey;
105
106         print_html(
107                 body => <<"             HTML");
108                         <center>
109                                 <form action='offline-upload.pl' method='post' enctype='multipart/form-data'>
110                                         <b> - Select an offline file to upload - </b><br/><br/>
111                                         <table>
112                                                 <tbody>
113                                                         <tr>
114                                                                 <td>File to Upload: </td>
115                                                                 <td><input type='file' name='file'> </input></td>
116                                                         </tr>
117                                                         <tr>
118                                                                 <td>Workstation Name: </td>
119                                                                 <td><input type='text' name='ws' value='$ws'></input></td>
120                                                         </tr>
121                                                         <tr>
122                                                                 <td>Time Delta: </td>
123                                                                 <td><input type='text' name='delta' value='$td'> </input></td>
124                                                         </tr>
125                                                         <tr>
126                                                                 <td>Session Name: (only letters, numbers, or _'s)</td>
127                                                                 <td><input type='text' name='seskey' value='$sk'> </input></td>
128                                                         </tr>
129                                                         <tr>
130                                                                 <td colspan='2' align='center'><input type='submit' name='Submit' value='Upload'> </input></td>
131                                                         </tr>
132                                                 </tbody>
133                                         </table>
134                                         <input type='hidden' name='ses' value='$at'> </input>
135                                         <input type='hidden' name='checksum' value='$cs'> </input>
136                                 </form>
137                         </center>
138                 HTML
139 }
140
141
142
143
144
145
146