Class: RackResize::Processing
- Inherits:
-
Object
- Object
- RackResize::Processing
- Defined in:
- lib/rack_resize/processing.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize(config:) ⇒ Processing
constructor
A new instance of Processing.
- #logger ⇒ Object
- #process!(source_file:, req_params:) ⇒ Object
- #process_file(source_file:, req_params:, target_file: nil) ⇒ Object
Constructor Details
#initialize(config:) ⇒ Processing
Returns a new instance of Processing.
7 8 9 |
# File 'lib/rack_resize/processing.rb', line 7 def initialize(config:) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/rack_resize/processing.rb', line 5 def config @config end |
Instance Method Details
#logger ⇒ Object
57 58 59 60 61 |
# File 'lib/rack_resize/processing.rb', line 57 def logger config.logger || (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) || (@_logger ||= (require 'logger'; Logger.new($stdout))) end |
#process!(source_file:, req_params:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rack_resize/processing.rb', line 11 def process!(source_file:, req_params:) if config.save_resized? cache_key = Digest::MD5.hexdigest("#{source_file}:#{req_params.to_a.sort.to_s}") output_ext = output_extension(source_file, req_params[:format]) tmp_file_name = config.cache_folder.join(cache_key + output_ext) FileUtils.mkdir_p(tmp_file_name.dirname) unless tmp_file_name.exist? process_file(req_params:, source_file:, target_file: tmp_file_name.to_s) end logger.info("Serving cached file #{tmp_file_name}") begin return StringIO.new(File.open(tmp_file_name.to_s, "rb", &:read)) rescue Errno::ENOENT process_file(req_params:, source_file:, target_file: tmp_file_name.to_s) return StringIO.new(File.open(tmp_file_name.to_s, "rb", &:read)) end else file_content = process_file(req_params:, source_file:, target_file: nil) return StringIO.new(file_content) end end |
#process_file(source_file:, req_params:, target_file: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rack_resize/processing.rb', line 35 def process_file(source_file:, req_params:, target_file: nil) dpr = req_params[:dpr]&.to_f || 1.0 dpr = dpr.clamp(0.1, 10.0) max = config.max_dimension.to_f target_width = (req_params[:w]&.to_i || req_params[:width]&.to_i)&.*(dpr)&.clamp(1, max) target_height = (req_params[:h]&.to_i || req_params[:height]&.to_i)&.*(dpr)&.clamp(1, max) fit = req_params[:fit] format = req_params[:format].then { |f| f == 'auto' ? nil : f } quality = req_params[:quality]&.to_i&.clamp(1, 100) bg_color = RackResize::ColorUtils.parse_color(req_params[:"bg-color"] || req_params[:background]) start_time = Time.now begin return config.processor_instance.resize(source_file:, target_file:, target_width:, target_height:, fit:, format:, quality:, bg_color:) ensure processing_time = (Time.now.to_f - start_time.to_f).round(3) logger.info("RESIZE IMAGE #{config.processor} #{source_file} - #{req_params} - #{processing_time}s") end end |