3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
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
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.
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
22 # standard or CPAN modules used
27 use CGI::Cookie; # need to check cookies before
28 # having CGI parse the POST request
31 my %cookies = fetch CGI::Cookie;
32 my $sessionID = $cookies{'CGISESSID'}->value;
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});
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
46 # FIXME - add authentication based on cookie
48 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
50 # FIXME - make staging area configurable
51 my $TEMPROOT = "/tmp";
52 my $OUTPUTDIR = "$TEMPROOT/$sessionID";
54 my $tmp_file_name = "$OUTPUTDIR/$fileid";
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', '', '');
62 $fh->binmode(); # for Windows compatibility
63 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
64 $session->param('current_upload', $fileid);
69 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
73 $query = new CGI \&upload_hook, $session;
75 send_reply('done', $fileid, $tmp_file_name);
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.
82 $session->param("$fileid.uploadprogress", 'done');
87 my ($file_name, $buffer, $bytes_read, $session) = @_;
89 # stash received file name
91 $session->param("$fileid.uploaded_filename", $file_name);
95 my $percentage = int(($bytes_read / $max_size) * 100);
96 if ($percentage > $progress) {
97 $progress = $percentage;
98 $session->param("$fileid.uploadprogress", $progress);
104 my ($upload_status, $fileid, $tmp_file_name) = @_;
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' }";