MARC import: part 1 of adding support for large files
[koha.git] / tools / upload-file.pl
1 #!/usr/bin/perl -w
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 CGI::Cookie; # need to check cookies before
28                  # having CGI parse the POST request
29 use Digest::MD5;
30
31 my %cookies = fetch CGI::Cookie;
32 my $sessionID = $cookies{'CGISESSID'}->value;
33
34 my $dbh = C4::Context->dbh;
35 # FIXME get correct session -- not just mysql
36 my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
37
38 # upload-file.pl must authenticate the user
39 # before processing the POST request,
40 # and quickly bounce if the user is
41 # not authorized.  Consequently, unlike
42 # most of the other CGI scripts, upload-file.pl
43 # requires that the session cookie already
44 # have been created., $fileid, $tmp_file_name
45
46 # FIXME - add authentication based on cookie
47
48 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
49
50 # FIXME - make staging area configurable
51 my $TEMPROOT = "/tmp";
52 my $OUTPUTDIR = "$TEMPROOT/$sessionID"; 
53 mkdir $OUTPUTDIR;
54 my $tmp_file_name = "$OUTPUTDIR/$fileid";
55
56 my $fh = new IO::File $tmp_file_name, "w";
57 unless (defined $fh) {
58     # FIXME - failed to create file for some reason
59     send_reply('failed', '', '');
60     exit 0;
61 }
62 $fh->binmode(); # for Windows compatibility
63 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
64 $session->param('current_upload', $fileid);
65 $session->flush();
66
67 my $progress = 0;
68 my $first_chunk = 1;
69 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
70
71 my $query;
72 $|++;
73 $query = new CGI \&upload_hook, $session;
74 clean_up();
75 send_reply('done', $fileid, $tmp_file_name);
76
77 # FIXME - if possible, trap signal caused by user cancelling upload
78 # 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.
79 exit 0;
80
81 sub clean_up {
82     $session->param("$fileid.uploadprogress", 'done');
83     $session->flush();
84 }
85
86 sub upload_hook {
87     my ($file_name, $buffer, $bytes_read, $session) = @_;
88     print $fh $buffer;
89     # stash received file name
90     if ($first_chunk) {
91         $session->param("$fileid.uploaded_filename", $file_name);
92         $session->flush();
93         $first_chunk = 0;
94     }
95     my $percentage = int(($bytes_read / $max_size) * 100);
96     if ($percentage > $progress) {
97         $progress = $percentage;
98         $session->param("$fileid.uploadprogress", $progress);
99         $session->flush();
100     }
101 }
102
103 sub send_reply {
104     my ($upload_status, $fileid, $tmp_file_name) = @_;
105
106     my $reply = CGI->new("");
107     print $reply->header(-type => 'text/html');
108     # response will be sent back as JSON
109     print "{ status: '$upload_status', fileid: '$fileid', tmp_file_name: '$tmp_file_name' }";
110 }