Skip to content

Commit

Permalink
Rack 3 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasc committed Mar 31, 2024
1 parent bf43c6d commit 9edc34e
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 180 deletions.
57 changes: 45 additions & 12 deletions lib/shrine/plugins/derivation_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,20 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
headers ||= {}

[status, headers, body]
if Rack.release >= "3"
headers["content-length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
body.map(&:bytesize).inject(0, :+).to_s
else
headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
end

if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

# Verifies validity of the URL, then extracts parameters from it (such as
Expand Down Expand Up @@ -411,7 +422,11 @@ def handle_request(request)
headers["Cache-Control"] = derivation.option(:cache_control)
end

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

def inspect
Expand Down Expand Up @@ -444,7 +459,11 @@ def expires_in(request)

# Halts the request with the error message.
def error!(status, message)
throw :halt, [status, { "Content-Type" => "text/plain" }, [message]]
if Rack.release >= "3"
throw :halt, [status, { "content-type" => "text/plain" }, [message]]
else
throw :halt, [status, { "Content-Type" => "text/plain" }, [message]]
end
end

def secret_key
Expand Down Expand Up @@ -485,19 +504,33 @@ def file_response(file, env)

status = response[0]

headers = {
"Content-Type" => type || response[1]["Content-Type"],
"Content-Length" => response[1]["Content-Length"],
"Content-Disposition" => content_disposition(file),
"Content-Range" => response[1]["Content-Range"],
"Accept-Ranges" => "bytes",
}.compact
headers = if Rack.release >= "3"
{
"content-type" => type || response[1]["content-type"],
"content-length" => response[1]["content-length"],
"content-disposition" => content_disposition(file),
"content-range" => response[1]["content-range"],
"accept-ranges" => "bytes",
}.compact
else
{
"Content-Type" => type || response[1]["Content-Type"],
"Content-Length" => response[1]["Content-Length"],
"Content-Disposition" => content_disposition(file),
"Content-Range" => response[1]["Content-Range"],
"Accept-Ranges" => "bytes",
}.compact
end

body = Rack::BodyProxy.new(response[2]) { File.delete(file.path) }

file.close

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

# This is called when `:upload` is enabled. Checks the storage for already
Expand Down
19 changes: 16 additions & 3 deletions lib/shrine/plugins/download_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "3"
headers["content-length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
body.map(&:bytesize).inject(0, :+).to_s
else
headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
end

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

def inspect
Expand Down Expand Up @@ -197,7 +206,11 @@ def bad_request!(message)

# Halts the request with the error message.
def error!(status, message)
throw :halt, [status, { "Content-Type" => "text/plain" }, [message]]
if Rack.release >= "3"
throw :halt, [status, { "content-type" => "text/plain" }, [message]]
else
throw :halt, [status, { "Content-Type" => "text/plain" }, [message]]
end
end
end
end
19 changes: 16 additions & 3 deletions lib/shrine/plugins/presign_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ def call(env)
end
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "3"
headers["content-length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
body.map(&:bytesize).inject(0, :+).to_s
else
headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
end

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

def inspect
Expand Down Expand Up @@ -172,7 +181,11 @@ def make_response(object, request)

# Used for early returning an error response.
def error!(status, message)
throw :halt, [status, { "Content-Type" => CONTENT_TYPE_TEXT }, [message]]
if Rack.release >= "3"
throw :halt, [status, { "content-type" => CONTENT_TYPE_TEXT }, [message]]
else
throw :halt, [status, { "Content-Type" => CONTENT_TYPE_TEXT }, [message]]
end
end

# Returns the uploader around the specified storage.
Expand Down
10 changes: 9 additions & 1 deletion lib/shrine/plugins/rack_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def call(**options)
headers = rack_headers(**options)
body = rack_body(**options)

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

private
Expand Down Expand Up @@ -141,6 +145,10 @@ def close
file.close
end

def bytesize
each.inject(0) { |sum, chunk| sum += chunk.length }
end

# Rack::Sendfile is activated when response body responds to #to_path.
def respond_to_missing?(name, include_private = false)
name == :to_path && path
Expand Down
19 changes: 16 additions & 3 deletions lib/shrine/plugins/upload_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "3"
headers["content-length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
body.map(&:bytesize).inject(0, :+).to_s
else
headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
end

[status, headers, body]
if Rack.release >= "3"
[status, headers.transform_keys(&:downcase), body]
else
[status, headers, body]
end
end

def inspect
Expand Down Expand Up @@ -210,7 +219,11 @@ def verify_checksum!(file, request)

# Used for early returning an error response.
def error!(status, message)
throw :halt, [status, { "Content-Type" => CONTENT_TYPE_TEXT }, [message]]
if Rack.release >= "3"
throw :halt, [status, { "content-type" => CONTENT_TYPE_TEXT }, [message]]
else
throw :halt, [status, { "Content-Type" => CONTENT_TYPE_TEXT }, [message]]
end
end

# Returns the uploader around the specified storage.
Expand Down
2 changes: 1 addition & 1 deletion shrine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ direct uploads for fully asynchronous user experience.
gem.add_development_dependency "mocha", "~> 1.11"

# for endpoint plugins
gem.add_development_dependency "rack", "~> 2.0"
gem.add_development_dependency "rack", ">= 2", "< 4"
gem.add_development_dependency "http-form_data", "~> 2.2"
gem.add_development_dependency "rack-test_app"

Expand Down
Loading

0 comments on commit 9edc34e

Please sign in to comment.