File Coverage

blib/lib/SWISH/Filters/Decompress.pm
Criterion Covered Total %
statement 26 55 47.2
branch 2 16 12.5
condition 0 2 0.0
subroutine 6 10 60.0
pod 1 5 20.0
total 35 88 39.7


line stmt bran cond sub pod time code
1             package SWISH::Filters::Decompress;
2 1     1   659 use strict;
  1         2  
  1         28  
3 1     1   5 use warnings;
  1         2  
  1         26  
4 1     1   4 use Carp;
  1         2  
  1         63  
5 1     1   5 use vars qw( $VERSION @ISA );
  1         1  
  1         54  
6             $VERSION = '0.190';
7             @ISA = ('SWISH::Filters::Base');
8 1     1   4 use SWISH::Filter::MIMETypes;
  1         2  
  1         548  
9              
10             my %mimes = (
11             'application/x-gzip' => 'gz',
12              
13             # deferred till we have way to deal with
14             # multiple docs in single file
15             #'application/x-compress' => 'zip',
16             );
17              
18             my %ext = reverse %mimes;
19              
20             sub new {
21 1     1 0 7663 my $class = shift;
22 1         5 my $self = bless( {}, $class );
23 1         1 my $ok;
24              
25 1         8 $self->{type} = 1;
26              
27 1         10 $self->{_mimetypes} = SWISH::Filter::MIMETypes->new;
28              
29             # set mimetypes etc. based on which modules/programs we have
30             # preference is to use Perl lib over binary cmd
31 1 50       10 if ( $self->use_modules(qw/ Compress::Zlib /) ) {
    0          
32 1         2 push( @{ $self->{mimetypes} }, qr!$ext{gz}! );
  1         18  
33 1         6 $self->{gz}->{perl}++;
34 1         3 $ok++;
35             }
36             elsif ( $self->find_binary('gunzip') ) {
37 0         0 $self->set_programs('gunzip');
38 0         0 push( @{ $self->{mimetypes} }, qr!$ext{gz}! );
  0         0  
39 0         0 $self->{gz}->{bin}++;
40 0         0 $ok++;
41             }
42              
43             # if ($self->use_modules(qw/ Archive::Zip /))
44             # {
45             # push(@{$self->{mimetypes}}, qr!$ext{zip}!);
46             # $self->{zip}->{perl}++;
47             # $ok++;
48             # }
49             # elsif ($self->find_binary('unzip'))
50             # {
51             # $self->set_programs('unzip');
52             # push(@{$self->{mimetypes}}, qr!$ext{zip}!);
53             # $self->{zip}->{bin}++;
54             # $ok++;
55             # }
56              
57 1 50       8 return $ok ? $self : undef;
58             }
59              
60             # TODO
61             sub zipinfo {
62 0     0 0   my $self = shift;
63 0 0         my $zfile = shift or croak "need zipfile";
64 0           my $i = $self->run_program( 'unzip', "-Z -1 $zfile" );
65 0   0       return split( /\n/, $i || '' );
66             }
67              
68             sub get_type {
69 0     0 0   my ( $self, $doc ) = @_;
70 0           ( my $name = $doc->name ) =~ s/\.(gz|zip)$//i;
71 0           $self->mywarn(" decompress: getting mime for $name");
72 0           return $self->{_mimetypes}->get_mime_type($name);
73             }
74              
75             sub decompress {
76 0     0 0   my ( $self, $doc ) = @_;
77              
78 0           my ( $buf, $status );
79              
80 0 0         if ( $self->{gz}->{perl} ) {
    0          
81 0           my $r = $doc->fetch_doc_reference;
82 0           $buf = Compress::Zlib::memGunzip($r);
83             }
84             elsif ( $self->{gz}->{bin} ) {
85 0           $buf = $self->run_program( 'gunzip', '-c', $doc->fetch_filename );
86             }
87              
88 0           $self->mywarn(" decompress: $doc was decompressed");
89              
90             #$self->mywarn(ref($buf) ? $$buf : $buf);
91              
92             # TODO .zip support
93              
94             # return a scalar ref
95 0 0         return ref($buf) ? $buf : \$buf;
96             }
97              
98             sub filter {
99 0     0 1   my ( $self, $doc ) = @_;
100              
101 0           my $buf = $self->decompress($doc); # returns scalar ref
102              
103 0 0         return undef unless $$buf;
104              
105 0           my $mime = $self->get_type($doc);
106              
107 0           $self->mywarn(
108             " decompress: " . $doc->name . " is now flagged as $mime" );
109              
110 0           $doc->set_content_type($mime);
111 0           $doc->set_continue(1);
112              
113             # return the document
114 0           return ( $buf, $doc->meta_data );
115             }
116              
117             1;
118              
119             __END__