File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Pipe/Jpeg.pm
Criterion Covered Total %
statement 16 22 72.7
branch 2 6 33.3
condition 1 3 33.3
subroutine 4 5 80.0
pod 1 1 100.0
total 24 37 64.8


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Pipe::Jpeg;
2 1     1   8 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  1         2  
  1         10  
3              
4 1     1   74 use Mojolicious::Plugin::AssetPack::Util qw(diag DEBUG);
  1         2  
  1         540  
5              
6             has app => 'jpegoptim';
7             has app_args => sub {
8             my $self = shift;
9             return [DEBUG ? ('-v') : ('-q'), qw(-f --stdin --stdout)] if $self->app eq 'jpegoptim';
10             return [];
11             };
12              
13             sub process {
14 1     1 1 5 my ($self, $assets) = @_;
15 1         11 my $store = $self->assetpack->store;
16 1         9 my $file;
17              
18             return $assets->each(sub {
19 1     1   13 my ($asset, $index) = @_;
20 1         9 my $attrs = $asset->TO_JSON;
21 1         7 $attrs->{key} = sprintf '%s-min', $self->app;
22 1         14 $attrs->{minified} = 1;
23 1 50 33     3 return if $asset->format !~ /^jpe?g$/ or $asset->minified;
24 1 50       20 return unless $self->assetpack->minify;
25 0 0         return $asset->content($file)->minified(1) if $file = $store->load($attrs);
26 0           diag 'Process "%s", with checksum %s.', $asset->url, $attrs->{checksum} if DEBUG;
27 0           $asset->content($store->save($self->_run_app($asset), $attrs))->FROM_JSON($attrs);
28 1         14 });
29             }
30              
31             sub _install_jpegoptim {
32 0     0     my $self = shift;
33 0           my $class = ref $self;
34 0           die "$class requires https://github.com/tjko/jpegoptim";
35             }
36              
37             1;
38              
39             =encoding utf8
40              
41             =head1 NAME
42              
43             Mojolicious::Plugin::AssetPack::Pipe::Jpeg - Crush JPEG image files
44              
45             =head1 DESCRIPTION
46              
47             L can be used to crush "jpeg" image
48             files.
49              
50             This pipe is EXPERIMENTAL. Feedback wanted.
51              
52             =head1 ATTRIBUTES
53              
54             =head2 app
55              
56             $str = $self->app;
57             $self = $self->app("jpegoptim");
58              
59             Can be used to set a custom application instead of "jpegoptim".
60              
61             =head2 app_args
62              
63             $array = $self->app_args;
64             $self = $self->app_args([qw(-f --stdin --stdout)]);
65              
66             Can be used to set custom L arguments. The special C<$input> string in
67             the argument list will be replaced with the path to a temp file holding the
68             image data.
69              
70             If no C<$input> element is found in the L list, then STDIN and
71             STDOUT will be used instead.
72              
73             =head1 METHODS
74              
75             =head2 process
76              
77             See L.
78              
79             =head1 SEE ALSO
80              
81             L.
82              
83             =cut