Class: RackResize::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_resize/rack_app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options) ⇒ RackApp

Returns a new instance of RackApp.



7
8
9
10
11
12
13
14
15
# File 'lib/rack_resize/rack_app.rb', line 7

def initialize(*args, **options)
  if args.first
    @app = args.first
  end

  @config = options == {} ? RackResize.config : RackResize::Configuration.new(options)
  @processing = RackResize::Processing.new(config: @config)
  @cf_path_prefix = options[:cf_path_prefix] || "/cdn-cgi/image"
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/rack_resize/rack_app.rb', line 5

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack_resize/rack_app.rb', line 17

def call(env)
  request = Rack::Request.new(env)
  fullpath = request.path_info

  RackResize::InputParsers::Cloudflare.parse_input(fullpath, cf_path_prefix: @cf_path_prefix) =>
    {route_matched:, req_params:, asset_path:}

  return @app.call(env) unless route_matched
  return error_resp("can't parse file path") unless asset_path

  return error_resp("no assets folders configured") if config.assets_folders.nil? || config.assets_folders.empty?

  has_matched = false
  asset_file = nil
  config.assets_folders.each do |prefix, folder|
    if asset_path.start_with?(prefix)
      asset_file = folder.join(asset_path.delete_prefix(prefix + (prefix.end_with?("/") ? "" : "/")))
      if asset_file.expand_path.to_s.start_with?(folder.to_s)
        has_matched = true
      end
      break
    end
  end

  if asset_path.include?("..")
    @processing.logger.info("RackResize::RackApp - File path has invalid byte sequence #{asset_path}")
    return error_resp(".. is not allowed in image path")
  end
  unless has_matched
    @processing.logger.info("RackResize::RackApp - requested file #{asset_path} not match any of configured assets folder #{config.assets_folders.keys}")
    return error_resp("invalid file path")
  end
  unless asset_file.exist?
    @processing.logger.info("RackResize::RackApp - File path fond #{asset_path} => #{asset_file}")
    return error_resp("file not exists on a server")
  end

  file_content = @processing.process!(source_file: asset_file, req_params:)
  return send_file(asset_file:, file_content:)
end

#error_resp(message, http_code: 404) ⇒ Object



58
59
60
# File 'lib/rack_resize/rack_app.rb', line 58

def error_resp(message, http_code: 404)
  [http_code, {}, [message]]
end

#send_file(asset_file:, file_content: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rack_resize/rack_app.rb', line 62

def send_file(asset_file:, file_content: nil)
  content_type = Rack::Mime.mime_type(File.extname(asset_file), "application/octet-stream")

  [
    200,
    {
      "content-type"        => content_type,
      "content-length"      => file_content.size.to_s,
      "content-disposition" => "inline",
      "cache-control"       => "max-age=#{config.http_cache_max_age}" # 1 day
    },
    file_content
  ]
end