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