File Coverage

blib/lib/CPAN/Access/AdHoc/Archive/Null.pm
Criterion Covered Total %
statement 110 117 94.0
branch 20 38 52.6
condition 6 12 50.0
subroutine 20 20 100.0
pod 8 8 100.0
total 164 195 84.1


line stmt bran cond sub pod time code
1             package CPAN::Access::AdHoc::Archive::Null;
2              
3 6     6   4774 use 5.008;
  6         18  
  6         218  
4              
5 6     6   29 use strict;
  6         9  
  6         189  
6 6     6   27 use warnings;
  6         7  
  6         185  
7              
8 6     6   26 use base qw{ CPAN::Access::AdHoc::Archive };
  6         7  
  6         581  
9              
10 6     6   29 use CPAN::Access::AdHoc::Util qw{ :carp __guess_media_type };
  6         7  
  6         804  
11 6     6   31 use File::Path 2.07 ();
  6         113  
  6         124  
12 6     6   25 use File::Spec ();
  6         6  
  6         66  
13 6     6   18 use HTTP::Date ();
  6         10  
  6         93  
14 6     6   20 use IO::File ();
  6         10  
  6         56  
15 6     6   20 use IO::Uncompress::Bunzip2 ();
  6         9  
  6         63  
16 6     6   26 use IO::Uncompress::Gunzip ();
  6         8  
  6         7866  
