-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6447 from samvera/update_downloads_controller_spec
Valkyrize downloads_controller_spec; Improved Range and Cache-Control features
- Loading branch information
Showing
7 changed files
with
219 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/controllers/concerns/hyrax/stream_file_downloads_controller_behavior.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
# Overrides Hydra::Controller:DownloadBehavior handing of HEAD requests to | ||
# respond with same headers as a GET request would receive. | ||
module StreamFileDownloadsControllerBehavior | ||
protected | ||
|
||
# Handle the HTTP show request | ||
def send_content | ||
response.headers['Accept-Ranges'] = 'bytes' | ||
if request.headers['HTTP_RANGE'] | ||
send_range | ||
else | ||
send_file_contents | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize | ||
def send_range | ||
_, range = request.headers['HTTP_RANGE'].split('bytes=') | ||
from, to = range.split('-').map(&:to_i) | ||
to = file.size - 1 unless to | ||
length = to - from + 1 | ||
response.headers['Content-Range'] = "bytes #{from}-#{to}/#{file.size}" | ||
response.headers['Content-Length'] = length.to_s | ||
self.status = 206 | ||
prepare_file_headers | ||
|
||
if request.head? | ||
head status | ||
else | ||
stream_body file.stream(request.headers['HTTP_RANGE']) | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
|
||
def send_file_contents | ||
return unless stale?(last_modified: file_last_modified, template: false) | ||
|
||
self.status = 200 | ||
prepare_file_headers | ||
|
||
if request.head? | ||
head status | ||
else | ||
stream_body file.stream | ||
end | ||
end | ||
|
||
def file_last_modified | ||
file.modified_date | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.