line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Siffra::Transfers; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
53144
|
use 5.014; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
6
|
1
|
|
|
1
|
|
3
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
7
|
1
|
|
|
1
|
|
470
|
use utf8; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
488
|
use Data::Dumper; |
|
1
|
|
|
|
|
5396
|
|
|
1
|
|
|
|
|
50
|
|
9
|
1
|
|
|
1
|
|
349
|
use DDP; |
|
1
|
|
|
|
|
34203
|
|
|
1
|
|
|
|
|
10
|
|
10
|
1
|
|
|
1
|
|
521
|
use Log::Any qw($log); |
|
1
|
|
|
|
|
8627
|
|
|
1
|
|
|
|
|
6
|
|
11
|
1
|
|
|
1
|
|
2090
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
91
|
|
12
|
|
|
|
|
|
|
$Carp::Verbose = 1; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$| = 1; #autoflush |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use constant { |
17
|
|
|
|
|
|
|
FALSE => 0, |
18
|
|
|
|
|
|
|
TRUE => 1, |
19
|
1
|
|
50
|
|
|
110
|
DEBUG => $ENV{ DEBUG } // 0, |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
FILE_ALREADY_DOWNLOADED => -1, |
22
|
1
|
|
|
1
|
|
16
|
}; |
|
1
|
|
|
|
|
2
|
|
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
1
|
|
546
|
use Term::ProgressBar; |
|
1
|
|
|
|
|
64087
|
|
|
1
|
|
|
|
|
66
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
BEGIN |
27
|
|
|
|
|
|
|
{ |
28
|
1
|
|
|
1
|
|
20
|
binmode( STDOUT, ":encoding(UTF-8)" ); |
|
1
|
|
|
1
|
|
5
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
10
|
|
29
|
1
|
|
|
|
|
13757
|
binmode( STDERR, ":encoding(UTF-8)" ); |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
430
|
require Siffra::Tools; |
32
|
1
|
|
|
1
|
|
10
|
use Exporter (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
33
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
115
|
|
34
|
1
|
|
|
|
|
5668
|
$VERSION = '0.08'; |
35
|
1
|
|
|
|
|
22
|
@ISA = qw(Siffra::Tools Exporter); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#Give a hoot don't pollute, do not export more than needed by default |
38
|
1
|
|
|
|
|
4
|
@EXPORT = qw(); |
39
|
1
|
|
|
|
|
3
|
@EXPORT_OK = qw(); |
40
|
1
|
|
|
|
|
3553
|
%EXPORT_TAGS = (); |
41
|
|
|
|
|
|
|
} ## end BEGIN |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# TODO - Verificar o diretorio de Download para se não existir criar. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 C<new()> |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Usage : $self->block_new_method() within text_pm_file() |
48
|
|
|
|
|
|
|
Purpose : Build 'new()' method as part of a pm file |
49
|
|
|
|
|
|
|
Returns : String holding sub new. |
50
|
|
|
|
|
|
|
Argument : $module: pointer to the module being built |
51
|
|
|
|
|
|
|
(as there can be more than one module built by EU::MM); |
52
|
|
|
|
|
|
|
for the primary module it is a pointer to $self |
53
|
|
|
|
|
|
|
Throws : n/a |
54
|
|
|
|
|
|
|
Comment : This method is a likely candidate for alteration in a subclass, |
55
|
|
|
|
|
|
|
e.g., pass a single hash-ref to new() instead of a list of |
56
|
|
|
|
|
|
|
parameters. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub new |
61
|
|
|
|
|
|
|
{ |
62
|
1
|
|
|
1
|
1
|
84
|
$log->debug( "new", { progname => $0, pid => $$, perl_version => $], package => __PACKAGE__ } ); |
63
|
1
|
|
|
|
|
6
|
my ( $class, %parameters ) = @_; |
64
|
1
|
|
|
|
|
12
|
my $self = $class->SUPER::new( %parameters ); |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
39
|
$self->_initialize( %parameters ); |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
3
|
return $self; |
69
|
|
|
|
|
|
|
} ## end sub new |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _initialize() |
72
|
|
|
|
|
|
|
{ |
73
|
1
|
|
|
1
|
|
5
|
$log->debug( "_initialize", { package => __PACKAGE__ } ); |
74
|
1
|
|
|
|
|
4
|
my ( $self, %parameters ) = @_; |
75
|
1
|
|
|
|
|
7
|
$self->SUPER::_initialize( %parameters ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$self->{ config } = { |
78
|
1
|
|
|
|
|
3169
|
protocol => undef, |
79
|
|
|
|
|
|
|
host => undef, |
80
|
|
|
|
|
|
|
user => undef, |
81
|
|
|
|
|
|
|
password => undef, |
82
|
|
|
|
|
|
|
port => undef, |
83
|
|
|
|
|
|
|
passive => undef, |
84
|
|
|
|
|
|
|
identity_file => undef, |
85
|
|
|
|
|
|
|
debug => undef, |
86
|
|
|
|
|
|
|
localDirectory => undef, |
87
|
|
|
|
|
|
|
ssh_options => undef, |
88
|
|
|
|
|
|
|
directories => {}, |
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
3
|
$self->{ connection } = undef; |
92
|
1
|
|
|
|
|
4
|
$self->{ json } = undef; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
95
|
|
|
|
|
|
|
# Tipos de protocolos suportados |
96
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
97
|
|
|
|
|
|
|
$self->{ supportedProtocols } = { |
98
|
1
|
|
|
|
|
9
|
'FTP' => { |
99
|
|
|
|
|
|
|
connect => 'connectFTP', |
100
|
|
|
|
|
|
|
getFiles => 'getFilesFTP', |
101
|
|
|
|
|
|
|
}, |
102
|
|
|
|
|
|
|
'SFTP' => { |
103
|
|
|
|
|
|
|
connect => 'connectSFTP', |
104
|
|
|
|
|
|
|
getFiles => 'getFilesSFTP', |
105
|
|
|
|
|
|
|
}, |
106
|
|
|
|
|
|
|
'LOCAL' => { |
107
|
|
|
|
|
|
|
connect => 'connectLOCAL', |
108
|
|
|
|
|
|
|
getFiles => 'getFilesLOCAL', |
109
|
|
|
|
|
|
|
}, |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
113
|
|
|
|
|
|
|
# Tipos de MIME-Types suportados |
114
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
115
|
|
|
|
|
|
|
$self->{ supportedMimeTypes } = { |
116
|
1
|
|
|
|
|
10
|
'application/x-tar-gz' => FALSE, # .tar.gz |
117
|
|
|
|
|
|
|
'application/x-gzip' => FALSE, # .tar.gz |
118
|
|
|
|
|
|
|
'application/x-bzip' => FALSE, # .bz2 |
119
|
|
|
|
|
|
|
'application/x-bzip2' => FALSE, # .bz2 |
120
|
|
|
|
|
|
|
'application/zip' => 'unZipFile', # .zip |
121
|
|
|
|
|
|
|
'application/x-rar-compressed' => FALSE, # .rar |
122
|
|
|
|
|
|
|
'application/x-tar' => FALSE, # .tar |
123
|
|
|
|
|
|
|
'application/x-7z-compressed' => FALSE, # .tar |
124
|
|
|
|
|
|
|
'application/pdf' => FALSE, # .pdf |
125
|
|
|
|
|
|
|
}; |
126
|
|
|
|
|
|
|
} ## end sub _initialize |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub END |
129
|
|
|
|
|
|
|
{ |
130
|
1
|
|
|
1
|
|
135
|
$log->debug( "END", { package => __PACKAGE__ } ); |
131
|
1
|
|
|
|
|
4
|
eval { $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email }->flush; }; |
|
1
|
|
|
|
|
62
|
|
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#################################################### Sets |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $setProtocol = sub { |
137
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
if ( !$self->{ supportedProtocols }->{ uc( $value ) } ) |
140
|
|
|
|
|
|
|
{ |
141
|
|
|
|
|
|
|
$log->error( "Protocolo [ $value ] não suportado..." ); |
142
|
|
|
|
|
|
|
return FALSE; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
return $self->{ config }->{ protocol } = uc( $value ); |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
my $setHost = sub { |
148
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
149
|
|
|
|
|
|
|
return $self->{ config }->{ host } = $value; |
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
my $setUser = sub { |
152
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
153
|
|
|
|
|
|
|
return $self->{ config }->{ user } = $value; |
154
|
|
|
|
|
|
|
}; |
155
|
|
|
|
|
|
|
my $setPassword = sub { |
156
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
157
|
|
|
|
|
|
|
return $self->{ config }->{ password } = $value; |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
my $setPort = sub { |
160
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
161
|
|
|
|
|
|
|
return $self->{ config }->{ port } = $value; |
162
|
|
|
|
|
|
|
}; |
163
|
|
|
|
|
|
|
my $setDebug = sub { |
164
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
165
|
|
|
|
|
|
|
return $self->{ config }->{ debug } = $value; |
166
|
|
|
|
|
|
|
}; |
167
|
|
|
|
|
|
|
my $setPassive = sub { |
168
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
169
|
|
|
|
|
|
|
return $self->{ config }->{ passive } = $value; |
170
|
|
|
|
|
|
|
}; |
171
|
|
|
|
|
|
|
my $setSsl = sub { |
172
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
173
|
|
|
|
|
|
|
return $self->{ config }->{ ssl } = $value; |
174
|
|
|
|
|
|
|
}; |
175
|
|
|
|
|
|
|
my $setSshOptions = sub { |
176
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
177
|
|
|
|
|
|
|
return $self->{ config }->{ ssh_options } = $value; |
178
|
|
|
|
|
|
|
}; |
179
|
|
|
|
|
|
|
my $setIdentityFile = sub { |
180
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
181
|
|
|
|
|
|
|
return $self->{ config }->{ identity_file } = $value; |
182
|
|
|
|
|
|
|
}; |
183
|
|
|
|
|
|
|
my $setLocalDirectory = sub { |
184
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
185
|
|
|
|
|
|
|
return $self->{ config }->{ localDirectory } = ( $value // './download/' ); |
186
|
|
|
|
|
|
|
}; |
187
|
|
|
|
|
|
|
my $setDirectories = sub { |
188
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$self->{ config }->{ directories } = undef; |
191
|
|
|
|
|
|
|
while ( my ( $remoteDirectory, $configuration ) = each( %{ $value } ) ) |
192
|
|
|
|
|
|
|
{ |
193
|
|
|
|
|
|
|
$configuration->{ remoteDirectory } = $remoteDirectory; |
194
|
|
|
|
|
|
|
return FALSE unless $self->addDirectory( $configuration ); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
return TRUE; |
197
|
|
|
|
|
|
|
}; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
#################################################### Sets |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 C<addDirectory()> |
202
|
|
|
|
|
|
|
=cut |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
205
|
|
|
|
|
|
|
# Adiciona uma configuracao para upload |
206
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
207
|
|
|
|
|
|
|
sub addDirectory |
208
|
|
|
|
|
|
|
{ |
209
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $configuration ) = @_; |
210
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
0
|
return FALSE unless $configuration->{ fileNameRule }; |
212
|
0
|
0
|
|
|
|
0
|
return FALSE unless $configuration->{ remoteDirectory }; |
213
|
0
|
0
|
|
|
|
0
|
return FALSE unless ref $configuration->{ downloadedFiles } eq 'HASH'; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
#{ |
216
|
|
|
|
|
|
|
# downloadedFiles {}, |
217
|
|
|
|
|
|
|
# fileNameRule "VALID_RETORNO_.*", |
218
|
|
|
|
|
|
|
# remoteDirectory "/valid/upload/" |
219
|
|
|
|
|
|
|
#} |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
0
|
$self->{ config }->{ directories }->{ $configuration->{ remoteDirectory } } = $configuration; |
222
|
0
|
|
|
|
|
0
|
return TRUE; |
223
|
|
|
|
|
|
|
} ## end sub addDirectory |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 C<cleanDirectories()> |
226
|
|
|
|
|
|
|
=cut |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
229
|
|
|
|
|
|
|
# Limpa os directories |
230
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
231
|
|
|
|
|
|
|
sub cleanDirectories |
232
|
|
|
|
|
|
|
{ |
233
|
0
|
|
|
0
|
1
|
0
|
my ( $self ) = @_; |
234
|
0
|
|
|
|
|
0
|
return $self->{ config }->{ directories } = {}; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 C<setConfig()> |
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub setConfig() |
241
|
|
|
|
|
|
|
{ |
242
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
0
|
$self->$setProtocol( $parameters{ protocol } ); |
245
|
0
|
|
|
|
|
0
|
$self->$setHost( $parameters{ host } ); |
246
|
0
|
|
|
|
|
0
|
$self->$setUser( $parameters{ user } ); |
247
|
0
|
|
|
|
|
0
|
$self->$setPassword( $parameters{ password } ); |
248
|
0
|
|
|
|
|
0
|
$self->$setPort( $parameters{ port } ); |
249
|
0
|
|
|
|
|
0
|
$self->$setDebug( $parameters{ debug } ); |
250
|
0
|
|
|
|
|
0
|
$self->$setPassive( $parameters{ passive } ); |
251
|
0
|
|
|
|
|
0
|
$self->$setSsl( $parameters{ ssl } ); |
252
|
0
|
|
|
|
|
0
|
$self->$setSshOptions( $parameters{ ssh_options } ); |
253
|
0
|
|
|
|
|
0
|
$self->$setIdentityFile( $parameters{ identity_file } ); |
254
|
0
|
|
|
|
|
0
|
$self->$setLocalDirectory( $parameters{ localDirectory } ); |
255
|
0
|
|
|
|
|
0
|
$self->$setDirectories( $parameters{ directories } ); |
256
|
|
|
|
|
|
|
|
257
|
0
|
|
|
|
|
0
|
return $self->testConfig( %parameters ); |
258
|
|
|
|
|
|
|
} ## end sub setConfig |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 C<testConfig()> |
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub testConfig() |
264
|
|
|
|
|
|
|
{ |
265
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
0
|
return TRUE; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head2 C<connect()> |
271
|
|
|
|
|
|
|
=cut |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub connect() |
274
|
|
|
|
|
|
|
{ |
275
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
276
|
|
|
|
|
|
|
|
277
|
0
|
0
|
|
|
|
0
|
return FALSE unless $self->testConfig(); |
278
|
|
|
|
|
|
|
|
279
|
0
|
|
|
|
|
0
|
my $connectSub = $self->{ supportedProtocols }->{ uc $self->{ config }->{ protocol } }->{ connect }; |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
0
|
return $self->$connectSub( %parameters ); |
282
|
|
|
|
|
|
|
} ## end sub connect |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head2 C<connectFTP()> |
285
|
|
|
|
|
|
|
=cut |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub connectFTP () |
288
|
|
|
|
|
|
|
{ |
289
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
290
|
|
|
|
|
|
|
|
291
|
0
|
|
|
|
|
0
|
my $moduleConn = 'Net::FTP'; |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
|
|
0
|
eval "require $moduleConn;"; |
294
|
|
|
|
|
|
|
|
295
|
0
|
0
|
|
|
|
0
|
if ( $@ ) |
296
|
|
|
|
|
|
|
{ |
297
|
0
|
|
|
|
|
0
|
$log->error( "Erro ao usar o módulo [ $moduleConn ]..." . $@ ); |
298
|
0
|
|
|
|
|
0
|
return FALSE; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
my %args = ( |
302
|
|
|
|
|
|
|
Host => $self->{ config }->{ host }, |
303
|
|
|
|
|
|
|
Port => $self->{ config }->{ port } // 21, |
304
|
|
|
|
|
|
|
Debug => $self->{ config }->{ debug }, |
305
|
0
|
|
0
|
|
|
0
|
Passive => $self->{ config }->{ passive } // 1, |
|
|
|
0
|
|
|
|
|
306
|
|
|
|
|
|
|
); |
307
|
|
|
|
|
|
|
|
308
|
0
|
|
|
|
|
0
|
$log->info( "Conectando no FTP [ $args{Host}\:$args{Port} ]" ); |
309
|
0
|
|
|
|
|
0
|
$self->{ connection } = Net::FTP->new( %args ); |
310
|
|
|
|
|
|
|
|
311
|
0
|
0
|
|
|
|
0
|
if ( $self->{ connection } ) |
312
|
|
|
|
|
|
|
{ |
313
|
0
|
0
|
|
|
|
0
|
$self->{ connection }->starttls() if $self->{ config }->{ ssl }; |
314
|
0
|
|
|
|
|
0
|
my $user = $self->{ config }->{ user }; |
315
|
0
|
|
|
|
|
0
|
my $password = $self->{ config }->{ password }; |
316
|
0
|
0
|
|
|
|
0
|
if ( !$self->{ connection }->login( $user, $password ) ) |
317
|
|
|
|
|
|
|
{ |
318
|
0
|
|
|
|
|
0
|
$log->error( $self->{ connection }->message ); |
319
|
0
|
|
|
|
|
0
|
return FALSE; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
else |
322
|
|
|
|
|
|
|
{ |
323
|
0
|
|
|
|
|
0
|
$log->info( "Conexão feita com sucesso no FTP [ ${user}\@$args{Host} ]..." ); |
324
|
0
|
|
|
|
|
0
|
return $self->{ connection }; |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
} ## end if ( $self->{ connection...}) |
327
|
|
|
|
|
|
|
else |
328
|
|
|
|
|
|
|
{ |
329
|
0
|
|
|
|
|
0
|
$log->error( "Não foi possÃvel criar o objeto FTP..." ); |
330
|
0
|
|
|
|
|
0
|
return FALSE; |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
0
|
|
|
|
|
0
|
return FALSE; |
334
|
|
|
|
|
|
|
} ## end sub connectFTP |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=head2 C<connectSFTP()> |
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
sub connectSFTP () |
340
|
|
|
|
|
|
|
{ |
341
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
342
|
|
|
|
|
|
|
|
343
|
0
|
|
|
|
|
0
|
my $moduleConn = 'Net::SFTP::Foreign'; |
344
|
|
|
|
|
|
|
|
345
|
0
|
|
|
|
|
0
|
eval "require $moduleConn;"; |
346
|
|
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
0
|
if ( $@ ) |
348
|
|
|
|
|
|
|
{ |
349
|
0
|
|
|
|
|
0
|
$log->error( "Erro ao usar o módulo [ $moduleConn ]..." . $@ ); |
350
|
0
|
|
|
|
|
0
|
return FALSE; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
my %args = ( |
354
|
|
|
|
|
|
|
host => $self->{ config }->{ host }, |
355
|
|
|
|
|
|
|
user => $self->{ config }->{ user }, |
356
|
|
|
|
|
|
|
password => $self->{ config }->{ password }, |
357
|
0
|
|
0
|
|
|
0
|
port => $self->{ config }->{ port } // 22, |
358
|
|
|
|
|
|
|
autodie => 0, |
359
|
|
|
|
|
|
|
more => [ |
360
|
|
|
|
|
|
|
-o => 'StrictHostKeyChecking no', |
361
|
|
|
|
|
|
|
-o => 'HostKeyAlgorithms +ssh-dss', |
362
|
|
|
|
|
|
|
], |
363
|
|
|
|
|
|
|
); |
364
|
0
|
0
|
|
|
|
0
|
push @{ $args{ more } }, '-v' if $self->{ config }->{ debug }; |
|
0
|
|
|
|
|
0
|
|
365
|
|
|
|
|
|
|
|
366
|
0
|
0
|
|
|
|
0
|
push @{ $args{ key_path } }, $self->{ config }->{ identity_file } if $self->{ config }->{ identity_file }; |
|
0
|
|
|
|
|
0
|
|
367
|
|
|
|
|
|
|
|
368
|
0
|
0
|
|
|
|
0
|
if ( $self->{ config }->{ ssh_options } ) |
369
|
|
|
|
|
|
|
{ |
370
|
0
|
|
|
|
|
0
|
my @ssh_options = split( '\|', $self->{ config }->{ ssh_options } ); |
371
|
0
|
|
|
|
|
0
|
push @{ $args{ more } }, map { -o => $_ } @ssh_options; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
0
|
|
|
|
|
0
|
$log->info( "Conectando no SFTP [ $args{host}\:$args{port} ]" ); |
375
|
0
|
|
|
|
|
0
|
$self->{ connection } = eval { Net::SFTP::Foreign->new( %args ); }; |
|
0
|
|
|
|
|
0
|
|
376
|
|
|
|
|
|
|
|
377
|
0
|
0
|
|
|
|
0
|
if ( $@ ) |
|
|
0
|
|
|
|
|
|
378
|
|
|
|
|
|
|
{ |
379
|
0
|
|
|
|
|
0
|
$log->error( $@ ); |
380
|
0
|
|
|
|
|
0
|
$log->error( "Não foi possÃvel criar o objeto SFTP..." ); |
381
|
0
|
|
|
|
|
0
|
return FALSE; |
382
|
|
|
|
|
|
|
} ## end if ( $@ ) |
383
|
|
|
|
|
|
|
elsif ( $self->{ connection }->error ) |
384
|
|
|
|
|
|
|
{ |
385
|
0
|
|
|
|
|
0
|
$log->error( $self->{ connection }->error ); |
386
|
0
|
|
|
|
|
0
|
return FALSE; |
387
|
|
|
|
|
|
|
} |
388
|
|
|
|
|
|
|
else |
389
|
|
|
|
|
|
|
{ |
390
|
0
|
|
|
|
|
0
|
$log->info( "Conexão feita com sucesso no SFTP [ $args{user}\@$args{host} ]..." ); |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
|
393
|
0
|
|
|
|
|
0
|
return $self->{ connection }; |
394
|
|
|
|
|
|
|
} ## end sub connectSFTP |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=head2 C<connectLocal()> |
397
|
|
|
|
|
|
|
=cut |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
sub connectLocal () |
400
|
|
|
|
|
|
|
{ |
401
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
402
|
|
|
|
|
|
|
|
403
|
0
|
|
|
|
|
0
|
return TRUE; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
=head2 C<getActiveDirectory()> |
407
|
|
|
|
|
|
|
=cut |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
410
|
|
|
|
|
|
|
# Pega o diretorio atual |
411
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
412
|
|
|
|
|
|
|
sub getActiveDirectory() |
413
|
|
|
|
|
|
|
{ |
414
|
0
|
|
|
0
|
1
|
0
|
my ( $self ) = @_; |
415
|
|
|
|
|
|
|
|
416
|
0
|
0
|
|
|
|
0
|
$self->{ config }->{ activeDirectory } = $self->{ config }->{ activeDirectory } ? $self->{ config }->{ activeDirectory } : '/'; |
417
|
|
|
|
|
|
|
|
418
|
0
|
|
|
|
|
0
|
return $self->{ config }->{ activeDirectory }; |
419
|
|
|
|
|
|
|
} ## end sub getActiveDirectory |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
=head2 C<setActiveDirectory()> |
422
|
|
|
|
|
|
|
=cut |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
425
|
|
|
|
|
|
|
# Pega o diretorio atual |
426
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
427
|
|
|
|
|
|
|
sub setActiveDirectory() |
428
|
|
|
|
|
|
|
{ |
429
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $directory ) = @_; |
430
|
|
|
|
|
|
|
|
431
|
0
|
|
|
|
|
0
|
return $self->{ config }->{ activeDirectory } = $directory; |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=head2 C<getFiles()> |
435
|
|
|
|
|
|
|
=cut |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
sub getFiles |
438
|
|
|
|
|
|
|
{ |
439
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
440
|
|
|
|
|
|
|
|
441
|
0
|
0
|
|
|
|
0
|
return FALSE unless ( $self->testConfig() ); |
442
|
|
|
|
|
|
|
|
443
|
0
|
|
|
|
|
0
|
my $protocol = uc $self->{ config }->{ protocol }; |
444
|
0
|
|
|
|
|
0
|
my $getFilesSub = $self->{ supportedProtocols }->{ $protocol }->{ getFiles }; |
445
|
0
|
|
|
|
|
0
|
my $retorno; |
446
|
|
|
|
|
|
|
|
447
|
0
|
|
|
|
|
0
|
while ( my ( $directory, $directoryConfiguration ) = each( %{ $self->{ config }->{ directories } } ) ) |
|
0
|
|
|
|
|
0
|
|
448
|
|
|
|
|
|
|
{ |
449
|
0
|
|
|
|
|
0
|
$self->setActiveDirectory( $directory ); |
450
|
|
|
|
|
|
|
|
451
|
0
|
|
|
|
|
0
|
$retorno->{ $directory } = $self->$getFilesSub( %parameters ); |
452
|
|
|
|
|
|
|
|
453
|
0
|
0
|
0
|
|
|
0
|
if ( ( ref $retorno->{ $directory } eq 'HASH' ) && ( $retorno->{ $directory }->{ error } == 0 ) && ( $directoryConfiguration->{ 'unpack' } ) ) |
|
|
|
0
|
|
|
|
|
454
|
|
|
|
|
|
|
{ |
455
|
0
|
|
|
|
|
0
|
foreach my $file ( @{ $retorno->{ $directory }->{ files } } ) |
|
0
|
|
|
|
|
0
|
|
456
|
|
|
|
|
|
|
{ |
457
|
0
|
0
|
0
|
|
|
0
|
if ( ref $file eq 'HASH' && $file->{ error } == 0 ) |
458
|
|
|
|
|
|
|
{ |
459
|
0
|
|
|
|
|
0
|
$file->{ 'unpack' } = $self->unPackFile( conf => $directoryConfiguration, file => $file ); |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
} ## end foreach my $file ( @{ $retorno...}) |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
} ## end if ( ( ref $retorno->{...})) |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
} ## end while ( my ( $directory, ...)) |
467
|
|
|
|
|
|
|
|
468
|
0
|
|
|
|
|
0
|
return $retorno; |
469
|
|
|
|
|
|
|
} ## end sub getFiles |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
=head2 C<getFilesFTP()> |
472
|
|
|
|
|
|
|
=cut |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
sub getFilesFTP() |
475
|
|
|
|
|
|
|
{ |
476
|
0
|
|
|
0
|
1
|
0
|
$log->debug( "getFilesFTP", { package => __PACKAGE__ } ); |
477
|
0
|
|
|
|
|
0
|
my ( $self, %parameters ) = @_; |
478
|
|
|
|
|
|
|
|
479
|
0
|
|
|
|
|
0
|
my $retorno = { |
480
|
|
|
|
|
|
|
error => 0, |
481
|
|
|
|
|
|
|
message => 'ok', |
482
|
|
|
|
|
|
|
downloadedFiles => [], |
483
|
|
|
|
|
|
|
notDownloadedFiles => [] |
484
|
|
|
|
|
|
|
}; |
485
|
|
|
|
|
|
|
|
486
|
0
|
|
|
|
|
0
|
my $remoteDirectory = $self->getActiveDirectory(); |
487
|
0
|
|
|
|
|
0
|
my $configuration = $self->{ config }->{ directories }->{ $remoteDirectory }; |
488
|
0
|
|
|
|
|
0
|
my $localDirectory = $self->{ config }->{ localDirectory }; |
489
|
|
|
|
|
|
|
|
490
|
0
|
|
|
|
|
0
|
$log->info( "Entrando em [ getFilesFTP ] para o diretório [ $remoteDirectory ]..." ); |
491
|
|
|
|
|
|
|
|
492
|
0
|
0
|
|
|
|
0
|
unless ( $self->{ connection }->cwd( $remoteDirectory ) ) |
493
|
|
|
|
|
|
|
{ |
494
|
|
|
|
|
|
|
return { |
495
|
|
|
|
|
|
|
error => 1, |
496
|
|
|
|
|
|
|
message => $self->{ connection }->message() |
497
|
0
|
|
|
|
|
0
|
}; |
498
|
|
|
|
|
|
|
} ## end unless ( $self->{ connection...}) |
499
|
|
|
|
|
|
|
|
500
|
0
|
0
|
|
|
|
0
|
unless ( $self->{ connection }->binary() ) |
501
|
|
|
|
|
|
|
{ |
502
|
|
|
|
|
|
|
return { |
503
|
|
|
|
|
|
|
error => 1, |
504
|
|
|
|
|
|
|
message => $self->{ connection }->message() |
505
|
0
|
|
|
|
|
0
|
}; |
506
|
|
|
|
|
|
|
} ## end unless ( $self->{ connection...}) |
507
|
|
|
|
|
|
|
|
508
|
0
|
|
|
|
|
0
|
my $remoteFiles = $self->{ connection }->ls(); |
509
|
|
|
|
|
|
|
|
510
|
0
|
0
|
0
|
|
|
0
|
if ( ( !defined $remoteFiles ) && ( $self->{ connection }->message() =~ /No files found/ ) ) |
|
|
0
|
|
|
|
|
|
511
|
|
|
|
|
|
|
{ |
512
|
0
|
|
|
|
|
0
|
$log->warn( $self->{ connection }->message() ); |
513
|
|
|
|
|
|
|
return { |
514
|
|
|
|
|
|
|
error => 0, |
515
|
0
|
|
|
|
|
0
|
message => $self->{ connection }->message(), |
516
|
|
|
|
|
|
|
files => [] |
517
|
|
|
|
|
|
|
}; |
518
|
|
|
|
|
|
|
} ## end if ( ( !defined $remoteFiles...)) |
519
|
|
|
|
|
|
|
elsif ( !defined $remoteFiles ) |
520
|
|
|
|
|
|
|
{ |
521
|
|
|
|
|
|
|
return { |
522
|
|
|
|
|
|
|
error => 1, |
523
|
0
|
|
0
|
|
|
0
|
message => ( $self->{ connection }->message() // '' ) |
524
|
|
|
|
|
|
|
}; |
525
|
|
|
|
|
|
|
} ## end elsif ( !defined $remoteFiles...) |
526
|
|
|
|
|
|
|
else |
527
|
|
|
|
|
|
|
{ |
528
|
0
|
|
|
|
|
0
|
foreach my $remoteFile ( @{ $remoteFiles } ) |
|
0
|
|
|
|
|
0
|
|
529
|
|
|
|
|
|
|
{ |
530
|
0
|
|
|
|
|
0
|
my $status = $self->canDownloadFile( fileName => $remoteFile ); |
531
|
0
|
0
|
|
|
|
0
|
if ( $status < 0 ) |
532
|
|
|
|
|
|
|
{ |
533
|
0
|
|
|
|
|
0
|
push( @{ $retorno->{ notDownloadedFiles } }, { name => $remoteFile, status => $status } ); |
|
0
|
|
|
|
|
0
|
|
534
|
0
|
|
|
|
|
0
|
next; |
535
|
|
|
|
|
|
|
} |
536
|
|
|
|
|
|
|
|
537
|
0
|
|
|
|
|
0
|
$log->info( "Baixando o arquivo [ $remoteFile ]..." ); |
538
|
|
|
|
|
|
|
|
539
|
0
|
|
|
|
|
0
|
my $localFile = $localDirectory . '/' . $remoteFile; |
540
|
0
|
|
|
|
|
0
|
my $get = eval { $self->{ connection }->get( $remoteFile, $localFile ); }; |
|
0
|
|
|
|
|
0
|
|
541
|
|
|
|
|
|
|
|
542
|
0
|
|
|
|
|
0
|
my $file = { |
543
|
|
|
|
|
|
|
error => 0, |
544
|
|
|
|
|
|
|
message => '', |
545
|
|
|
|
|
|
|
name => $remoteFile, |
546
|
|
|
|
|
|
|
file_size => -1, |
547
|
|
|
|
|
|
|
md5sum => undef, |
548
|
|
|
|
|
|
|
filePath => $localFile |
549
|
|
|
|
|
|
|
}; |
550
|
|
|
|
|
|
|
|
551
|
0
|
0
|
|
|
|
0
|
if ( !$get ) |
552
|
|
|
|
|
|
|
{ |
553
|
0
|
|
|
|
|
0
|
$file->{ error } = 1; |
554
|
0
|
|
|
|
|
0
|
$file->{ message } = $self->{ connection }->message(); |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
else |
557
|
|
|
|
|
|
|
{ |
558
|
0
|
|
|
|
|
0
|
$file->{ file_size } = -s $localFile; |
559
|
0
|
|
|
|
|
0
|
my $md5 = $self->getFileMD5( file => $localFile ); |
560
|
0
|
|
|
|
|
0
|
$file->{ md5sum } = $md5; |
561
|
|
|
|
|
|
|
} ## end else [ if ( !$get ) ] |
562
|
|
|
|
|
|
|
|
563
|
0
|
|
|
|
|
0
|
push( @{ $retorno->{ downloadedFiles } }, $file ); |
|
0
|
|
|
|
|
0
|
|
564
|
0
|
0
|
|
|
|
0
|
last() if ( $parameters{ only_one_file } ); |
565
|
|
|
|
|
|
|
} ## end foreach my $remoteFile ( @{...}) |
566
|
|
|
|
|
|
|
} ## end else [ if ( ( !defined $remoteFiles...))] |
567
|
|
|
|
|
|
|
|
568
|
0
|
0
|
|
|
|
0
|
if ( scalar @{ $retorno->{ downloadedFiles } } == 0 ) |
|
0
|
|
|
|
|
0
|
|
569
|
|
|
|
|
|
|
{ |
570
|
0
|
|
|
|
|
0
|
$log->warn( 'Nenhum arquivo para ser baixado...' ); |
571
|
|
|
|
|
|
|
} |
572
|
0
|
|
|
|
|
0
|
return $retorno; |
573
|
|
|
|
|
|
|
} ## end sub getFilesFTP |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
=head2 C<canDownloadFile()> |
576
|
|
|
|
|
|
|
=cut |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
sub canDownloadFile() |
579
|
|
|
|
|
|
|
{ |
580
|
0
|
|
|
0
|
1
|
0
|
$log->debug( "canDownloadFile", { package => __PACKAGE__ } ); |
581
|
0
|
|
|
|
|
0
|
my ( $self, %parameters ) = @_; |
582
|
0
|
|
|
|
|
0
|
my $fileName = $parameters{ fileName }; |
583
|
|
|
|
|
|
|
|
584
|
0
|
|
|
|
|
0
|
my $activeDownload = $self->{ config }->{ directories }->{ $self->getActiveDirectory() }; |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
# ja foi baixado |
587
|
0
|
0
|
|
|
|
0
|
return -1 if ( $activeDownload->{ downloadedFiles }{ $fileName } ); |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
# nao bate com a regra de nomes |
590
|
0
|
0
|
|
|
|
0
|
if ( defined $activeDownload->{ fileNameRule } ) |
591
|
|
|
|
|
|
|
{ |
592
|
0
|
|
|
|
|
0
|
$self->{ message } = "Arquivo [ $fileName ] não bate com a regra de filename_pattern."; |
593
|
0
|
0
|
|
|
|
0
|
return -2 if ( $fileName !~ /$activeDownload->{fileNameRule}/ ); |
594
|
0
|
|
|
|
|
0
|
return TRUE; |
595
|
|
|
|
|
|
|
} ## end if ( defined $activeDownload...) |
596
|
|
|
|
|
|
|
else |
597
|
|
|
|
|
|
|
{ |
598
|
0
|
|
|
|
|
0
|
$log->error( "Não existe regra para o nome do arquivo......" ); |
599
|
0
|
|
|
|
|
0
|
return FALSE; |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
|
602
|
0
|
|
|
|
|
0
|
return FALSE; |
603
|
|
|
|
|
|
|
} ## end sub canDownloadFile |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
=head2 C<getFilesSFTP()> |
606
|
|
|
|
|
|
|
=cut |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
sub getFilesSFTP() |
609
|
|
|
|
|
|
|
{ |
610
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
611
|
|
|
|
|
|
|
|
612
|
0
|
|
|
|
|
0
|
my $retorno = { |
613
|
|
|
|
|
|
|
error => 0, |
614
|
|
|
|
|
|
|
message => 'ok', |
615
|
|
|
|
|
|
|
downloadedFiles => [], |
616
|
|
|
|
|
|
|
notDownloadedFiles => [] |
617
|
|
|
|
|
|
|
}; |
618
|
|
|
|
|
|
|
|
619
|
0
|
|
|
|
|
0
|
my $remoteDirectory = $self->getActiveDirectory(); |
620
|
0
|
|
|
|
|
0
|
my $configuration = $self->{ config }->{ directories }->{ $remoteDirectory }; |
621
|
0
|
|
|
|
|
0
|
my $localDirectory = $self->{ config }->{ localDirectory }; #'./download/'; |
622
|
|
|
|
|
|
|
|
623
|
0
|
|
|
|
|
0
|
$log->info( "Entrando em [ getFilesSFTP ] para o diretório [ $remoteDirectory ]..." ); |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
my $ls = $self->{ connection }->ls( |
626
|
|
|
|
|
|
|
$remoteDirectory, |
627
|
|
|
|
|
|
|
wanted => sub { |
628
|
0
|
|
|
0
|
|
0
|
my $entry = $_[ 1 ]; |
629
|
0
|
|
0
|
|
|
0
|
return ( $entry->{ a }->{ size } > 0 and ( $entry->{ filename } =~ qr/$configuration->{ fileNameRule }/i ) and ref $configuration->{ downloadedFiles } eq 'HASH' and !$configuration->{ downloadedFiles }->{ $entry->{ filename } } ); |
630
|
|
|
|
|
|
|
} |
631
|
0
|
|
|
|
|
0
|
); |
632
|
|
|
|
|
|
|
|
633
|
0
|
0
|
|
|
|
0
|
if ( scalar @{ $ls } > 0 ) |
|
0
|
|
|
|
|
0
|
|
634
|
|
|
|
|
|
|
{ |
635
|
|
|
|
|
|
|
my $callback = sub { |
636
|
0
|
|
|
0
|
|
0
|
my ( $sftp, $data, $offset, $size, $progress ) = @_; |
637
|
0
|
0
|
|
|
|
0
|
$offset = $size if ( $offset >= $size ); |
638
|
0
|
|
|
|
|
0
|
$progress->update( $offset ); |
639
|
0
|
|
|
|
|
0
|
}; |
640
|
|
|
|
|
|
|
|
641
|
0
|
|
|
|
|
0
|
foreach my $remoteFile ( @{ $ls } ) |
|
0
|
|
|
|
|
0
|
|
642
|
|
|
|
|
|
|
{ |
643
|
|
|
|
|
|
|
my $progress = Term::ProgressBar->new( |
644
|
|
|
|
|
|
|
{ |
645
|
|
|
|
|
|
|
name => $remoteFile->{ filename } . " ( $remoteFile->{ a }->{ size } ) ", |
646
|
|
|
|
|
|
|
count => $remoteFile->{ a }->{ size }, |
647
|
0
|
|
|
|
|
0
|
ETA => 'linear', |
648
|
|
|
|
|
|
|
remove => 1, |
649
|
|
|
|
|
|
|
silent => !DEBUG, |
650
|
|
|
|
|
|
|
} |
651
|
|
|
|
|
|
|
); |
652
|
0
|
|
|
|
|
0
|
$progress->minor( 0 ); |
653
|
|
|
|
|
|
|
|
654
|
0
|
|
|
0
|
|
0
|
my %options = ( callback => sub { &$callback( @_, $progress ); }, mkpath => 1 ); |
|
0
|
|
|
|
|
0
|
|
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
#$log->debug( $remoteDirectory . $remoteFile->{ filename } ); |
657
|
0
|
|
|
|
|
0
|
$self->{ connection }->get( $remoteDirectory . $remoteFile->{ filename }, $localDirectory . $remoteFile->{ filename }, %options ); |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
my $downloadedFile = { |
660
|
|
|
|
|
|
|
error => FALSE, |
661
|
|
|
|
|
|
|
message => undef, |
662
|
|
|
|
|
|
|
filename => $remoteFile->{ filename }, |
663
|
|
|
|
|
|
|
size => $remoteFile->{ a }->{ size }, |
664
|
|
|
|
|
|
|
md5sum => undef, |
665
|
|
|
|
|
|
|
filePath => $localDirectory . $remoteFile->{ filename }, |
666
|
0
|
|
|
|
|
0
|
}; |
667
|
|
|
|
|
|
|
|
668
|
0
|
0
|
|
|
|
0
|
if ( $self->{ connection }->error ) |
669
|
|
|
|
|
|
|
{ |
670
|
0
|
|
|
|
|
0
|
$log->error( $self->{ connection }->error ); |
671
|
0
|
|
|
|
|
0
|
$log->error( $remoteDirectory . $remoteFile->{ filename } ); |
672
|
0
|
|
|
|
|
0
|
$downloadedFile->{ error } = TRUE; |
673
|
0
|
|
|
|
|
0
|
$downloadedFile->{ message } = $self->{ connection }->error; |
674
|
0
|
|
|
|
|
0
|
$downloadedFile->{ remoteFile } = $remoteDirectory . $remoteFile->{ filename }; |
675
|
|
|
|
|
|
|
} ## end if ( $self->{ connection...}) |
676
|
|
|
|
|
|
|
else |
677
|
|
|
|
|
|
|
{ |
678
|
0
|
|
|
|
|
0
|
$downloadedFile->{ md5sum } = $self->getFileMD5( file => $localDirectory . $remoteFile->{ filename } ); |
679
|
0
|
|
|
|
|
0
|
$downloadedFile->{ message } = 'Ok'; |
680
|
0
|
|
|
|
|
0
|
$log->info( "Download do arquivo [ $downloadedFile->{ filename } ] feito com sucesso..." ); |
681
|
|
|
|
|
|
|
} ## end else [ if ( $self->{ connection...})] |
682
|
|
|
|
|
|
|
|
683
|
0
|
|
|
|
|
0
|
push( @{ $retorno->{ downloadedFiles } }, $downloadedFile ); |
|
0
|
|
|
|
|
0
|
|
684
|
|
|
|
|
|
|
} ## end foreach my $remoteFile ( @{...}) |
685
|
|
|
|
|
|
|
} ## end if ( scalar @{ $ls } >...) |
686
|
|
|
|
|
|
|
else |
687
|
|
|
|
|
|
|
{ |
688
|
0
|
|
|
|
|
0
|
my $msg = "Nenhum arquivo para ser baixado..."; |
689
|
0
|
|
|
|
|
0
|
$log->warn( $msg ); |
690
|
|
|
|
|
|
|
return { |
691
|
0
|
|
|
|
|
0
|
error => 0, |
692
|
|
|
|
|
|
|
message => $msg, |
693
|
|
|
|
|
|
|
files => [] |
694
|
|
|
|
|
|
|
}; |
695
|
|
|
|
|
|
|
} ## end else [ if ( scalar @{ $ls } >...)] |
696
|
|
|
|
|
|
|
|
697
|
0
|
|
|
|
|
0
|
return $retorno; |
698
|
|
|
|
|
|
|
} ## end sub getFilesSFTP |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
=head2 C<getFilesLOCAL()> |
701
|
|
|
|
|
|
|
=cut |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
sub getFilesLOCAL() |
704
|
|
|
|
|
|
|
{ |
705
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
706
|
0
|
|
|
|
|
0
|
return TRUE; |
707
|
|
|
|
|
|
|
} |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
710
|
|
|
|
|
|
|
# Descompacta arquivos |
711
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
=head2 C<unPackFile()> |
714
|
|
|
|
|
|
|
=cut |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
sub unPackFile() |
717
|
|
|
|
|
|
|
{ |
718
|
0
|
|
|
0
|
1
|
0
|
my ( $self, %parameters ) = @_; |
719
|
0
|
|
|
|
|
0
|
my $mmt = $parameters{ conf }{ 'MIME-Type' }; |
720
|
|
|
|
|
|
|
|
721
|
0
|
|
|
|
|
0
|
my $files = []; |
722
|
0
|
0
|
|
|
|
0
|
if ( !$self->{ supportedMimeTypes }->{ $mmt } == FALSE ) |
723
|
|
|
|
|
|
|
{ |
724
|
|
|
|
|
|
|
my $unPackParam = { |
725
|
|
|
|
|
|
|
fileName => $parameters{ file }{ file_path }, |
726
|
|
|
|
|
|
|
out_path => $parameters{ conf }{ unpackDirectory } |
727
|
0
|
|
|
|
|
0
|
}; |
728
|
|
|
|
|
|
|
|
729
|
0
|
|
|
|
|
0
|
my $subname = $self->{ supportedMimeTypes }->{ $mmt }; |
730
|
0
|
|
|
|
|
0
|
$files = $self->$subname( $unPackParam ); |
731
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
} ## end if ( !$self->{ supportedMimeTypes...}) |
733
|
|
|
|
|
|
|
else |
734
|
|
|
|
|
|
|
{ |
735
|
0
|
|
|
|
|
0
|
return { message => "MIME-Type $mmt não suportado", error => 1 }; |
736
|
|
|
|
|
|
|
} |
737
|
|
|
|
|
|
|
|
738
|
0
|
|
|
|
|
0
|
return { error => 0, files => $files }; |
739
|
|
|
|
|
|
|
} ## end sub unPackFile |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
742
|
|
|
|
|
|
|
# Descompacta arquivos ZIP |
743
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
=head2 C<unZipFile()> |
746
|
|
|
|
|
|
|
=cut |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
sub unZipFile() |
749
|
|
|
|
|
|
|
{ |
750
|
|
|
|
|
|
|
|
751
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $param ) = @_; |
752
|
|
|
|
|
|
|
|
753
|
0
|
|
|
|
|
0
|
my $cmd = "unzip -o -b \"$param->{fileName}\" -d \"$param->{out_path}\""; |
754
|
0
|
|
|
|
|
0
|
my @files = `$cmd`; |
755
|
|
|
|
|
|
|
|
756
|
0
|
|
|
|
|
0
|
my $unpack = []; |
757
|
|
|
|
|
|
|
|
758
|
0
|
|
|
|
|
0
|
foreach ( @files ) |
759
|
|
|
|
|
|
|
{ |
760
|
0
|
0
|
|
|
|
0
|
next unless ( $_ =~ /inflating:\s(.+)$/ ); |
761
|
0
|
|
|
|
|
0
|
my $fileName = $1; |
762
|
0
|
|
|
|
|
0
|
$fileName =~ s/(^\s*)|(\s*$)//g; |
763
|
0
|
|
|
|
|
0
|
push( @{ $unpack }, $fileName ); |
|
0
|
|
|
|
|
0
|
|
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
} ## end foreach ( @files ) |
766
|
|
|
|
|
|
|
|
767
|
0
|
|
|
|
|
0
|
return $unpack; |
768
|
|
|
|
|
|
|
} ## end sub unZipFile |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
sub DESTROY |
771
|
|
|
|
|
|
|
{ |
772
|
1
|
|
|
1
|
|
551
|
my ( $self, %parameters ) = @_; |
773
|
1
|
|
|
|
|
9
|
$log->debug( 'DESTROY', { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => FALSE } ); |
774
|
1
|
50
|
|
|
|
7
|
return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; |
775
|
|
|
|
|
|
|
|
776
|
1
|
50
|
33
|
|
|
11
|
if ( blessed( $self ) && $self->isa( __PACKAGE__ ) ) |
777
|
|
|
|
|
|
|
{ |
778
|
1
|
|
|
|
|
8
|
$self->SUPER::DESTROY( %parameters ); |
779
|
1
|
|
|
|
|
25
|
$log->debug( "DESTROY", { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => TRUE } ); |
780
|
|
|
|
|
|
|
} |
781
|
|
|
|
|
|
|
else |
782
|
|
|
|
|
|
|
{ |
783
|
|
|
|
|
|
|
# TODO |
784
|
|
|
|
|
|
|
} |
785
|
|
|
|
|
|
|
} ## end sub DESTROY |
786
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
#################### main pod documentation begin ################### |
788
|
|
|
|
|
|
|
## Below is the stub of documentation for your module. |
789
|
|
|
|
|
|
|
## You better edit it! |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=encoding UTF-8 |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
=head1 NAME |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
Siffra::Transfers - File transfers module |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
=head1 SYNOPSIS |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
use Siffra::Transfers; |
801
|
|
|
|
|
|
|
blah blah blah |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
=head1 DESCRIPTION |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
Stub documentation for this module was created by ExtUtils::ModuleMaker. |
807
|
|
|
|
|
|
|
It looks like the author of the extension was negligent enough |
808
|
|
|
|
|
|
|
to leave the stub unedited. |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
Blah blah blah. |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
=head1 USAGE |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
=head1 BUGS |
818
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
=head1 SUPPORT |
822
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
=head1 AUTHOR |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
Luiz Benevenuto |
828
|
|
|
|
|
|
|
CPAN ID: LUIZBENE |
829
|
|
|
|
|
|
|
Siffra TI |
830
|
|
|
|
|
|
|
luiz@siffra.com.br |
831
|
|
|
|
|
|
|
https://siffra.com.br |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
=head1 COPYRIGHT |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
This program is free software; you can redistribute |
836
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
The full text of the license can be found in the |
839
|
|
|
|
|
|
|
LICENSE file included with this module. |
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=head1 SEE ALSO |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
perl(1). |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=cut |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
#################### main pod documentation end ################### |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
1; |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
# The preceding line will help the module return a true value |
853
|
|
|
|
|
|
|
|