Class: RackResize::RackApp
- Inherits:
-
Object
- Object
- RackResize::RackApp
- Defined in:
- lib/rack_resize/rack_app.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #error_resp(message, http_code: 404) ⇒ Object
-
#initialize(*args, **options) ⇒ RackApp
constructor
A new instance of RackApp.
- #send_file(asset_file:, file_content: nil) ⇒ Object
Constructor Details
#initialize(*args, **options) ⇒ RackApp
Returns a new instance of RackApp.
9 10 11 12 13 14 15 16 17 |
# File 'lib/rack_resize/rack_app.rb', line 9 def initialize(*args, **) if args.first @app = args.first end @config = == {} ? RackResize.config : RackResize::Configuration.new() @processing = RackResize::Processing.new(config: @config) @cf_path_prefix = [:cf_path_prefix] || "/cdn-cgi/image" end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/rack_resize/rack_app.rb', line 7 def config @config end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/rack_resize/rack_app.rb', line 19 def call(env) request = Rack::Request.new(env) fullpath = request.path_info unless fullpath.start_with?(@cf_path_prefix) return @app.call(env) end # process string like this # /cdn-cgi/image/width=426,format=auto/assets/templates/vancouver-27c47f55.jpg fullpath = fullpath.delete_prefix("/cdn-cgi").delete_prefix("/image").delete_prefix("/") file_path_match = fullpath.match(%r{(?<params>[^\/]+)(?<file>\/.+?)(-[\da-f]{8})?(?<ext>\.\w{2,})$}) return error_resp("can't parse file path") unless file_path_match req_params = file_path_match[:params].split(",").map {|s| s.split("=") }.to_h.transform_keys(&:to_sym) asset_path = "#{file_path_match[:file]}#{file_path_match[:ext]}" if defined?(Rails) asset_path = asset_path.delete_prefix("/assets/") else asset_path = asset_path.delete_prefix("/#{config.assets_folder}/") end asset_file = config.assets_folder.join(asset_path.sub(%r{^/assets/}, '')) return error_resp(".. is not allowed in image path") if asset_path.include?("..") return error_resp("invalid file path") unless asset_file.to_s.start_with?(config.assets_folder.to_s) return error_resp("file not exists on a server") unless asset_file.exist? 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
55 56 57 |
# File 'lib/rack_resize/rack_app.rb', line 55 def error_resp(, http_code: 404) [http_code, {}, []] end |
#send_file(asset_file:, file_content: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rack_resize/rack_app.rb', line 59 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 |