From b5430c576ce9b1a71984407b9f13264840ee86f9 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 5 Apr 2017 15:32:33 -0700 Subject: [PATCH] add a cache_dir_mode option to set the permission mode on the cached tmp files --- CHANGELOG.markdown | 2 ++ README.markdown | 1 + lib/image_optim.rb | 6 +++++- lib/image_optim/cache.rb | 2 ++ lib/image_optim/config.rb | 5 +++++ lib/image_optim/handler.rb | 10 +++++++--- lib/image_optim/runner/option_parser.rb | 5 +++++ 7 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 24581287..d5668daf 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -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) diff --git a/README.markdown b/README.markdown index 67b2b6da..b15fc3ff 100644 --- a/README.markdown +++ b/README.markdown @@ -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`. diff --git a/lib/image_optim.rb b/lib/image_optim.rb index 3a3ff80c..e1a5b746 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -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 @@ -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)) @@ -106,7 +110,7 @@ def optimize_image(original) return unless (workers = workers_for_image(original)) optimized = @cache.fetch(original) do - Handler.for(original) do |handler| + Handler.for(self, original) do |handler| workers.each do |worker| handler.process do |src, dst| worker.optimize(src, dst) diff --git a/lib/image_optim/cache.rb b/lib/image_optim/cache.rb index 4e062e57..c12fbaad 100644 --- a/lib/image_optim/cache.rb +++ b/lib/image_optim/cache.rb @@ -8,6 +8,7 @@ class Cache def initialize(image_optim, workers_by_format) return unless image_optim.cache_dir @cache_dir = FSPath.new(image_optim.cache_dir) + @cache_dir_mode = image_optim.cache_dir_mode @cache_worker_digests = image_optim.cache_worker_digests @options_by_format = Hash[workers_by_format.map do |format, workers| [format, workers.map(&:inspect).sort.join(', ')] @@ -33,6 +34,7 @@ def fetch(original) if optimized tmp = FSPath.temp_file_path(digest, @cache_dir) + FileUtils.chmod(@cache_dir_mode, tmp) unless @cache_dir_mode.nil? FileUtils.mv(optimized, tmp) tmp.rename(cached) cached_path = CachePath.convert(cached) diff --git a/lib/image_optim/config.rb b/lib/image_optim/config.rb index 80d73922..f2b27ad5 100644 --- a/lib/image_optim/config.rb +++ b/lib/image_optim/config.rb @@ -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 diff --git a/lib/image_optim/handler.rb b/lib/image_optim/handler.rb index 63323ec3..6ba8d24c 100644 --- a/lib/image_optim/handler.rb +++ b/lib/image_optim/handler.rb @@ -7,19 +7,20 @@ class Handler attr_reader :result # original must respond to temp_path - def initialize(original) + def initialize(image_optim, original) unless original.respond_to?(:temp_path) fail ArgumentError, 'original should respond to temp_path' end + @cache_dir_mode = image_optim.cache_dir_mode @original = original @result = nil end # with no associated block, works as new. Otherwise creates instance and # passes it to block, runs cleanup and returns result of handler - def self.for(original) - handler = new(original) + def self.for(image_optim, original) + handler = new(image_optim, original) if block_given? begin yield handler @@ -38,6 +39,9 @@ def process @src ||= @original @dst ||= @original.temp_path + FileUtils.chmod(@cache_dir_mode, @src) unless @cache_dir_mode.nil? + FileUtils.chmod(@cache_dir_mode, @dst) unless @cache_dir_mode.nil? + return unless yield @src, @dst @result = @dst if @src == @original diff --git a/lib/image_optim/runner/option_parser.rb b/lib/image_optim/runner/option_parser.rb index 5f72bfa3..3e95acbb 100644 --- a/lib/image_optim/runner/option_parser.rb +++ b/lib/image_optim/runner/option_parser.rb @@ -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