line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Images::Util; |
2
|
9
|
|
|
9
|
|
674209
|
use Mojo::Base -base; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
55
|
|
3
|
9
|
|
|
9
|
|
1301
|
use 5.20.0; |
|
9
|
|
|
|
|
32
|
|
|
9
|
|
|
|
|
303
|
|
4
|
9
|
|
|
9
|
|
940
|
use experimental 'signatures'; |
|
9
|
|
|
|
|
1276
|
|
|
9
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
9
|
|
|
9
|
|
850
|
use Exporter 'import'; |
|
9
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
245
|
|
7
|
9
|
|
|
9
|
|
1897
|
use IO::All; |
|
9
|
|
|
|
|
33191
|
|
|
9
|
|
|
|
|
54
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = ( |
10
|
|
|
|
|
|
|
qw(install_route expand_static calc_static plugin_log check_dir check_id)); |
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $INSECURE_IDS = $ENV{IMAGES_ALLOW_INSECURE_IDS}; |
14
|
|
|
|
|
|
|
my $INSECURE_DIRS = $ENV{IMAGES_ALLOW_INSECURE_DIRS}; |
15
|
|
|
|
|
|
|
|
16
|
69
|
50
|
|
69
|
1
|
5333
|
sub check_dir($dir, $app) { |
|
69
|
50
|
|
|
|
129
|
|
|
69
|
|
|
|
|
86
|
|
|
69
|
|
|
|
|
70
|
|
|
69
|
|
|
|
|
61
|
|
17
|
69
|
100
|
|
|
|
155
|
return $dir if $INSECURE_DIRS; |
18
|
50
|
|
|
|
|
51
|
my $msg |
19
|
50
|
|
100
|
|
|
190
|
= "insecure dir ${\ ( $dir // 'undef' ) }, set IMAGES_ALLOW_INSECURE_DIRS env to allow it"; |
20
|
50
|
100
|
100
|
|
|
1088
|
die $msg if !$dir || Mojo::Path->new($app->home)->contains("$dir"); |
21
|
31
|
|
|
|
|
5363
|
return $dir; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# maybe too hard |
25
|
72
|
50
|
|
72
|
1
|
141
|
sub check_id($id) { |
|
72
|
50
|
|
|
|
124
|
|
|
72
|
|
|
|
|
68
|
|
|
72
|
|
|
|
|
82
|
|
26
|
72
|
100
|
|
|
|
181
|
return $id if $INSECURE_IDS; |
27
|
50
|
100
|
|
|
|
359
|
die "insecure id $id, set IMAGES_ALLOW_INSECURE_IDS env to allow it" |
28
|
|
|
|
|
|
|
unless $id =~ /^[\ \-\w\/]+$/; |
29
|
45
|
|
|
|
|
273
|
return $id; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
45
|
50
|
|
45
|
1
|
1848
|
sub plugin_log($app, $tmpl, @args) { |
|
45
|
|
|
|
|
56
|
|
|
45
|
|
|
|
|
49
|
|
|
45
|
|
|
|
|
129
|
|
|
45
|
|
|
|
|
51
|
|
33
|
45
|
|
|
|
|
145
|
my $msg = sprintf($tmpl, @args); |
34
|
45
|
|
|
|
|
1010
|
$app->log->debug("Mojolicious::Plugin::Images $msg"); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
45
|
50
|
|
45
|
1
|
19893
|
sub calc_static($dir, $url_prefix, $home) { |
|
45
|
50
|
|
|
|
125
|
|
|
45
|
|
|
|
|
64
|
|
|
45
|
|
|
|
|
53
|
|
|
45
|
|
|
|
|
49
|
|
|
45
|
|
|
|
|
39
|
|
38
|
45
|
100
|
|
|
|
99
|
return unless defined $url_prefix; |
39
|
|
|
|
|
|
|
|
40
|
43
|
|
|
|
|
131
|
$dir = io->dir($dir)->canonpath; |
41
|
43
|
100
|
|
|
|
63823
|
$dir = $home->rel_dir($dir) unless io->dir($dir)->is_absolute; |
42
|
|
|
|
|
|
|
|
43
|
43
|
|
|
|
|
6466
|
my $prefix = Mojo::Path->new($url_prefix)->canonicalize->to_route; |
44
|
43
|
|
|
|
|
5881
|
$prefix = Mojo::Path->new($prefix)->leading_slash(0)->trailing_slash(0); |
45
|
|
|
|
|
|
|
|
46
|
43
|
100
|
|
|
|
2938
|
$dir =~ s/$prefix$// or return; |
47
|
|
|
|
|
|
|
|
48
|
31
|
|
|
|
|
1998
|
io->dir($dir)->canonpath; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
34
|
50
|
|
34
|
1
|
21078
|
sub expand_static($dir, $url_prefix, $app) { |
|
34
|
50
|
|
|
|
75
|
|
|
34
|
|
|
|
|
51
|
|
|
34
|
|
|
|
|
43
|
|
|
34
|
|
|
|
|
37
|
|
|
34
|
|
|
|
|
33
|
|
52
|
34
|
100
|
|
|
|
764
|
my $static_path = calc_static($dir, $url_prefix, $app->home) or return; |
53
|
23
|
|
|
|
|
4081
|
$static_path = check_dir($static_path, $app); |
54
|
20
|
|
|
|
|
423
|
my $paths = $app->static->paths; |
55
|
20
|
100
|
|
|
|
526
|
push @$paths, $static_path unless grep { $_ eq $static_path } @$paths; |
|
30
|
|
|
|
|
158
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# install route /$prefix/(*key)-$suffix.$ext |
59
|
21
|
50
|
|
21
|
1
|
55
|
sub install_route($app, $moniker, $opts) { |
|
21
|
50
|
|
|
|
52
|
|
|
21
|
|
|
|
|
35
|
|
|
21
|
|
|
|
|
44
|
|
|
21
|
|
|
|
|
27
|
|
|
21
|
|
|
|
|
17
|
|
60
|
21
|
|
|
|
|
162
|
my $placeholder = '(*images_id)'; |
61
|
21
|
|
50
|
|
|
55
|
my $end = $opts->{suffix} // ''; |
62
|
21
|
100
|
|
|
|
52
|
$end .= ".${\$opts->{ext}}" if $opts->{ext}; |
|
20
|
|
|
|
|
43
|
|
63
|
21
|
|
|
|
|
110
|
my $path = Mojo::Path->new($opts->{url_prefix})->leading_slash(1) |
64
|
|
|
|
|
|
|
->trailing_slash(1)->merge("${placeholder}${end}"); |
65
|
|
|
|
|
|
|
|
66
|
21
|
|
|
|
|
5130
|
plugin_log($app, "Installing route GET $path for Images '$moniker'"); |
67
|
21
|
|
|
0
|
|
1231
|
$app->routes->get("$path")->to(cb => sub { _action(shift, $moniker) }); |
|
0
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
0
|
|
|
sub _action($c, $moniker) { |
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $id = $c->stash('images_id'); |
72
|
0
|
|
|
|
|
|
my $img = $c->images->$moniker; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
plugin_log($c->app, "Generating $moniker: $id"); |
75
|
0
|
0
|
0
|
|
|
|
return $c->reply->static($img->url($id)) |
76
|
|
|
|
|
|
|
if $img->exists($id) || $img->sync($id); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return $c->reply->not_found; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
__END__ |