Line 0
Link Here
|
0 |
- |
1 |
package Koha::FTP; |
|
|
2 |
|
3 |
# Copyright 2015 Vaara-kirjastot |
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 |
use Modern::Perl; |
21 |
|
22 |
use Net::FTP; |
23 |
use Scalar::Util qw( blessed ); |
24 |
|
25 |
use Koha::Exception::ConnectionFailed; |
26 |
use Koha::Exception::LoginFailed; |
27 |
use Koha::Exception::RemoteInvocation; |
28 |
|
29 |
=head1 FTP |
30 |
|
31 |
Koha::FTP - Wrapper to Net::FTP to make dealing with FTP-connections so much fun! |
32 |
|
33 |
=head1 SYNOPSIS |
34 |
|
35 |
sub moveAFileToAnotherFtpDirectory { |
36 |
my ($filePath, $targetDirectory, $ftpcon) = @_; |
37 |
my($fileName, $dirs, $suffix) = File::Basename::fileparse( $filePath ); |
38 |
|
39 |
try { |
40 |
|
41 |
my $ftp = Koha::FTP->new( Net::FTP->new() ); |
42 |
|
43 |
my $currentDir = $ftp->getCurrentFtpDirectory(); |
44 |
$ftp->changeFtpDirectory($targetDirectory, $ftp); |
45 |
$ftp->putFtpFile($filePath, $ftp); |
46 |
$ftp->changeFtpDirectory($currentDir, $ftp); |
47 |
$ftp->deleteFtpFile($fileName, $ftp); |
48 |
|
49 |
} catch { |
50 |
if (blessed($_)){ |
51 |
if ($_->isa('Koha::Exception::ConnectionFailed')) { |
52 |
warn $_->error()."\n"; |
53 |
} |
54 |
elsif ($_->isa('Koha::Exception::LoginFailed')) { |
55 |
warn $_->error()."\n"; |
56 |
} |
57 |
else { |
58 |
$_->rethrow(); |
59 |
} |
60 |
} |
61 |
else { |
62 |
die $_; |
63 |
} |
64 |
}; |
65 |
} |
66 |
|
67 |
=cut |
68 |
|
69 |
|
70 |
=head new |
71 |
|
72 |
my $ftp = Koha::FTP->new(); |
73 |
my $ftp = Koha::FTP->new( Net::FTP->new() ); |
74 |
|
75 |
=cut |
76 |
|
77 |
sub new { |
78 |
my ($class, $self) = @_; |
79 |
$self = {} unless ($self); |
80 |
|
81 |
if (blessed $self && $self->isa('Net::FTP')) { |
82 |
my $ftpcon = $self; |
83 |
$self = {}; |
84 |
$self->{_connection} = $ftpcon; |
85 |
} |
86 |
|
87 |
bless $self, $class; |
88 |
return $self; |
89 |
} |
90 |
|
91 |
=head connect |
92 |
STATIC METHOD! |
93 |
|
94 |
Koha::FTP::connect({ Host => 10.0.0.23, |
95 |
Passive => 0 , |
96 |
Port => 21, |
97 |
Timeout => 10, |
98 |
}, $connectionId); |
99 |
|
100 |
@PARAM1, Config HASH, |
101 |
@PARAM2, String, vendor identifier or a ftp-connection specific identifier. |
102 |
@RETURNS, Net::FTP-connection object |
103 |
@THROWS Koha::Exception::LoginFailed |
104 |
Koha::Exception::ConnectionFailed |
105 |
=cut |
106 |
|
107 |
sub connect { |
108 |
my ($config, $connectionId) = @_; |
109 |
|
110 |
my $ftpcon = Net::FTP->new(Host => $config->{host}, |
111 |
Passive => ($config->{protocol} =~ m/passive/) ? 1 : 0 , |
112 |
Port => $config->{port} || 21, |
113 |
Timeout => 10); |
114 |
unless ($ftpcon) { |
115 |
Koha::Exception::ConnectionFailed->throw( error => |
116 |
"Koha::FTP:> Connecting to '$connectionId', cannot connect to '$connectionId' ftp server: $@"); |
117 |
} |
118 |
|
119 |
if ($ftpcon->login( $config->{username}, $config->{password} )){ |
120 |
return $ftpcon; |
121 |
} |
122 |
else { |
123 |
Koha::Exception::LoginFailed->throw( error => |
124 |
"Koha::FTP:> Connecting to '$connectionId', cannot login to '$connectionId' ftp server: $@"); |
125 |
} |
126 |
return undef; |
127 |
} |
128 |
|
129 |
sub getConnection { |
130 |
my ($self) = @_; |
131 |
return $self->{_connection}; |
132 |
} |
133 |
|
134 |
sub quit { |
135 |
my ($self) = @_; |
136 |
$self->{_connection}->quit() if blessed($self->{_connection}) && $self->{_connection}->isa('Net::FTP'); |
137 |
} |
138 |
|
139 |
=head getFtpFile |
140 |
@THROWS Koha::Exception::RemoteInvocation |
141 |
=cut |
142 |
|
143 |
sub getFtpFile { |
144 |
my ($self, $directory, $filename) = @_; |
145 |
my $ftpcon = $self->getConnection(); |
146 |
|
147 |
if ($ftpcon->get($filename, $directory.$filename)) { |
148 |
return 0; #Great! no errors! |
149 |
} else { |
150 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot get '$filename' from ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
151 |
} |
152 |
} |
153 |
|
154 |
=head listFtpDirectory |
155 |
@THROWS Koha::Exception::RemoteInvocation |
156 |
=cut |
157 |
|
158 |
sub listFtpDirectory { |
159 |
my ($self) = @_; |
160 |
my $ftpcon = $self->getConnection(); |
161 |
|
162 |
if (my $ftpfiles = $ftpcon->ls()) { |
163 |
return $ftpfiles; #Great! no errors! |
164 |
} else { |
165 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot get directory listing from ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
166 |
} |
167 |
} |
168 |
|
169 |
=head putFtpFile |
170 |
@THROWS Koha::Exception::RemoteInvocation |
171 |
=cut |
172 |
|
173 |
sub putFtpFile { |
174 |
my ($self, $filePath) = @_; |
175 |
my $ftpcon = $self->getConnection(); |
176 |
|
177 |
if ($ftpcon->put($filePath)) { |
178 |
return 0; #Great! no errors! |
179 |
} else { |
180 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot put the file '$filePath' to ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
181 |
} |
182 |
} |
183 |
|
184 |
=head changeFtpDirectory |
185 |
@THROWS Koha::Exception::RemoteInvocation |
186 |
=cut |
187 |
|
188 |
sub changeFtpDirectory { |
189 |
my ($self, $directory) = @_; |
190 |
my $ftpcon = $self->getConnection(); |
191 |
|
192 |
if ($ftpcon->cwd($directory) ) { |
193 |
return 0; #Great! no errors! |
194 |
} else { |
195 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot change to the remote directory '$directory' in ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
196 |
} |
197 |
} |
198 |
|
199 |
=head getCurrentFtpDirectory |
200 |
@THROWS Koha::Exception::RemoteInvocation |
201 |
=cut |
202 |
|
203 |
sub getCurrentFtpDirectory { |
204 |
my ($self) = @_; |
205 |
my $ftpcon = $self->getConnection(); |
206 |
|
207 |
if (my $cwd = $ftpcon->pwd() ) { |
208 |
return $cwd; #Great! no errors! |
209 |
} else { |
210 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot get the remote working directory in ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
211 |
} |
212 |
} |
213 |
|
214 |
=head deleteFtpFile |
215 |
@THROWS Koha::Exception::RemoteInvocation |
216 |
=cut |
217 |
|
218 |
sub deleteFtpFile { |
219 |
my ($self, $filename) = @_; |
220 |
my $ftpcon = $self->getConnection(); |
221 |
|
222 |
if ($ftpcon->delete($filename)) { |
223 |
return 0; #Great! no errors! |
224 |
} else { |
225 |
Koha::Exception::RemoteInvocation->throw(error => "Koha::FTP:> Cannot delete the file '$filename' from ftp server '".$self->{connectionId}."': ".$ftpcon->message); |
226 |
} |
227 |
} |
228 |
|
229 |
1; #Happy nice joy fun! |