| 1 | package Net::SFTP::Foreign::Tempdir::Extract::File; | ||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 9 | ▶ | use File::Tempdir qw{}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 9 | ▶ | use Path::Class::File 0.34 qw{}; #move_to capability | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use Archive::Extract qw{}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use base qw{Path::Class::File}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 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<Path::Class>, L<Archive::Extract> and L<File::Tempdir> | ||||||||||
| 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<Archive::Extract>) | ||||||||||
| 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 | 0 | ▶ | sub extract { | ||||||||
|
Subroutine
extract: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||
| 42 | 0 | my $self = shift; | |||||||||
| 43 | 0 | ▶ | my $ae = Archive::Extract->new(archive=>"$self") or die(qq{Error: Cannot create Archive::Extract object with archive "$self". Verfiy extension.}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | ▶ | my $ae_dir = File::Tempdir->new or die(qq{Error: Cannot create File::Tempdir object}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | ▶ | $ae->extract(to => $ae_dir->name) or die(qq{Error: }. $ae->error); #extracts all files to a temp dir | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | next unless -f $file; #only process files and not folders | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 0 | my $tmpdir = File::Tempdir->new; #separate tmp directory for each file for fine grained cleanup | |||||||||
| 56 | 0 | ▶ | die(sprintf(qq{Error: Dir "%s" is not a directory}, $tmpdir->name)) unless -d $tmpdir->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | $file->move_to(Path::Class::File->new($tmpdir->name, $file->basename)) or die("Error: Failed to move file to temp directory"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $file->{"__tmpdir"} = $tmpdir; #needed for scope clean up of File::Tempdir object | |||||||||
| 60 | 0 | ▶ | die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 0 | push @files, $file; | |||||||||
| 63 | } | ||||||||||
| 64 | 0 | ▶ | return wantarray ? @files : \@files; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<Path::Class>, L<File::Tempdir>, L<Archive::Extract> | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 9 | 1; | |||||||||