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 C4::Auth qw/get_session check_cookie_auth/;
28 use CGI::Cookie; # need to check cookies before
29 # having CGI parse the POST request
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, "", "");
40 my $session = get_session($sessionID);
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
50 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
52 # FIXME - make staging area configurable
53 my $TEMPROOT = "/tmp";
54 my $OUTPUTDIR = "$TEMPROOT/$sessionID";
56 my $tmp_file_name = "$OUTPUTDIR/$fileid";
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', '', '');
64 $fh->binmode(); # for Windows compatibility
65 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
66 $session->param('current_upload', $fileid);
71 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
75 $query = new CGI \&upload_hook, $session;
77 send_reply('done', $fileid, $tmp_file_name);
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.
84 $session->param("$fileid.uploadprogress", 'done');
89 my ($file_name, $buffer, $bytes_read, $session) = @_;
91 # stash received file name
93 $session->param("$fileid.uploaded_filename", $file_name);
97 my $percentage = int(($bytes_read / $max_size) * 100);
98 if ($percentage > $progress) {
99 $progress = $percentage;
100 $session->param("$fileid.uploadprogress", $progress);
106 my ($upload_status, $fileid, $tmp_file_name) = @_;
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' }";