| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::StaticCompressor::File; |
|
2
|
|
|
|
|
|
|
# Single-file cache container |
|
3
|
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
20
|
use strict; |
|
|
4
|
|
|
|
|
13
|
|
|
|
4
|
|
|
|
|
146
|
|
|
5
|
4
|
|
|
4
|
|
22
|
use warnings; |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
106
|
|
|
6
|
4
|
|
|
4
|
|
21
|
use utf8; |
|
|
4
|
|
|
|
|
14
|
|
|
|
4
|
|
|
|
|
25
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
81
|
use Encode; |
|
|
4
|
|
|
|
|
10
|
|
|
|
4
|
|
|
|
|
503
|
|
|
9
|
4
|
|
|
4
|
|
3068
|
use JavaScript::Minifier qw//; |
|
|
4
|
|
|
|
|
8141
|
|
|
|
4
|
|
|
|
|
105
|
|
|
10
|
4
|
|
|
4
|
|
3201
|
use CSS::Minifier qw//; |
|
|
4
|
|
|
|
|
4716
|
|
|
|
4
|
|
|
|
|
86
|
|
|
11
|
4
|
|
|
4
|
|
27
|
use Mojo::Asset::File; |
|
|
4
|
|
|
|
|
11
|
|
|
|
4
|
|
|
|
|
123
|
|
|
12
|
4
|
|
|
4
|
|
118
|
use Mojo::Util qw//; |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
4704
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
|
15
|
14
|
|
|
14
|
1
|
75
|
my ($class, %hash) = @_; |
|
16
|
14
|
|
|
|
|
49
|
my $s = bless({}, $class); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Initialize |
|
19
|
14
|
|
50
|
|
|
75
|
$s->{mojo_static} = $hash{config}->{mojo_static} || die('Not specified mojo_static'); |
|
20
|
14
|
|
50
|
|
|
72
|
$s->{path_cache_dir} = $hash{config}->{path_cache_dir} || die('Not specified path_cache_dir'); |
|
21
|
14
|
|
50
|
|
|
61
|
$s->{path_single_cache_dir} = $hash{config}->{path_single_cache_dir} || die('Not specified path_single_cache_dir'); |
|
22
|
|
|
|
|
|
|
|
|
23
|
14
|
|
|
|
|
32
|
$s->{file_key} = undef; |
|
24
|
14
|
|
50
|
|
|
48
|
$s->{path_file} = $hash{path_file} || die('Not specified path_file'); # Path of raw file |
|
25
|
14
|
|
50
|
|
|
51
|
$s->{extension} = $hash{extension} || die('Not specified extension'); |
|
26
|
14
|
|
100
|
|
|
66
|
$s->{is_minify} = $hash{is_minify} || 0; |
|
27
|
|
|
|
|
|
|
|
|
28
|
14
|
100
|
|
|
|
58
|
$s->{is_use_cache} = ($s->{is_minify}) ? 1 : 0; |
|
29
|
14
|
|
|
|
|
25
|
$s->{updated_at} = undef; # Updated_at of processed (epoch sec) |
|
30
|
14
|
|
|
|
|
26
|
$s->{content} = undef; # processed content |
|
31
|
14
|
|
|
|
|
26
|
$s->{path_cached_file} = undef; |
|
32
|
|
|
|
|
|
|
|
|
33
|
14
|
100
|
66
|
|
|
88
|
if(defined $s->{path_file} && $s->{is_use_cache}){ |
|
34
|
8
|
|
|
|
|
31
|
$s->_load_file(); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
14
|
|
|
|
|
301
|
return $s; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Generate the key of file |
|
41
|
|
|
|
|
|
|
sub get_key { |
|
42
|
19
|
|
|
19
|
1
|
64
|
my $s = shift; |
|
43
|
19
|
50
|
|
|
|
41
|
if($s->{is_use_cache}){ |
|
44
|
19
|
100
|
|
|
|
51
|
if(!defined $s->{file_key}){ |
|
45
|
8
|
|
|
|
|
36
|
$s->{file_key} = Mojo::Util::sha1_sum( $s->{path_file} ).'.'.$s->{extension}; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
19
|
|
|
|
|
182
|
return $s->{file_key}; |
|
48
|
|
|
|
|
|
|
} else { |
|
49
|
0
|
|
|
|
|
0
|
return; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Get the processed content |
|
54
|
|
|
|
|
|
|
sub get_content { |
|
55
|
4
|
|
|
4
|
1
|
5
|
my $s = shift; |
|
56
|
|
|
|
|
|
|
# Check for update of source file |
|
57
|
4
|
|
|
|
|
10
|
$s->_load_file(); |
|
58
|
4
|
|
|
|
|
115
|
return $s->{content}; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Get for updated_at |
|
62
|
|
|
|
|
|
|
sub get_updated_at { |
|
63
|
13
|
|
|
13
|
1
|
21
|
my $s = shift; |
|
64
|
|
|
|
|
|
|
# Check for update of source file |
|
65
|
13
|
|
|
|
|
59
|
$s->_load_file(); |
|
66
|
13
|
|
|
|
|
448
|
return $s->{updated_at}; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Get the path of file (raw) |
|
70
|
|
|
|
|
|
|
sub get_raw_path { |
|
71
|
11
|
|
|
11
|
1
|
19
|
my $s = shift; |
|
72
|
11
|
|
|
|
|
49
|
return $s->{path_file}; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Processing the file |
|
76
|
|
|
|
|
|
|
sub _process { |
|
77
|
0
|
|
|
0
|
|
0
|
my $s = shift; |
|
78
|
0
|
|
|
|
|
0
|
$s->_load_file(); |
|
79
|
0
|
0
|
|
|
|
0
|
if($s->{is_minify}){ |
|
80
|
0
|
|
|
|
|
0
|
$s->_minify(); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Check and make the single cache directory |
|
85
|
|
|
|
|
|
|
sub _check_cache_dir { |
|
86
|
19
|
|
|
19
|
|
27
|
my $s = shift; |
|
87
|
19
|
50
|
|
|
|
530
|
if(! -d $s->{path_single_cache_dir}){ |
|
88
|
|
|
|
|
|
|
# Make the directory |
|
89
|
0
|
0
|
|
|
|
0
|
mkdir( $s->{path_single_cache_dir} ) || die("Can't make a directory: ".$s->{path_single_cache_dir}); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Load content from the file and process |
|
94
|
|
|
|
|
|
|
sub _load_file { |
|
95
|
25
|
|
|
25
|
|
38
|
my $s = shift; |
|
96
|
25
|
100
|
|
|
|
123
|
if(! $s->{is_use_cache}){ |
|
97
|
6
|
|
|
|
|
7
|
my $asset; |
|
98
|
6
|
|
|
|
|
7
|
eval { |
|
99
|
6
|
|
|
|
|
34
|
$asset = $s->{mojo_static}->file($s->{path_file}); |
|
100
|
6
|
|
|
|
|
824
|
my $updated_at = (stat($asset->path()))[9]; |
|
101
|
6
|
50
|
33
|
|
|
168
|
if(!defined $s->{updated_at} || $s->{updated_at} < $updated_at){ # Is Updated |
|
102
|
6
|
|
|
|
|
23
|
$s->{content} = Encode::decode_utf8($asset->slurp()); |
|
103
|
6
|
|
|
|
|
791
|
$s->{updated_at} = $updated_at; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
6
|
50
|
|
|
|
56
|
}; if($@){ die ("Can't read static file: ". $s->{path_file} ."\n$@"); } |
|
|
0
|
|
|
|
|
0
|
|
|
106
|
6
|
|
|
|
|
21
|
return; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
19
|
|
|
|
|
46
|
$s->_check_cache_dir(); |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# Generate cache path |
|
112
|
19
|
|
|
|
|
56
|
$s->{path_cached_file} = $s->{path_single_cache_dir}.$s->get_key(); |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Load the file from cache |
|
115
|
19
|
50
|
|
|
|
55
|
if(defined $s->{path_cached_file}){ |
|
116
|
19
|
|
|
|
|
27
|
eval{ |
|
117
|
19
|
|
|
|
|
122
|
my $cache = Mojo::Asset::File->new( path => $s->{path_cached_file} ); |
|
118
|
19
|
|
|
|
|
679
|
$s->{updated_at} = (stat($s->{path_cached_file}))[9]; |
|
119
|
19
|
|
|
|
|
77
|
$s->{content} = $cache->slurp(); |
|
120
|
|
|
|
|
|
|
}; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Load the file and check for update |
|
124
|
19
|
|
|
|
|
2786
|
my ($asset, $updated_at, $raw_content); |
|
125
|
19
|
|
|
|
|
25
|
eval { |
|
126
|
19
|
|
|
|
|
92
|
$asset = $s->{mojo_static}->file($s->{path_file}); |
|
127
|
19
|
|
|
|
|
2369
|
$updated_at = (stat($asset->path()))[9]; |
|
128
|
19
|
|
|
|
|
547
|
$raw_content = Encode::decode_utf8($asset->slurp()); |
|
129
|
19
|
50
|
|
|
|
2701
|
}; if($@){ die ("Can't read static file: ".$asset->path()."\n$@"); } |
|
|
0
|
|
|
|
|
0
|
|
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Process and cache |
|
132
|
19
|
50
|
33
|
|
|
148
|
if(!defined $s->{updated_at} || $s->{updated_at} < $updated_at){ # Is Updated |
|
133
|
0
|
|
|
|
|
|
$s->{content} = $raw_content; |
|
134
|
0
|
|
|
|
|
|
$s->{updated_at} = $updated_at; |
|
135
|
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if($s->{is_minify}){ |
|
137
|
|
|
|
|
|
|
# Process the file |
|
138
|
0
|
|
|
|
|
|
$s->_minify(); |
|
139
|
|
|
|
|
|
|
# Cache to the file |
|
140
|
0
|
|
|
|
|
|
my $cache = Mojo::Asset::File->new(); |
|
141
|
0
|
|
|
|
|
|
$cache->add_chunk( Encode::encode_utf8($s->{content}) ); |
|
142
|
0
|
|
|
|
|
|
$cache->move_to( $s->{path_cached_file} ); |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Minify the content |
|
148
|
|
|
|
|
|
|
sub _minify { |
|
149
|
0
|
|
|
0
|
|
|
my $s = shift; |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
if($s->{extension} eq 'js'){ |
|
|
|
0
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
$s->{content} = JavaScript::Minifier::minify(input => $s->{content}); |
|
153
|
|
|
|
|
|
|
} elsif($s->{extension} eq 'css'){ |
|
154
|
0
|
|
|
|
|
|
$s->{content} = CSS::Minifier::minify(input => $s->{content}); |
|
155
|
|
|
|
|
|
|
} else { |
|
156
|
0
|
|
|
|
|
|
die('Not supported file type'); |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |
|
161
|
|
|
|
|
|
|
__END__ |
|
162
|
|
|
|
|
|
|
=head1 NAME |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Mojolicious::Plugin::StaticCompressor::File |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This is internal package that manipulate for single file. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Please see POD for L<Mojolicious::Plugin::StaticCompressor>. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor/blob/master/README.pod> |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 METHODS |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 new ( ... ) |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Initialize a instance for single file. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 get_key ( ) |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Get a cache key of the file. (If necessary, generate it.) |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 get_content ( ) |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Get the processed content of the file. |
|
187
|
|
|
|
|
|
|
(Check for updates of source files. And if necessary, update cache.) |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 get_updated_at ( ) |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Get te updated_at (epoch seconds) from of the file. |
|
192
|
|
|
|
|
|
|
(Check for updates of source files. And if necessary, update cache.) |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 get_raw_path ( ) |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Get a path of the source file. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
L<Mojolicious::Plugin::StaticCompressor> ( L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor> ) |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Please see POD for L<Mojolicious::Plugin::StaticCompressor>. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor/blob/master/README.pod> |