File Coverage

blib/lib/Siffra/Transfers.pm
Criterion Covered Total %
statement 71 269 26.3
branch 2 68 2.9
condition 2 28 7.1
subroutine 19 39 48.7
pod 18 18 100.0
total 112 422 26.5


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