17              
18             our $VERSION = '0.000_194';
19              
20             my %decode = (
21             gzip => sub {
22             my ( $content ) = @_;
23             my $rslt;
24             IO::Uncompress::Gunzip::gunzip( $content, \$rslt );
25             return $rslt;
26             },
27             'x-bzip2' => sub {
28             my ( $content ) = @_;
29             my $rslt;
30             IO::Uncompress::Bunzip2::bunzip2( $content, \$rslt );
31             return $rslt;
32             },
33             );
34              
35              
36             sub new {
37 14     14 1 1791 my ( $class, %arg ) = @_;
38              
39 14   33     89 my $self = bless {}, ref $class || $class;
40 14         74 my $attr = $self->__attr();
41              
42 14 50 33     55 ref $arg{content}
43             or defined $arg{path}
44             or $arg{path} = $arg{content};
45              
46 14         26 my $mtime = delete $arg{mtime};
47              
48 14 50       39 if ( defined( my $content = delete $arg{content} ) ) {
49              
50 14 100       50 if ( my $encoding = delete $arg{encoding} ) {
    50          
    50          
51 9 50       26 $decode{$encoding}
52             or __wail( "Unsupported encoding '$encoding'" );
53 9         25 $content = $decode{$encoding}->( $content );
54             } elsif ( ! ref $content ) {
55 0         0 local $/ = undef; # Slurp mode
56 0 0       0 open my $fh, '<', $content
57             or __wail( "Unable to open $content: $!" );
58 0         0 my @stat = stat $fh;
59 0         0 $content = <$fh>;
60 0         0 close $fh;
61             @stat
62 0 0       0 and $mtime = $stat[9];
63             } elsif ( 'SCALAR' eq ref $content ) {
64 5         5 $content = ${ $content };
  5         12  
65             }
66              
67 14         22 my ( $base_dir, $file_name );
68 14 50       39 if ( $arg{path} ) {
69 14         257 ( undef, $base_dir, $file_name ) =
70             File::Spec->splitpath( $arg{path} );
71 14         43 $base_dir =~ s{ \A authors/id/
72             ([^/]) / ( \1 [^/] ) / \2 [^/]* / }{}smx;
73 14         59 $file_name =~ s/ [.] (?: gz | bz2 ) \z //smx;
74             } else {
75 0         0 ( $base_dir, $file_name ) = ( '', 'unknown' );
76             }
77              
78 14         32 $attr->{base_dir} = $base_dir;
79 14         52 $attr->{contents}{$file_name} = {
80             content => $content,
81             mtime => $mtime,
82             };
83              
84 14         73 $self->archive( undef );
85              
86             }
87              
88 14         54 $self->mtime( $mtime );
89 14         58 $self->path( delete $arg{path} );
90              
91 14         103 return $self;
92             }
93              
94             sub base_directory {
95 5     5 1 385 my ( $self ) = @_;
96 5         13 my $attr = $self->__attr();
97              
98 5         94 return $attr->{base_dir};
99             }
100              
101             sub extract {
102 1     1 1 6 my ( $self ) = @_;
103 1         5 my $attr = $self->__attr();
104              
105 1 50       4 my @dirs = grep { defined $_ and '' ne $_ } File::Spec->splitdir(
  2         15  
106             $self->base_directory() );
107 1         1 my $where;
108 1         3 foreach my $dir ( @dirs ) {
109 1 50       3 $where = defined $where ? File::Spec->catdir( $where, $dir ) :
110             $dir;
111 1 50 33     56 -d $where
112             or mkdir $where
113             or __wail( "Unable to mkdir $where: $!" );
114             }
115              
116 1         4 foreach my $name ( keys %{ $attr->{contents} } ) {
  1         4  
117 1         13 my $path = File::Spec->catfile( $where, $name );
118 1 50       7 my $fh = IO::File->new( $path, '>' )
119             or __wail( "Unable to open $path for output: $!" );
120 1         134 print { $fh } $attr->{contents}{$name}{content};
  1         11  
121 1         36 close $fh;
122 1         3 my $mtime = $attr->{contents}{$name}{mtime};
123 1         26 utime $mtime, $mtime, $path;
124             }
125              
126 1         8 return $self;
127             }
128              
129             sub get_item_content {
130 14     14 1 39 my ( $self, $file ) = @_;
131 14         38 my $attr = $self->__attr();
132              
133 12         42 defined $file
134 14 100       34 or ( $file ) = keys %{ $attr->{contents} };
135              
136 14         55 return $attr->{contents}{$file}{content};
137             }
138              
139             sub get_item_mtime {
140 1     1 1 2 my ( $self, $file ) = @_;
141 1         4 my $attr = $self->__attr();
142              
143 1         5 defined $file
144 1 50       4 or ( $file ) = keys %{ $attr->{contents} };
145              
146 1         3 return $attr->{contents}{$file}{mtime};
147             }
148              
149             {
150              
151             my %handled = map { $_ => 1 } qw{ application/octet-stream };
152              
153             sub __handle_http_response {
154 27     27   35 my ( $class, $rslt ) = @_;
155              
156 27         66 my $content_type = $rslt->header( 'Content-Type' );
157              
158 27 100 100     988 $handled{ $content_type }
159             or $content_type =~ m{ \A text/ }smx
160             or return;
161              
162 14         62 return $class->new(
163             content => \( scalar $rslt->content() ),
164             encoding => scalar $rslt->header( 'Content-Encoding' ),
165             mtime => HTTP::Date::str2time(
166             scalar $rslt->header( 'Last-Modified' ) ),
167             path => scalar $rslt->header( 'Content-Location' ),
168             );
169             }
170              
171             }
172              
173             sub item_present {
174 1     1 1 4 my ( $self, $item ) = @_;
175 1         3 my $attr = $self->__attr();
176              
177 1         7 return defined $attr->{contents}{$item};
178             }
179              
180             sub list_contents {
181 3     3 1 16 my ( $self ) = @_;
182 3         9 my $attr = $self->__attr();
183              
184 3         3 return ( sort keys %{ $attr->{contents} } );
  3         19  
185             }
186              
187             {
188             my %known_encoding = (
189             # The null encoder does a binmode() on its file handle because I
190             # believe that is equivalent to what happens with the
191             # IO::Compress::* packages - i.e. they compress bytes, not
192             # characters.
193             '' => sub {
194             my ( $fn, $content ) = @_;
195             open my $fh, '>', $fn or __wail( "Open $fn failed: $!" );
196             binmode $fh;
197             print { $fh } $content;
198             close $fh;
199             return;
200             },
201             'gzip' => sub {
202             my ( $fn, $content ) = @_;
203             require IO::Compress::Gzip;
204             IO::Compress::Gzip::gzip( \$content, $fn, AutoClose => 1 )
205             or __wail("gzip $fn failed: $IO::Compress::Gzip::GzipError"
206             );
207             return;
208             },
209             'x-bzip2' => sub {
210             my ( $fn, $content ) = @_;
211             require IO::Compress::Bzip2;
212             IO::Compress::Bzip2::bzip2( \$content, $fn )
213             or __wail("bzip2 $fn failed: $IO::Compress::Bzip2::Bzip2Error"
214             );
215             return;
216             },
217             );
218              
219             sub write : method { ## no critic (ProhibitBuiltInHomonyms)
220 1     1 1 6 my ( $self, $fn ) = @_;
221 1         3 my $attr = $self->__attr();
222              
223 1         2 my ( $file ) = keys %{ $attr->{contents} };
  1         4  
224 1 50       4 if ( ! defined $fn ) {
225 1         4 $fn = ( File::Spec->splitpath( $self->path() ) )[2];
226             }
227 1         6 my $resp = HTTP::Response->new();
228 1         34 __guess_media_type( $resp, $fn );
229 1         3 my $encoding = $resp->header( 'Content-Encoding' );
230 1 50       25 defined $encoding
231             or $encoding = '';
232 1 50       4 my $code = $known_encoding{$encoding}
233             or __wail( "Encoding $encoding not supported" );
234 1         4 $code->( $fn, $attr->{contents}{$file}{content} );
235 1         5 return $self;
236             }
237             }
238              
239             1;
240              
241             __END__