Skip to content

Commit

Permalink
add a cache_dir_mode option to set the permission mode on the cached …
Browse files Browse the repository at this point in the history
…tmp files
  • Loading branch information
nathantsoi committed Apr 5, 2017
1 parent 7f77946 commit 5abb79b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## unreleased

* Added `cache_dir_permissions` option to cache results [@nathantsoi](https://github.com/nathantsoi)

## v0.24.2 (2017-02-18)

* Describe `nice` level option [#140](https://github.com/toy/image_optim/issues/140) [@toy](https://github.com/toy)
Expand Down
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ optipng:
* `:skip_missing_workers` — Skip workers with missing or problematic binaries *(defaults to `false`)*
* `:allow_lossy` — Allow lossy workers and optimizations *(defaults to `false`)*
* `:cache_dir` — Configure cache directory
* `:cache_dir_mode` — Configure cache directory permissions mode per https://apidock.com/ruby/FileUtils/chmod
* `:cache_worker_digests` - Also cache worker digests along with original file digest and worker options: updating workers invalidates cache

Worker can be disabled by passing `false` instead of options hash or by setting option `:disable` to `true`.
Expand Down
4 changes: 4 additions & 0 deletions lib/image_optim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ImageOptim
# Cache directory
attr_reader :cache_dir

# Cache directory permissions mode per https://apidock.com/ruby/FileUtils/chmod
attr_reader :cache_dir_mode

# Cache worker digests
attr_reader :cache_worker_digests

Expand Down Expand Up @@ -75,6 +78,7 @@ def initialize(options = {})
skip_missing_workers
allow_lossy
cache_dir
cache_dir_mode
cache_worker_digests
].each do |name|
instance_variable_set(:"@#{name}", config.send(name))
Expand Down
1 change: 1 addition & 0 deletions lib/image_optim/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def fetch(original)

if optimized
tmp = FSPath.temp_file_path(digest, @cache_dir)
FileUtils.chmod(@cache_dir_permissions, tmp) unless @cache_dir_permissions.nil?
FileUtils.mv(optimized, tmp)
tmp.rename(cached)
cached_path = CachePath.convert(cached)
Expand Down
5 changes: 5 additions & 0 deletions lib/image_optim/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def cache_dir
dir unless dir.nil? || dir.empty?
end

def cache_dir_mode
dir_mode = get!(:cache_dir_mode)
dir_mode unless dir_mode.nil?
end

def cache_worker_digests
!!get!(:cache_worker_digests)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/image_optim/runner/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def wrap_regex(width)
options[:cache_dir] = cache_dir
end

op.on('--cache-dir-mode MODE', 'Cache optimized images '\
'with the specified permissions mode') do |cache_dir|
options[:cache_dir_mode] = cache_dir_mode
end

op.on('--cache-worker-digests', 'Cache worker digests '\
'(updating workers invalidates cache)') do |cache_worker_digests|
options[:cache_worker_digests] = cache_worker_digests
Expand Down

0 comments on commit 5abb79b

Please sign in to comment.