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 |
|
57 |
use C4::Context; |
58 |
|
59 |
=head2 GetUploadedFile |
60 |
|
61 |
my $file = C4::UploadedFiles::GetUploadedFile($id); |
62 |
|
63 |
Returns a hashref containing infos on uploaded files. |
64 |
Hash keys are: |
65 |
|
66 |
=over 2 |
67 |
|
68 |
=item * id: id of the file (same as given in argument) |
69 |
|
70 |
=item * filename: name of the file |
71 |
|
72 |
=item * dir: directory where file is stored (relative to syspref 'uploadPath') |
73 |
|
74 |
=back |
75 |
|
76 |
It returns undef if file is not found |
77 |
|
78 |
=cut |
79 |
|
80 |
sub GetUploadedFile { |
81 |
my ($id) = @_; |
82 |
|
83 |
return unless $id; |
84 |
|
85 |
my $dbh = C4::Context->dbh; |
86 |
my $query = qq{ |
87 |
SELECT id, filename, dir |
88 |
FROM uploaded_files |
89 |
WHERE id = ? |
90 |
}; |
91 |
my $sth = $dbh->prepare($query); |
92 |
$sth->execute($id); |
93 |
my $file = $sth->fetchrow_hashref; |
94 |
|
95 |
return $file; |
96 |
} |
97 |
|
98 |
=head2 UploadFile |
99 |
|
100 |
my $id = C4::UploadedFiles::UploadFile($filename, $dir, $io_handle); |
101 |
|
102 |
Upload a new file and returns its id (its SHA-1 sum, actually). |
103 |
|
104 |
Parameters: |
105 |
|
106 |
=over 2 |
107 |
|
108 |
=item * $filename: name of the file |
109 |
|
110 |
=item * $dir: directory where to store the file (path relative to syspref 'uploadPath' |
111 |
|
112 |
=item * $io_handle: valid IO::Handle object, can be retrieved with |
113 |
$cgi->upload('uploaded_file')->handle; |
114 |
|
115 |
=back |
116 |
|
117 |
=cut |
118 |
|
119 |
sub UploadFile { |
120 |
my ($filename, $dir, $handle) = @_; |
121 |
|
122 |
if($filename =~ m#/(^|/)\.\.(/|$)# or $dir =~ m#(^|/)\.\.(/|$)#) { |
123 |
warn "Filename or dirname contains '..'. Aborting upload"; |
124 |
return; |
125 |
} |
126 |
|
127 |
my $buffer; |
128 |
my $data = ''; |
129 |
while($handle->read($buffer, 1024)) { |
130 |
$data .= $buffer; |
131 |
} |
132 |
$handle->close; |
133 |
|
134 |
my $sha = new Digest::SHA; |
135 |
$sha->add($data); |
136 |
my $id = $sha->hexdigest; |
137 |
|
138 |
# Test if this id already exist |
139 |
my $file = GetUploadedFile($id); |
140 |
if($file) { |
141 |
return $file->{id}; |
142 |
} |
143 |
|
144 |
my $upload_path = C4::Context->preference("uploadPath"); |
145 |
my $file_path = "$upload_path/$dir/$filename"; |
146 |
$file_path =~ s#/+#/#; |
147 |
if( -f $file_path ) { |
148 |
warn "Id $id not in database, but present in filesystem, do nothing"; |
149 |
return; |
150 |
} |
151 |
|
152 |
my $out_fh; |
153 |
unless(open $out_fh, '>', $file_path) { |
154 |
warn "Failed to open file '$file_path': $!"; |
155 |
return; |
156 |
} |
157 |
|
158 |
print $out_fh $data; |
159 |
close $out_fh; |
160 |
|
161 |
my $dbh = C4::Context->dbh; |
162 |
my $query = qq{ |
163 |
INSERT INTO uploaded_files (id, filename, dir) |
164 |
VALUES (?,?, ?); |
165 |
}; |
166 |
my $sth = $dbh->prepare($query); |
167 |
if($sth->execute($id, $filename, $dir)) { |
168 |
return $id; |
169 |
} |
170 |
|
171 |
return undef; |
172 |
} |
173 |
|
174 |
=head2 DelUploadedFile |
175 |
|
176 |
C4::UploadedFiles::DelUploadedFile($id); |
177 |
|
178 |
Remove a previously uploaded file, given its id. |
179 |
|
180 |
Returns a false value if an error occurs. |
181 |
|
182 |
=cut |
183 |
|
184 |
sub DelUploadedFile { |
185 |
my ($id) = @_; |
186 |
|
187 |
my $file = GetUploadedFile($id); |
188 |
if($file) { |
189 |
my $upload_path = C4::Context->preference("uploadPath"); |
190 |
my $file_path = "$upload_path/$file->{dir}/$file->{filename}"; |
191 |
$file_path =~ s#/+#/#; |
192 |
my $file_deleted = 0; |
193 |
unless( -f $file_path ) { |
194 |
warn "Id $file->{id} is in database but not in filesystem, removing id from database"; |
195 |
$file_deleted = 1; |
196 |
} else { |
197 |
if(unlink $file_path) { |
198 |
$file_deleted = 1; |
199 |
} |
200 |
} |
201 |
|
202 |
unless($file_deleted) { |
203 |
warn "File $file_path cannot be deleted: $!"; |
204 |
} |
205 |
|
206 |
my $dbh = C4::Context->dbh; |
207 |
my $query = qq{ |
208 |
DELETE FROM uploaded_files |
209 |
WHERE id = ? |
210 |
}; |
211 |
my $sth = $dbh->prepare($query); |
212 |
return $sth->execute($id); |
213 |
} |
214 |
} |
215 |
|
216 |
1; |