Lines 32-38
use Net::SFTP::Foreign;
Link Here
|
32 |
|
32 |
|
33 |
use Koha::Database; |
33 |
use Koha::Database; |
34 |
use Koha::DateUtils qw( dt_from_string ); |
34 |
use Koha::DateUtils qw( dt_from_string ); |
35 |
use Koha::Encryption; |
35 |
use Koha::SFTP::Servers; |
36 |
|
36 |
|
37 |
sub new { |
37 |
sub new { |
38 |
my ( $class, $account_id ) = @_; |
38 |
my ( $class, $account_id ) = @_; |
Lines 62-94
sub download_messages {
Link Here
|
62 |
my ( $self, $message_type ) = @_; |
62 |
my ( $self, $message_type ) = @_; |
63 |
$self->{message_type} = $message_type; |
63 |
$self->{message_type} = $message_type; |
64 |
|
64 |
|
|
|
65 |
my $sftp_server_id = $self->{account}->download_sftp_server_id; |
66 |
my $sftp_server_transport; |
67 |
$sftp_server_transport = Koha::SFTP::Servers->find($sftp_server_id)->transport |
68 |
if ( $sftp_server_id ); |
69 |
|
65 |
my @retrieved_files; |
70 |
my @retrieved_files; |
66 |
|
71 |
|
67 |
if ( $self->{account}->transport eq 'SFTP' ) { |
72 |
if ($message_type) { |
68 |
@retrieved_files = $self->sftp_download(); |
73 |
if ( !$sftp_server_transport ) { |
69 |
} |
74 |
@retrieved_files = $self->file_download(); |
70 |
elsif ( $self->{account}->transport eq 'FILE' ) { |
75 |
} elsif ( $sftp_server_transport eq 'sftp' ) { |
71 |
@retrieved_files = $self->file_download(); |
76 |
@retrieved_files = $self->sftp_download(); |
72 |
} |
77 |
} elsif ( $sftp_server_transport eq 'ftp' ) { |
73 |
else { # assume FTP |
78 |
@retrieved_files = $self->ftp_download(); |
74 |
@retrieved_files = $self->ftp_download(); |
79 |
} |
75 |
} |
80 |
} |
|
|
81 |
|
76 |
return @retrieved_files; |
82 |
return @retrieved_files; |
77 |
} |
83 |
} |
78 |
|
84 |
|
79 |
sub upload_messages { |
85 |
sub upload_messages { |
80 |
my ( $self, @messages ) = @_; |
86 |
my ( $self, @messages ) = @_; |
|
|
87 |
|
88 |
my $sftp_server_id = $self->{account}->upload_sftp_server_id; |
89 |
my $sftp_server_transport; |
90 |
$sftp_server_transport = Koha::SFTP::Servers->find($sftp_server_id)->transport |
91 |
if ( $sftp_server_id ); |
92 |
|
81 |
if (@messages) { |
93 |
if (@messages) { |
82 |
if ( $self->{account}->transport eq 'SFTP' ) { |
94 |
if ( !$sftp_server_transport ) { |
|
|
95 |
$self->ftp_upload(@messages); |
96 |
} elsif ( $sftp_server_transport eq 'sftp' ) { |
83 |
$self->sftp_upload(@messages); |
97 |
$self->sftp_upload(@messages); |
84 |
} |
98 |
} elsif ( $sftp_server_transport eq 'ftp' ) { |
85 |
elsif ( $self->{account}->transport eq 'FILE' ) { |
|
|
86 |
$self->file_upload(@messages); |
99 |
$self->file_upload(@messages); |
87 |
} |
100 |
} |
88 |
else { # assume FTP |
|
|
89 |
$self->ftp_upload(@messages); |
90 |
} |
91 |
} |
101 |
} |
|
|
102 |
|
92 |
return; |
103 |
return; |
93 |
} |
104 |
} |
94 |
|
105 |
|
Lines 131-147
sub file_download {
Link Here
|
131 |
sub sftp_download { |
142 |
sub sftp_download { |
132 |
my $self = shift; |
143 |
my $self = shift; |
133 |
|
144 |
|
|
|
145 |
my $sftp_server_id = $self->{account}->download_sftp_server_id; |
146 |
return |
147 |
unless $sftp_server_id; |
148 |
my $sftp_server = Koha::SFTP::Servers->find($sftp_server_id); |
149 |
return |
150 |
unless $sftp_server; |
151 |
|
134 |
my $file_ext = _get_file_ext( $self->{message_type} ); |
152 |
my $file_ext = _get_file_ext( $self->{message_type} ); |
135 |
|
153 |
|
136 |
# C = ready to retrieve E = Edifact |
154 |
# C = ready to retrieve E = Edifact |
137 |
my $msg_hash = $self->message_hash(); |
155 |
my $msg_hash = $self->message_hash(); |
138 |
my @downloaded_files; |
156 |
my @downloaded_files; |
139 |
my $port = $self->{account}->download_port ? $self->{account}->download_port : '22'; |
|
|
140 |
my $sftp = Net::SFTP::Foreign->new( |
157 |
my $sftp = Net::SFTP::Foreign->new( |
141 |
host => $self->{account}->host, |
158 |
host => $sftp_server->host, |
142 |
user => $self->{account}->username, |
159 |
user => $sftp_server->user_name, |
143 |
password => Koha::Encryption->new->decrypt_hex($self->{account}->password), |
160 |
password => $sftp_server->plain_text_password, |
144 |
port => $port, |
161 |
port => $sftp_server->port, |
145 |
timeout => 10, |
162 |
timeout => 10, |
146 |
); |
163 |
); |
147 |
if ( $sftp->error ) { |
164 |
if ( $sftp->error ) { |
Lines 213-236
sub ingest {
Link Here
|
213 |
sub ftp_download { |
230 |
sub ftp_download { |
214 |
my $self = shift; |
231 |
my $self = shift; |
215 |
|
232 |
|
|
|
233 |
my $sftp_server_id = $self->{account}->download_sftp_server_id; |
234 |
return |
235 |
unless $sftp_server_id; |
236 |
my $sftp_server = Koha::SFTP::Servers->find($sftp_server_id); |
237 |
return |
238 |
unless $sftp_server; |
239 |
|
216 |
my $file_ext = _get_file_ext( $self->{message_type} ); |
240 |
my $file_ext = _get_file_ext( $self->{message_type} ); |
217 |
|
241 |
|
218 |
# C = ready to retrieve E = Edifact |
242 |
# C = ready to retrieve E = Edifact |
219 |
|
243 |
|
220 |
my $msg_hash = $self->message_hash(); |
244 |
my $msg_hash = $self->message_hash(); |
221 |
my @downloaded_files; |
245 |
my @downloaded_files; |
222 |
my $port = $self->{account}->download_port ? $self->{account}->download_port : '21'; |
|
|
223 |
my $ftp = Net::FTP->new( |
246 |
my $ftp = Net::FTP->new( |
224 |
$self->{account}->host, |
247 |
$sftp_server->host, |
225 |
Port => $port, |
248 |
Port => $sftp_server->port, |
226 |
Timeout => 10, |
249 |
Timeout => 10, |
227 |
Passive => 1 |
250 |
Passive => $sftp_server->passiv, |
228 |
) |
251 |
) |
229 |
or return $self->_abort_download( |
252 |
or return $self->_abort_download( |
230 |
undef, |
253 |
undef, |
231 |
"Cannot connect to $self->{account}->host: $EVAL_ERROR" |
254 |
"Cannot connect to " . $sftp_server->host . ": " . $EVAL_ERROR |
232 |
); |
255 |
); |
233 |
$ftp->login( $self->{account}->username, Koha::Encryption->new->decrypt_hex($self->{account}->password) ) |
256 |
$ftp->login( $sftp_server->user_name, $sftp_server->plain_text_password ) |
234 |
or return $self->_abort_download( $ftp, "Cannot login: $ftp->message()" ); |
257 |
or return $self->_abort_download( $ftp, "Cannot login: $ftp->message()" ); |
235 |
$ftp->cwd( $self->{account}->download_directory ) |
258 |
$ftp->cwd( $self->{account}->download_directory ) |
236 |
or return $self->_abort_download( $ftp, |
259 |
or return $self->_abort_download( $ftp, |
Lines 264-281
sub ftp_download {
Link Here
|
264 |
|
287 |
|
265 |
sub ftp_upload { |
288 |
sub ftp_upload { |
266 |
my ( $self, @messages ) = @_; |
289 |
my ( $self, @messages ) = @_; |
267 |
my $port = $self->{account}->upload_port ? $self->{account}->upload_port : '21'; |
290 |
|
|
|
291 |
my $sftp_server_id = $self->{account}->upload_sftp_server_id; |
292 |
return |
293 |
unless $sftp_server_id; |
294 |
my $sftp_server = Koha::SFTP::Servers->find($sftp_server_id); |
295 |
return |
296 |
unless $sftp_server; |
297 |
|
268 |
my $ftp = Net::FTP->new( |
298 |
my $ftp = Net::FTP->new( |
269 |
$self->{account}->host, |
299 |
$sftp_server->host, |
270 |
Port => $port, |
300 |
Port => $sftp_server->port, |
271 |
Timeout => 10, |
301 |
Timeout => 10, |
272 |
Passive => 1 |
302 |
Passive => $sftp_server->passiv |
273 |
) |
303 |
) |
274 |
or return $self->_abort_download( |
304 |
or return $self->_abort_download( |
275 |
undef, |
305 |
undef, |
276 |
"Cannot connect to $self->{account}->host: $EVAL_ERROR" |
306 |
"Cannot connect to " . $sftp_server->host . ": " . $EVAL_ERROR |
277 |
); |
307 |
); |
278 |
$ftp->login( $self->{account}->username, Koha::Encryption->new->decrypt_hex($self->{account}->password) ) |
308 |
$ftp->login( $sftp_server->user_name, $sftp_server->plain_text_password ) |
279 |
or return $self->_abort_download( $ftp, "Cannot login: $ftp->message()" ); |
309 |
or return $self->_abort_download( $ftp, "Cannot login: $ftp->message()" ); |
280 |
$ftp->cwd( $self->{account}->upload_directory ) |
310 |
$ftp->cwd( $self->{account}->upload_directory ) |
281 |
or return $self->_abort_download( $ftp, |
311 |
or return $self->_abort_download( $ftp, |
Lines 303-317
sub ftp_upload {
Link Here
|
303 |
|
333 |
|
304 |
sub sftp_upload { |
334 |
sub sftp_upload { |
305 |
my ( $self, @messages ) = @_; |
335 |
my ( $self, @messages ) = @_; |
306 |
my $port = $self->{account}->upload_port ? $self->{account}->upload_port : '22'; |
336 |
|
|
|
337 |
my $sftp_server_id = $self->{account}->upload_sftp_server_id; |
338 |
return |
339 |
unless $sftp_server_id; |
340 |
my $sftp_server = Koha::SFTP::Servers->find($sftp_server_id); |
341 |
return |
342 |
unless $sftp_server; |
343 |
|
307 |
my $sftp = Net::SFTP::Foreign->new( |
344 |
my $sftp = Net::SFTP::Foreign->new( |
308 |
host => $self->{account}->host, |
345 |
host => $sftp_server->host, |
309 |
user => $self->{account}->username, |
346 |
user => $sftp_server->user_name, |
310 |
password => Koha::Encryption->new->decrypt_hex($self->{account}->password), |
347 |
password => $sftp_server->plain_text_password, |
311 |
port => $port, |
348 |
port => $sftp_server->port, |
312 |
timeout => 10, |
349 |
timeout => 10, |
313 |
); |
350 |
); |
314 |
$sftp->die_on_error("Cannot ssh to $self->{account}->host"); |
351 |
$sftp->die_on_error("Cannot ssh to " . $sftp_server->host); |
315 |
$sftp->setcwd( $self->{account}->upload_directory ) |
352 |
$sftp->setcwd( $self->{account}->upload_directory ) |
316 |
or return $self->_abort_download( $sftp, |
353 |
or return $self->_abort_download( $sftp, |
317 |
"Cannot change remote dir : " . $sftp->error ); |
354 |
"Cannot change remote dir : " . $sftp->error ); |