File Coverage

blib/lib/Net/SFTP/Foreign/Tempdir/Extract/File.pm
Criterion Covered Total %
statement 18 35 51.4
branch 0 18 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 61 40.9


line stmt bran cond sub pod time code
1             package Net::SFTP::Foreign::Tempdir::Extract::File;
2 9     9   72 use strict;
  9         25  
  9         296  
3 9     9   53 use warnings;
  9         23  
  9         250  
4 9     9   51 use File::Tempdir qw{};
  9         22  
  9         243  
5 9     9   2943 use Path::Class::File 0.34 qw{}; #move_to capability
  9         91568  
  9         257  
6 9     9   6632 use Archive::Extract qw{};
  9         1479931  
  9         446  
7 9     9   98 use base qw{Path::Class::File};
  9         24  
  9         4094  
8              
9             our $VERSION = '0.18';
10              
11             =head1 NAME
12              
13             Net::SFTP::Foreign::Tempdir::Extract::File - Path::Class::File with an extract method
14              
15             =head1 SYNOPSIS
16              
17             use Net::SFTP::Foreign::Tempdir::Extract;
18             my $sftp = Net::SFTP::Foreign::Tempdir::Extract->new(user=>$user, match=>qr/\.zip\Z/);
19             my $file = $sftp->next; # isa Net::SFTP::Foreign::Tempdir::Extract::File
20              
21             =head1 DESCRIPTION
22              
23             Net::SFTP::Foreign::Tempdir::Extract::File is a convince wrapper around L, L and L
24              
25             =head1 USAGE
26              
27             my $archive = Net::SFTP::Foreign::Tempdir::Extract::File->new( $path, $filename );
28             my @files = $archive->extract; #array of Net::SFTP::Foreign::Tempdir::Extract::File files
29              
30             =head2 extract
31              
32             Extracts tar.gz and Zip files to temporary directory (any format supported by L)
33              
34             my @files = $archive->extract; #list of Net::SFTP::Foreign::Tempdir::Extract::File files
35             my $files = $archive->extract; #array reference of Net::SFTP::Foreign::Tempdir::Extract::File files
36              
37             Note: These files are temporary and will be cleaned up when the file object variable goes out of scope.
38              
39             =cut
40              
41             sub extract {
42 0     0 1   my $self = shift;
43 0 0         my $ae = Archive::Extract->new(archive=>"$self") or die(qq{Error: Cannot create Archive::Extract object with archive "$self". Verfiy extension.});
44 0 0         my $ae_dir = File::Tempdir->new or die(qq{Error: Cannot create File::Tempdir object});
45 0 0         $ae->extract(to => $ae_dir->name) or die(qq{Error: }. $ae->error); #extracts all files to a temp dir
46 0           my @files = ();
47 0           my $filenames = $ae->files; #array reference in scalar context
48             #loop through each file, bless and move to individual temp folders
49 0           foreach my $filename (@$filenames) {
50 0           my $file = $self->new($ae_dir->name => $filename); #isa Path::Class::File object
51 0 0         die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file;
52              
53 0 0         next unless -f $file; #only process files and not folders
54              
55 0           my $tmpdir = File::Tempdir->new; #separate tmp directory for each file for fine grained cleanup
56 0 0         die(sprintf(qq{Error: Dir "%s" is not a directory}, $tmpdir->name)) unless -d $tmpdir->name;
57              
58 0 0         $file->move_to(Path::Class::File->new($tmpdir->name, $file->basename)) or die("Error: Failed to move file to temp directory");
59 0           $file->{"__tmpdir"} = $tmpdir; #needed for scope clean up of File::Tempdir object
60 0 0         die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file;
61              
62 0           push @files, $file;
63             }
64 0 0         return wantarray ? @files : \@files;
65             }
66              
67             #head2 __tmpdir
68             #
69             #property to keep the tmp directory in scope for the life of the file object
70             #
71             #cut
72              
73             =head1 TODO
74              
75             Support other archive formats besides zip
76              
77             =head1 BUGS
78              
79             Use GitHub to fork repository and submit pull requests.
80              
81             =head1 AUTHOR
82              
83             Michael R. Davis
84             CPAN ID: MRDVT
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             MIT License
89              
90             Copyright (c) 2021 Michael R. Davis
91              
92             Permission is hereby granted, free of charge, to any person obtaining a copy
93             of this software and associated documentation files (the "Software"), to deal
94             in the Software without restriction, including without limitation the rights
95             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
96             copies of the Software, and to permit persons to whom the Software is
97             furnished to do so, subject to the following conditions:
98              
99             The above copyright notice and this permission notice shall be included in all
100             copies or substantial portions of the Software.
101              
102             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
103             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
104             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
105             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
106             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
107             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
108             SOFTWARE.
109              
110             =head1 SEE ALSO
111              
112             L, L, L
113              
114             =cut
115              
116             1;