MARC import: part 2 of large file support
[koha.git] / tools / upload-file.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21
22 # standard or CPAN modules used
23 use IO::File;
24 use CGI;
25 use CGI::Session;
26 use C4::Context;
27 use C4::Auth qw/get_session check_cookie_auth/;
28 use CGI::Cookie; # need to check cookies before
29                  # having CGI parse the POST request
30 use Digest::MD5;
31
32 my %cookies = fetch CGI::Cookie;
33 my ($auth_status, $sessionID) = check_cookie_auth($cookies{'CGISESSID'}->value, { tools => 1 });
34 if ($auth_status ne "ok") {
35     $auth_status = 'denied' if $auth_status eq 'failed';
36     send_reply($auth_status, "", "");
37     exit 0;
38 }
39
40 my $session = get_session($sessionID);
41
42 # upload-file.pl must authenticate the user
43 # before processing the POST request,
44 # and quickly bounce if the user is
45 # not authorized.  Consequently, unlike
46 # most of the other CGI scripts, upload-file.pl
47 # requires that the session cookie already
48 # have been created., $fileid, $tmp_file_name
49
50 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
51
52 # FIXME - make staging area configurable
53 my $TEMPROOT = "/tmp";
54 my $OUTPUTDIR = "$TEMPROOT/$sessionID"; 
55 mkdir $OUTPUTDIR;
56 my $tmp_file_name = "$OUTPUTDIR/$fileid";
57
58 my $fh = new IO::File $tmp_file_name, "w";
59 unless (defined $fh) {
60     # FIXME - failed to create file for some reason
61     send_reply('failed', '', '');
62     exit 0;
63 }
64 $fh->binmode(); # for Windows compatibility
65 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
66 $session->param('current_upload', $fileid);
67 $session->flush();
68
69 my $progress = 0;
70 my $first_chunk = 1;
71 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
72
73 my $query;
74 $|++;
75 $query = new CGI \&upload_hook, $session;
76 clean_up();
77 send_reply('done', $fileid, $tmp_file_name);
78
79 # FIXME - if possible, trap signal caused by user cancelling upload
80 # FIXME - something is wrong during cleanup: \t(in cleanup) Can't call method "commit" on unblessed reference at /usr/local/share/perl/5.8.8/CGI/Session/Driver/DBI.pm line 130 during global destruction.
81 exit 0;
82
83 sub clean_up {
84     $session->param("$fileid.uploadprogress", 'done');
85     $session->flush();
86 }
87
88 sub upload_hook {
89     my ($file_name, $buffer, $bytes_read, $session) = @_;
90     print $fh $buffer;
91     # stash received file name
92     if ($first_chunk) {
93         $session->param("$fileid.uploaded_filename", $file_name);
94         $session->flush();
95         $first_chunk = 0;
96     }
97     my $percentage = int(($bytes_read / $max_size) * 100);
98     if ($percentage > $progress) {
99         $progress = $percentage;
100         $session->param("$fileid.uploadprogress", $progress);
101         $session->flush();
102     }
103 }
104
105 sub send_reply {
106     my ($upload_status, $fileid, $tmp_file_name) = @_;
107
108     my $reply = CGI->new("");
109     print $reply->header(-type => 'text/html');
110     # response will be sent back as JSON
111     print "{ status: '$upload_status', fileid: '$fileid', tmp_file_name: '$tmp_file_name' }";
112 }