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.
4 5 6 |
# File 'lib/rack_resize/processing.rb', line 4 def initialize(config:) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
2 3 4 |
# File 'lib/rack_resize/processing.rb', line 2 def config @config end |
Instance Method Details
#logger ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rack_resize/processing.rb', line 39 def logger @logger ||= begin if defined?(Rails) && Rails.logger Rails.logger else require 'logger' Logger.new(STDOUT) end end end |
#process!(source_file:, req_params:) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rack_resize/processing.rb', line 8 def process!(source_file:, req_params:) if config.save_resized? tmp_file_name = config.cache_folder.join(Digest::MD5.hexdigest(source_file.to_s) + File.extname(source_file)) 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}") return StringIO.new(File.open(tmp_file_name.to_s, "rb", &:read)) 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
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rack_resize/processing.rb', line 25 def process_file(source_file:, req_params:, target_file: nil) dpr = req_params[:dpr]&.to_f || 1.0 target_width = (req_params[:w]&.to_i || req_params[:width]&.to_i)&.*(dpr) target_height = (req_params[:h]&.to_i || req_params[:height]&.to_i)&.*(dpr) start_time = Time.now begin return config.processor_instance.resize(source_file:, target_file:, target_width:, target_height:) 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 |