File tree 7 files changed +27
-4
lines changed
7 files changed +27
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## unreleased
4
4
5
+ * Added ` cache_dir_permissions ` option to cache results [ @nathantsoi ] ( https://github.com/nathantsoi )
6
+
5
7
## v0.24.2 (2017-02-18)
6
8
7
9
* Describe ` nice ` level option [ #140 ] ( https://github.com/toy/image_optim/issues/140 ) [ @toy ] ( https://github.com/toy )
Original file line number Diff line number Diff line change @@ -270,6 +270,7 @@ optipng:
270
270
* `:skip_missing_workers` — Skip workers with missing or problematic binaries *(defaults to `false`)*
271
271
* `:allow_lossy` — Allow lossy workers and optimizations *(defaults to `false`)*
272
272
* `:cache_dir` — Configure cache directory
273
+ * `:cache_dir_mode` — Configure cache directory permissions mode per https://apidock.com/ruby/FileUtils/chmod
273
274
* `:cache_worker_digests` - Also cache worker digests along with original file digest and worker options: updating workers invalidates cache
274
275
275
276
Worker can be disabled by passing `false` instead of options hash or by setting option `:disable` to `true`.
Original file line number Diff line number Diff line change @@ -41,6 +41,9 @@ class ImageOptim
41
41
# Cache directory
42
42
attr_reader :cache_dir
43
43
44
+ # Cache directory permissions mode per https://apidock.com/ruby/FileUtils/chmod
45
+ attr_reader :cache_dir_mode
46
+
44
47
# Cache worker digests
45
48
attr_reader :cache_worker_digests
46
49
@@ -75,6 +78,7 @@ def initialize(options = {})
75
78
skip_missing_workers
76
79
allow_lossy
77
80
cache_dir
81
+ cache_dir_mode
78
82
cache_worker_digests
79
83
] . each do |name |
80
84
instance_variable_set ( :"@#{ name } " , config . send ( name ) )
@@ -106,7 +110,7 @@ def optimize_image(original)
106
110
return unless ( workers = workers_for_image ( original ) )
107
111
108
112
optimized = @cache . fetch ( original ) do
109
- Handler . for ( original ) do |handler |
113
+ Handler . for ( self , original ) do |handler |
110
114
workers . each do |worker |
111
115
handler . process do |src , dst |
112
116
worker . optimize ( src , dst )
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ class Cache
8
8
def initialize ( image_optim , workers_by_format )
9
9
return unless image_optim . cache_dir
10
10
@cache_dir = FSPath . new ( image_optim . cache_dir )
11
+ @cache_dir_mode = image_optim . cache_dir_mode
11
12
@cache_worker_digests = image_optim . cache_worker_digests
12
13
@options_by_format = Hash [ workers_by_format . map do |format , workers |
13
14
[ format , workers . map ( &:inspect ) . sort . join ( ', ' ) ]
@@ -33,6 +34,7 @@ def fetch(original)
33
34
34
35
if optimized
35
36
tmp = FSPath . temp_file_path ( digest , @cache_dir )
37
+ FileUtils . chmod ( @cache_dir_mode , tmp ) unless @cache_dir_mode . nil?
36
38
FileUtils . mv ( optimized , tmp )
37
39
tmp . rename ( cached )
38
40
cached_path = CachePath . convert ( cached )
Original file line number Diff line number Diff line change @@ -153,6 +153,11 @@ def cache_dir
153
153
dir unless dir . nil? || dir . empty?
154
154
end
155
155
156
+ def cache_dir_mode
157
+ dir_mode = get! ( :cache_dir_mode )
158
+ dir_mode unless dir_mode . nil?
159
+ end
160
+
156
161
def cache_worker_digests
157
162
!!get! ( :cache_worker_digests )
158
163
end
Original file line number Diff line number Diff line change @@ -7,19 +7,20 @@ class Handler
7
7
attr_reader :result
8
8
9
9
# original must respond to temp_path
10
- def initialize ( original )
10
+ def initialize ( image_optim , original )
11
11
unless original . respond_to? ( :temp_path )
12
12
fail ArgumentError , 'original should respond to temp_path'
13
13
end
14
14
15
+ @cache_dir_mode = image_optim . cache_dir_mode
15
16
@original = original
16
17
@result = nil
17
18
end
18
19
19
20
# with no associated block, works as new. Otherwise creates instance and
20
21
# passes it to block, runs cleanup and returns result of handler
21
- def self . for ( original )
22
- handler = new ( original )
22
+ def self . for ( image_optim , original )
23
+ handler = new ( image_optim , original )
23
24
if block_given?
24
25
begin
25
26
yield handler
@@ -38,6 +39,9 @@ def process
38
39
@src ||= @original
39
40
@dst ||= @original . temp_path
40
41
42
+ FileUtils . chmod ( @cache_dir_mode , @src ) unless @cache_dir_mode . nil?
43
+ FileUtils . chmod ( @cache_dir_mode , @dst ) unless @cache_dir_mode . nil?
44
+
41
45
return unless yield @src , @dst
42
46
@result = @dst
43
47
if @src == @original
Original file line number Diff line number Diff line change @@ -153,6 +153,11 @@ def wrap_regex(width)
153
153
options [ :cache_dir ] = cache_dir
154
154
end
155
155
156
+ op . on ( '--cache-dir-mode MODE' , 'Cache optimized images ' \
157
+ 'with the specified permissions mode' ) do |cache_dir |
158
+ options [ :cache_dir_mode ] = cache_dir_mode
159
+ end
160
+
156
161
op . on ( '--cache-worker-digests' , 'Cache worker digests ' \
157
162
'(updating workers invalidates cache)' ) do |cache_worker_digests |
158
163
options [ :cache_worker_digests ] = cache_worker_digests
You can’t perform that action at this time.
0 commit comments