new C4::Auth::get_session for single place to get CGI::Session object
[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 C4::Auth qw/get_session/;
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 $sessionID = $cookies{'CGISESSID'}->value;
34
35 my $session = get_session($sessionID);
36
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
44
45 # FIXME - add authentication based on cookie
46
47 my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
48
49 # FIXME - make staging area configurable
50 my $TEMPROOT = "/tmp";
51 my $OUTPUTDIR = "$TEMPROOT/$sessionID"; 
52 mkdir $OUTPUTDIR;
53 my $tmp_file_name = "$OUTPUTDIR/$fileid";
54
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', '', '');
59     exit 0;
60 }
61 $fh->binmode(); # for Windows compatibility
62 $session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
63 $session->param('current_upload', $fileid);
64 $session->flush();
65
66 my $progress = 0;
67 my $first_chunk = 1;
68 my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
69
70 my $query;
71 $|++;
72 $query = new CGI \&upload_hook, $session;
73 clean_up();
74 send_reply('done', $fileid, $tmp_file_name);
75
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.
78 exit 0;
79
80 sub clean_up {
81     $session->param("$fileid.uploadprogress", 'done');
82     $session->flush();
83 }
84
85 sub upload_hook {
86     my ($file_name, $buffer, $bytes_read, $session) = @_;
87     print $fh $buffer;
88     # stash received file name
89     if ($first_chunk) {
90         $session->param("$fileid.uploaded_filename", $file_name);
91         $session->flush();
92         $first_chunk = 0;
93     }
94     my $percentage = int(($bytes_read / $max_size) * 100);
95     if ($percentage > $progress) {
96         $progress = $percentage;
97         $session->param("$fileid.uploadprogress", $progress);
98         $session->flush();
99     }
100 }
101
102 sub send_reply {
103     my ($upload_status, $fileid, $tmp_file_name) = @_;
104
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' }";
109 }