line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::StaticAttachment; |
2
|
2
|
|
|
2
|
|
22956
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
10534
|
|
|
2
|
|
|
|
|
22
|
|
3
|
2
|
|
|
2
|
|
1354
|
use File::Basename; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
212
|
|
4
|
2
|
|
|
2
|
|
756
|
use Mojo::Util qw'quote decode url_unescape';# |
|
2
|
|
|
|
|
87565
|
|
|
2
|
|
|
|
|
1362
|
|
5
|
|
|
|
|
|
|
#~ use Encode qw( encode ); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has [qw'app']; |
8
|
|
|
|
|
|
|
has paths => sub { {}; }; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
1
|
|
|
1
|
1
|
53
|
my ($self, $app, $args) = @_; |
12
|
1
|
|
|
|
|
5
|
$self->app($app)->parse_paths(delete $args->{paths}); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#~ $app->log->debug( $app->dumper($self->paths) ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$app->hook(after_static => sub { |
17
|
2
|
|
|
2
|
|
22402
|
my $c = shift; |
18
|
2
|
|
|
|
|
9
|
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
|
50
|
33
|
|
|
201
|
unless my $conf = $self->paths->{$path} || $self->paths->{decode 'UTF-8', $path}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#~ warn $path, %$conf; |
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
|
|
55
|
my $headers = $c->res->content->headers; |
29
|
|
|
|
|
|
|
#~ $headers->add( 'Content-Type' => $content_type . ';name=' . $filename ); |
30
|
2
|
|
50
|
|
|
44
|
my $content_type = $conf->{content_type} || $headers->content_type || 'application/x-download'; |
31
|
|
|
|
|
|
|
#~ warn $conf->{filename}; |
32
|
2
|
|
|
|
|
28
|
$headers->content_type($content_type . ';name=' . $conf->{filename}); |
33
|
|
|
|
|
|
|
#~ $headers->add( 'Content-Disposition'=>'attachment;filename=' . $filename ); |
34
|
2
|
|
|
|
|
21
|
$headers->content_disposition('attachment;filename=' . $conf->{filename}); |
35
|
|
|
|
|
|
|
#~ $c->rendered; |
36
|
1
|
|
|
|
|
16
|
}); |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
24
|
return $self; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub parse_paths { |
43
|
1
|
|
|
1
|
0
|
12
|
my $self = shift; |
44
|
1
|
50
|
|
|
|
5
|
my $args = ref $_[0] ? shift : \@_; |
45
|
1
|
|
|
|
|
3
|
my $paths = $self->paths; |
46
|
1
|
|
|
|
|
7
|
while (my ($path, $conf) = splice(@$args,0,2)) { |
47
|
3
|
|
|
|
|
71
|
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
|
50
|
|
|
|
8
|
my $content_type = $self->app->types->type( $format ) |
54
|
|
|
|
|
|
|
if $format; |
55
|
|
|
|
|
|
|
#~ $content_type ||= 'application/x-download'; |
56
|
|
|
|
|
|
|
|
57
|
3
|
100
|
|
|
|
12
|
unless (ref $conf) { |
58
|
1
|
50
|
|
|
|
5
|
unshift @$args, $conf |
59
|
|
|
|
|
|
|
if $conf; |
60
|
1
|
|
|
|
|
3
|
$conf = {}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
3
|
|
66
|
|
|
16
|
$conf->{filename} ||= $filename; |
64
|
3
|
|
|
|
|
12
|
$conf->{filename} = quote $conf->{filename}; |
65
|
|
|
|
|
|
|
utf8::encode($conf->{filename}) |
66
|
3
|
100
|
|
|
|
36
|
if utf8::is_utf8($conf->{filename}); |
67
|
|
|
|
|
|
|
|
68
|
3
|
100
|
|
|
|
11
|
utf8::encode($path) |
69
|
|
|
|
|
|
|
if utf8::is_utf8($path); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
#~ warn $conf->{filename}; |
72
|
3
|
50
|
0
|
|
|
6
|
$conf->{content_type} ||= $content_type |
73
|
|
|
|
|
|
|
if $content_type; |
74
|
3
|
|
|
|
|
18
|
$paths->{$path} = $conf;#{encode 'UTF-8', $path} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} |
77
|
1
|
|
|
|
|
4
|
return $self; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
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 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
L |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Михаил Че (Mikhail Che), C<< >> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 BUGS / CONTRIBUTING |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Please report any bugs or feature requests at L. 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 |