| 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
Condition33%1/3
$self->paths->{$path} || $self->paths->{decode('UTF-8', $path)}
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)}
| |||||||||||||||||||||||||||||
| 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'
$conf->{"content_type"} or $headers->content_type
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 1 | my $paths = $self->paths; | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | while (my ($path, $conf) = splice(@$args,0,2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | if $format; | ||||||||||||||||||||||||||||
| 55 | #~ $content_type ||= 'application/x-download'; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | unless (ref $conf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | unshift @$args, $conf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | if $conf; | ||||||||||||||||||||||||||||
| 60 | 1 | $conf = {}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | $conf->{filename} ||= $filename; | ||||||||||||||||||||||||||
|
Condition66%2/3
$conf->{"filename"} ||= $filename
MC/DC0%0/2
$conf->{"filename"} ||= $filename
$conf->{"filename"}
$filename
Truth table66%2/3
A ||= B
A= $conf->{"filename"}B= $filename
| |||||||||||||||||||||||||||||
| 64 | 3 | $conf->{filename} = quote $conf->{filename}; | |||||||||||||||||||||||||||
| 65 | utf8::encode($conf->{filename}) | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if utf8::is_utf8($conf->{filename}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | utf8::encode($path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | if utf8::is_utf8($path); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #~ warn $conf->{filename}; | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | $conf->{content_type} ||= $content_type | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conf->{"content_type"} ||= $content_type
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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||