line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::StaticAttachment; |
2
|
2
|
|
|
2
|
|
13802
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
6614
|
|
|
2
|
|
|
|
|
14
|
|
3
|
2
|
|
|
2
|
|
820
|
use File::Basename; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
120
|
|
4
|
2
|
|
|
2
|
|
482
|
use Mojo::Util qw'quote encode decode url_unescape';# |
|
2
|
|
|
|
|
69522
|
|
|
2
|
|
|
|
|
987
|
|
5
|
|
|
|
|
|
|
#~ use Encode qw( encode ); |
6
|
|
|
|
|
|
|
#~ use URI::Escape; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has [qw'app']; |
9
|
|
|
|
|
|
|
has paths => sub { {}; }; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
30
|
my ($self, $app, $args) = @_; |
13
|
1
|
|
|
|
|
2
|
$self->app($app)->parse_paths(delete $args->{paths}); |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
7
|
$app->log->debug( $app->dumper($self->paths) ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$app->hook(after_static => sub { |
18
|
2
|
|
|
2
|
|
12312
|
my $c = shift; |
19
|
2
|
|
|
|
|
5
|
my $path = url_unescape $c->req->url->path; |
20
|
|
|
|
|
|
|
#~ utf8::encode($path) |
21
|
|
|
|
|
|
|
#~ unless utf8::is_utf8($path); |
22
|
|
|
|
|
|
|
#~ warn url_unescape $path; |
23
|
|
|
|
|
|
|
#~ $app->log->debug($path); |
24
|
|
|
|
|
|
|
return |
25
|
2
|
50
|
33
|
|
|
109
|
unless my $conf = $self->paths->{$path} || $self->paths->{decode 'UTF-8', $path}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#~ warn $path, %$conf; |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
|
|
27
|
my $headers = $c->res->content->headers; |
30
|
|
|
|
|
|
|
#~ $headers->add( 'Content-Type' => $content_type . ';name=' . $filename ); |
31
|
2
|
|
50
|
|
|
29
|
my $content_type = $conf->{content_type} || $headers->content_type || 'application/x-download'; |
32
|
|
|
|
|
|
|
#~ warn $conf->{filename}; |
33
|
2
|
|
|
|
|
14
|
$headers->content_type($content_type . ';name=' . $conf->{filename}); |
34
|
|
|
|
|
|
|
#~ $headers->add( 'Content-Disposition'=>'attachment;filename=' . $filename ); |
35
|
2
|
|
|
|
|
12
|
$headers->content_disposition('attachment;filename=' . $conf->{filename}); |
36
|
|
|
|
|
|
|
#~ $c->rendered; |
37
|
1
|
|
|
|
|
423
|
}); |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
12
|
return $self; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub parse_paths { |
44
|
1
|
|
|
1
|
0
|
9
|
my $self = shift; |
45
|
1
|
50
|
|
|
|
3
|
my $args = ref $_[0] ? shift : \@_; |
46
|
1
|
|
|
|
|
2
|
my $paths = $self->paths; |
47
|
1
|
|
|
|
|
4
|
while (my ($path, $conf) = splice(@$args,0,2)) { |
48
|
3
|
|
|
|
|
39
|
my($filename, $dirs, $format) = fileparse($path); |
49
|
|
|
|
|
|
|
#~ utf8::decode($filename);# if !utf8::is_utf8($filename); |
50
|
|
|
|
|
|
|
#~ utf8::decode($path) if !utf8::is_utf8($path); |
51
|
|
|
|
|
|
|
#~ $filename = quote $filename; # quote the filename, per RFC 5987 |
52
|
|
|
|
|
|
|
#~ $filename = decode $filename; |
53
|
|
|
|
|
|
|
#~ unless utf8::is_utf8($filename); |
54
|
3
|
50
|
|
|
|
7
|
my $content_type = $self->app->types->type( $format ) |
55
|
|
|
|
|
|
|
if $format; |
56
|
|
|
|
|
|
|
#~ $content_type ||= 'application/x-download'; |
57
|
|
|
|
|
|
|
|
58
|
3
|
100
|
|
|
|
6
|
unless (ref $conf) { |
59
|
1
|
50
|
|
|
|
4
|
unshift @$args, $conf |
60
|
|
|
|
|
|
|
if $conf; |
61
|
1
|
|
|
|
|
1
|
$conf = {}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
66
|
|
|
10
|
$conf->{filename} ||= $filename; |
65
|
3
|
|
|
|
|
7
|
$conf->{filename} = quote $conf->{filename}; |
66
|
|
|
|
|
|
|
utf8::encode($conf->{filename}) |
67
|
3
|
100
|
|
|
|
21
|
if utf8::is_utf8($conf->{filename}); |
68
|
|
|
|
|
|
|
|
69
|
3
|
100
|
|
|
|
6
|
utf8::encode($path) |
70
|
|
|
|
|
|
|
if utf8::is_utf8($path); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#~ warn $conf->{filename}; |
73
|
3
|
50
|
0
|
|
|
4
|
$conf->{content_type} ||= $content_type |
74
|
|
|
|
|
|
|
if $content_type; |
75
|
3
|
|
|
|
|
9
|
$paths->{$path} = $conf;#{encode 'UTF-8', $path} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
1
|
|
|
|
|
1
|
return $self; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
our $VERSION=0.002; # End of Mojolicious::Plugin::StaticAttachment |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=pod |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=encoding utf8 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Доброго всем |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 Mojolicious::Plugin::StaticAttachment |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 NAME |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Mojolicious::Plugin::StaticAttachment - Add 'Content-Disposition' header for specified statics. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Mojolicious plugin. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 SYNOPSIS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$app->plugin('StaticAttachment' |
105
|
|
|
|
|
|
|
=> paths => [ |
106
|
|
|
|
|
|
|
'/foo.txt', |
107
|
|
|
|
|
|
|
'bar.portable.doc.format'=>{content_type=>'application/pdf', filename=>"файлик.pdf"}]) |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 VERSION |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Version 0.002 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 SEE ALSO |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
L |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 AUTHOR |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Михаил Че (Mikhail Che), C<< >> |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 BUGS / CONTRIBUTING |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Please report any bugs or feature requests at L. Pull requests also welcome. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 COPYRIGHT |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Copyright 2017 Mikhail Che. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
132
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |