Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a cache_dir_mode option to set the permission mode on the cached … #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/image_optim/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')]
Expand All @@ -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)
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
10 changes: 7 additions & 3 deletions lib/image_optim/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,6 +39,9 @@ def process
@src ||= @original
@dst ||= @original.temp_path

FileUtils.chmod(@cache_dir_mode, @src) unless @cache_dir_mode.nil?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also had to set the permissions here, i think because temp_path was giving mode 600 files

FileUtils.chmod(@cache_dir_mode, @dst) unless @cache_dir_mode.nil?

return unless yield @src, @dst
@result = @dst
if @src == @original
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