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/;
28 use CGI::Cookie; # need to check cookies before
29 # having CGI parse the POST request
32 my %cookies = fetch CGI::Cookie;
33 my $sessionID = $cookies{'CGISESSID'}->value;
35 my $session = get_session($sessionID);
37 # upload-file.pl must authenticate the user
38 # before processing the POST request,
39 # and quickly bounce if the user is
40 # not authorized. Consequently, unlike
41 # most of the other CGI scripts, upload-file.pl
42 # requires that the session cookie already
43 # have been created., $fileid, $tmp_file_name
45 # FIXME - add authentication based on cookie
47 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
49 # FIXME - make staging area configurable
50 my $TEMPROOT = "/tmp";
51 my $OUTPUTDIR = "$TEMPROOT/$sessionID";
53 my $tmp_file_name = "$OUTPUTDIR/$fileid";
55 my $fh = new IO::File $tmp_file_name, "w";
56 unless (defined $fh) {
57 # FIXME - failed to create file for some reason
58 send_reply('failed', '', '');
61 $fh->binmode(); # for Windows compatibility
62 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
63 $session->param('current_upload', $fileid);
68 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
72 $query = new CGI \&upload_hook, $session;
74 send_reply('done', $fileid, $tmp_file_name);
76 # FIXME - if possible, trap signal caused by user cancelling upload
77 # 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 $session->param("$fileid.uploadprogress", 'done');
86 my ($file_name, $buffer, $bytes_read, $session) = @_;
88 # stash received file name
90 $session->param("$fileid.uploaded_filename", $file_name);
94 my $percentage = int(($bytes_read / $max_size) * 100);
95 if ($percentage > $progress) {
96 $progress = $percentage;
97 $session->param("$fileid.uploadprogress", $progress);
103 my ($upload_status, $fileid, $tmp_file_name) = @_;
105 my $reply = CGI->new("");
106 print $reply->header(-type => 'text/html');
107 # response will be sent back as JSON
108 print "{ status: '$upload_status', fileid: '$fileid', tmp_file_name: '$tmp_file_name' }";