Line 0
Link Here
|
|
|
1 |
package C4::UploadedFiles; |
2 |
|
3 |
# Copyright 2011-2012 BibLibre |
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 |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
C4::UploadedFiles - Functions to deal with files uploaded with cataloging plugin upload.pl |
23 |
|
24 |
=head1 SYNOPSIS |
25 |
|
26 |
use C4::UploadedFiles; |
27 |
|
28 |
my $filename = $cgi->param('uploaded_file'); |
29 |
my $file = $cgi->upload('uploaded_file'); |
30 |
my $dir = $input->param('dir'); |
31 |
|
32 |
# upload file |
33 |
my $id = C4::UploadedFiles::UploadFile($filename, $dir, $file->handle); |
34 |
|
35 |
# retrieve file infos |
36 |
my $uploaded_file = C4::UploadedFiles::GetUploadedFile($id); |
37 |
|
38 |
# delete file |
39 |
C4::UploadedFiles::DelUploadedFile($id); |
40 |
|
41 |
=head1 DESCRIPTION |
42 |
|
43 |
This module provides basic functions for adding, retrieving and deleting files related to |
44 |
cataloging plugin upload.pl. |
45 |
|
46 |
It uses uploaded_files table. |
47 |
|
48 |
It is not related to C4::UploadedFile |
49 |
|
50 |
=head1 FUNCTIONS |
51 |
|
52 |
=cut |
53 |
|
54 |
use Modern::Perl; |
55 |
use Digest::SHA; |
56 |
use Fcntl; |
57 |
|
58 |
use C4::Context; |
59 |
|
60 |
=head2 GetUploadedFile |
61 |
|
62 |
my $file = C4::UploadedFiles::GetUploadedFile($id); |
63 |
|
64 |
Returns a hashref containing infos on uploaded files. |
65 |
Hash keys are: |
66 |
|
67 |
=over 2 |
68 |
|
69 |
=item * id: id of the file (same as given in argument) |
70 |
|
71 |
=item * filename: name of the file |
72 |
|
73 |
=item * dir: directory where file is stored (relative to syspref 'uploadPath') |
74 |
|
75 |
=back |
76 |
|
77 |
It returns undef if file is not found |
78 |
|
79 |
=cut |
80 |
|
81 |
sub GetUploadedFile { |
82 |
my ($id) = @_; |
83 |
|
84 |
return unless $id; |
85 |
|
86 |
my $dbh = C4::Context->dbh; |
87 |
my $query = qq{ |
88 |
SELECT id, filename, dir |
89 |
FROM uploaded_files |
90 |
WHERE id = ? |
91 |
}; |
92 |
my $sth = $dbh->prepare($query); |
93 |
$sth->execute($id); |
94 |
my $file = $sth->fetchrow_hashref; |
95 |
|
96 |
return $file; |
97 |
} |
98 |
|
99 |
=head2 UploadFile |
100 |
|
101 |
my $id = C4::UploadedFiles::UploadFile($filename, $dir, $io_handle); |
102 |
|
103 |
Upload a new file and returns its id (its SHA-1 sum, actually). |
104 |
|
105 |
Parameters: |
106 |
|
107 |
=over 2 |
108 |
|
109 |
=item * $filename: name of the file |
110 |
|
111 |
=item * $dir: directory where to store the file (path relative to syspref 'uploadPath' |
112 |
|
113 |
=item * $io_handle: valid IO::Handle object, can be retrieved with |
114 |
$cgi->upload('uploaded_file')->handle; |
115 |
|
116 |
=back |
117 |
|
118 |
=cut |
119 |
|
120 |
sub UploadFile { |
121 |
my ($filename, $dir, $handle) = @_; |
122 |
|
123 |
if($filename =~ m#/(^|/)\.\.(/|$)# or $dir =~ m#(^|/)\.\.(/|$)#) { |
124 |
warn "Filename or dirname contains '..'. Aborting upload"; |
125 |
return; |
126 |
} |
127 |
|
128 |
my $buffer; |
129 |
my $data = ''; |
130 |
while($handle->read($buffer, 1024)) { |
131 |
$data .= $buffer; |
132 |
} |
133 |
$handle->close; |
134 |
|
135 |
my $sha = new Digest::SHA; |
136 |
$sha->add($data); |
137 |
my $id = $sha->hexdigest; |
138 |
|
139 |
# Test if this id already exist |
140 |
my $file = GetUploadedFile($id); |
141 |
if($file) { |
142 |
return $file->{id}; |
143 |
} |
144 |
|
145 |
my $upload_path = C4::Context->preference("uploadPath"); |
146 |
my $file_path = "$upload_path/$dir/$filename"; |
147 |
$file_path =~ s#/+#/#; |
148 |
|
149 |
my $out_fh; |
150 |
# Create the file only if it doesn't exist |
151 |
unless( sysopen($out_fh, $file_path, O_WRONLY|O_CREAT|O_EXCL) ) { |
152 |
warn "Failed to open file '$file_path': $!"; |
153 |
return; |
154 |
} |
155 |
|
156 |
print $out_fh $data; |
157 |
close $out_fh; |
158 |
|
159 |
my $dbh = C4::Context->dbh; |
160 |
my $query = qq{ |
161 |
INSERT INTO uploaded_files (id, filename, dir) |
162 |
VALUES (?,?, ?); |
163 |
}; |
164 |
my $sth = $dbh->prepare($query); |
165 |
if($sth->execute($id, $filename, $dir)) { |
166 |
return $id; |
167 |
} |
168 |
|
169 |
return undef; |
170 |
} |
171 |
|
172 |
=head2 DelUploadedFile |
173 |
|
174 |
C4::UploadedFiles::DelUploadedFile($id); |
175 |
|
176 |
Remove a previously uploaded file, given its id. |
177 |
|
178 |
Returns a false value if an error occurs. |
179 |
|
180 |
=cut |
181 |
|
182 |
sub DelUploadedFile { |
183 |
my ($id) = @_; |
184 |
|
185 |
my $file = GetUploadedFile($id); |
186 |
if($file) { |
187 |
my $upload_path = C4::Context->preference("uploadPath"); |
188 |
my $file_path = "$upload_path/$file->{dir}/$file->{filename}"; |
189 |
$file_path =~ s#/+#/#; |
190 |
my $file_deleted = 0; |
191 |
unless( -f $file_path ) { |
192 |
warn "Id $file->{id} is in database but not in filesystem, removing id from database"; |
193 |
$file_deleted = 1; |
194 |
} else { |
195 |
if(unlink $file_path) { |
196 |
$file_deleted = 1; |
197 |
} |
198 |
} |
199 |
|
200 |
unless($file_deleted) { |
201 |
warn "File $file_path cannot be deleted: $!"; |
202 |
} |
203 |
|
204 |
my $dbh = C4::Context->dbh; |
205 |
my $query = qq{ |
206 |
DELETE FROM uploaded_files |
207 |
WHERE id = ? |
208 |
}; |
209 |
my $sth = $dbh->prepare($query); |
210 |
return $sth->execute($id); |
211 |
} |
212 |
} |
213 |
|
214 |
1; |