blib/lib/Mojolicious/Plugin/StaticAttachment.pm

filter: Statementstmt 100% 40 / 40 Branchbran 72.2% 13 / 18 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 75.2% 67 / 89 CC 15 SCAR 20.7
CC
15
Cov
82%
parse_paths
12.1
__ANON__
5.6
register
1.0
CRAP 7.9
↑ Summary
1 package Mojolicious::Plugin::StaticAttachment;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::Util qw'quote decode url_unescape';#
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #~ use Encode qw( encode );
6  
7 2 has [qw'app'];
8 1 ▶has paths => sub { {}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.1
Pod documented
11 1 my ($self, $app, $args) = @_;
12 1 $self->app($app)->parse_paths(delete $args->{paths});
13
14 #~ $app->log->debug( $app->dumper($self->paths) );
15
16 2 ▶ $app->hook(after_static => sub {
Subroutine __ANON__: called CC=5 SCAR=17.3
17 2 my $c = shift;
18 2 my $path = url_unescape $c->req->url->path;
19 #~ utf8::encode($path)
20 #~ unless utf8::is_utf8($path);
21 #~ warn url_unescape $path;
22 #~ $app->log->debug($path);
23 return
24 2 ▶ unless my $conf = $self->paths->{$path} || $self->paths->{decode 'UTF-8', $path};
Branch: 1 branch
BranchTrueFalseTotal
unless my $conf = $self->paths->{$path} || $self->paths->{decode("UTF-8", $path)} 0 2 2
Condition33%1/3
$self->paths->{$path} || $self->paths->{decode('UTF-8', $path)}
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->paths->{$path} || $self->paths->{decode('UTF-8', $path)}
$self->paths->{$path} $self->paths->{decode('UTF-8', $path)}
Truth table33%1/3
A || B
A= $self->paths->{$path}B= $self->paths->{decode('UTF-8', $path)}
ABresult
1X1
011
000
25
26 #~ warn $path, %$conf;
27
28 2 my $headers = $c->res->content->headers;
29 #~ $headers->add( 'Content-Type' => $content_type . ';name=' . $filename );
30 2 ▶ my $content_type = $conf->{content_type} || $headers->content_type || 'application/x-download';
Condition60%3/5
$conf->{'content_type'} || $headers->content_type || 'application/x-download'
l!l
20
$conf->{"content_type"} or $headers->content_type
l!l&&r!l&&!r
110
MC/DC0%0/2
$conf->{'content_type'} || $headers->content_type || 'application/x-download'
$conf->{"content_type"} $headers->content_type
Truth table66%2/3
A or B
A= $conf->{"content_type"}B= $headers->content_type
ABresult
1X1
011
000
31 #~ warn $conf->{filename};
32 2 $headers->content_type($content_type . ';name=' . $conf->{filename});
33 #~ $headers->add( 'Content-Disposition'=>'attachment;filename=' . $filename );
34 2 $headers->content_disposition('attachment;filename=' . $conf->{filename});
35 #~ $c->rendered;
36 1 });
37
38 1 return $self;
39
40 }
41  
42 1 ▶sub parse_paths {
Subroutine parse_paths: called CC=11 SCAR=25.0
Pod undocumented
43 1 my $self = shift;
44 1 ▶ my $args = ref $_[0] ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
45 1 my $paths = $self->paths;
46 1 ▶ while (my ($path, $conf) = splice(@$args,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$args, 0, 2)) 3 1 4
47 3 my($filename, $dirs, $format) = fileparse($path);
48 #~ utf8::decode($filename);# if !utf8::is_utf8($filename);
49 #~ utf8::decode($path) if !utf8::is_utf8($path);
50 #~ $filename = quote $filename; # quote the filename, per RFC 5987
51 #~ $filename = decode $filename;
52 #~ unless utf8::is_utf8($filename);
53 3 ▶ my $content_type = $self->app->types->type( $format )
Branch: 1 branch
BranchTrueFalseTotal
if $format 0 3 3
54 if $format;
55 #~ $content_type ||= 'application/x-download';
56
57 3 ▶ unless (ref $conf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $conf) 1 2 3
58 1 ▶ unshift @$args, $conf
Branch: 1 branch
BranchTrueFalseTotal
if $conf 1 0 1
59 if $conf;
60 1 $conf = {};
61 }
62
63 3 ▶ $conf->{filename} ||= $filename;
Condition66%2/3
$conf->{"filename"} ||= $filename
l!l&&r!l&&!r
120
MC/DC0%0/2
$conf->{"filename"} ||= $filename
$conf->{"filename"} $filename
Truth table66%2/3
A ||= B
A= $conf->{"filename"}B= $filename
ABresult
1X1
011
000
64 3 $conf->{filename} = quote $conf->{filename};
65 utf8::encode($conf->{filename})
66 3 ▶ if utf8::is_utf8($conf->{filename});
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($conf->{"filename"}) 2 1 3
67
68 3 ▶ utf8::encode($path)
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($path) 1 2 3
69 if utf8::is_utf8($path);
70
71 #~ warn $conf->{filename};
72 3 ▶ $conf->{content_type} ||= $content_type
Branch: 1 branch
BranchTrueFalseTotal
if $content_type 0 3 3
Condition0%0/3
$conf->{"content_type"} ||= $content_type
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{"content_type"} ||= $content_type
$conf->{"content_type"} $content_type
Truth table0%0/3
A ||= B
A= $conf->{"content_type"}B= $content_type
ABresult
1X1
011
000
73 if $content_type;
74 3 $paths->{$path} = $conf;#{encode 'UTF-8', $path}
75
76 }
77 1 return $self;
78
79 }
80  
81 2 our $VERSION=0.003; # End of Mojolicious::Plugin::StaticAttachment
82  
83 =pod
84  
85 =encoding utf8
86  
87 Доброго всем
88  
89 =head1 Mojolicious::Plugin::StaticAttachment
90  
91 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
92  
93 =head1 NAME
94  
95 Mojolicious::Plugin::StaticAttachment - Add 'Content-Disposition' header for specified statics.
96  
97 =head1 DESCRIPTION
98  
99 Mojolicious plugin.
100  
101 =head1 SYNOPSIS
102  
103 $app->plugin('StaticAttachment'
104 => paths => [
105 '/foo.txt',
106 'bar.portable.doc.format'=>{content_type=>'application/pdf', filename=>"файлик.pdf"}])
107  
108 =head1 VERSION
109  
110 Version 0.003
111  
112 =head1 SEE ALSO
113  
114 L<Mojolicious>
115  
116 L<Mojolicious::Plugin::RenderFile>
117  
118 =head1 AUTHOR
119  
120 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
121  
122 =head1 BUGS / CONTRIBUTING
123  
124 Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-StaticAttachment/issues>. Pull requests also welcome.
125  
126 =head1 COPYRIGHT
127  
128 Copyright 2017 Mikhail Che.
129  
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
132  
133 =cut
↑ Summary