Skip to content

Commit

Permalink
Fix file iterator bytesize fetching
Browse files Browse the repository at this point in the history
Neither Rack::Files nor Rack::File have ever responded to #map, but the respond to #each, so we should use that instead.

Also, on Rack >= 2.1, Rack::Files::Iterator#bytesize was introduced, avoiding the need to calculate it ourselves.
  • Loading branch information
davidalejandroaguilar committed Nov 3, 2023
1 parent bf43c6d commit 4193111
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
15 changes: 13 additions & 2 deletions lib/shrine/plugins/derivation_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,14 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "2.1"
headers["Content-Length"] ||= body.byesize.to_s
else
sum = 0
body.each { |chunk| sum += chunk.bytesize }

headers["Content-Length"] = sum.to_s
end

[status, headers, body]
end
Expand Down Expand Up @@ -444,7 +451,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 Rak.release > "2"
throw :halt, [status, {"content-type" => "text/plain"}, [message]]
else
throw :halt, [status, {"Content-Type" => "text/plain"}, [message]]
end
end

def secret_key
Expand Down
9 changes: 8 additions & 1 deletion lib/shrine/plugins/download_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "2.1"
headers["Content-Length"] ||= body.byesize.to_s
else
sum = 0
body.each { |chunk| sum += chunk.bytesize }

headers["Content-Length"] = sum.to_s
end

[status, headers, body]
end
Expand Down
9 changes: 8 additions & 1 deletion lib/shrine/plugins/presign_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ def call(env)
end
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "2.1"
headers["Content-Length"] ||= body.byesize.to_s
else
sum = 0
body.each { |chunk| sum += chunk.bytesize }

headers["Content-Length"] = sum.to_s
end

[status, headers, body]
end
Expand Down
9 changes: 8 additions & 1 deletion lib/shrine/plugins/upload_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ def call(env)
handle_request(request)
end

headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s
if Rack.release >= "2.1"
headers["Content-Length"] ||= body.byesize.to_s
else
sum = 0
body.each { |chunk| sum += chunk.bytesize }

headers["Content-Length"] = sum.to_s
end

[status, headers, body]
end
Expand Down

0 comments on commit 4193111

Please sign in to comment.