Module: Bulkrax::DownloadBehavior
- Included in:
- ExportersController, ImportersController
- Defined in:
- app/controllers/concerns/bulkrax/download_behavior.rb
Instance Method Summary collapse
-
#content_head ⇒ Object
render an HTTP HEAD response.
-
#content_options ⇒ Object
Create some headers for the datastream.
- #download_content_type ⇒ Object
-
#file ⇒ Object
The following download code is based on github.com/samvera/hydra-head/blob/main/hydra-core/app/controllers/concerns/hydra/controller/download_behavior.rb.
-
#file_name ⇒ String
Override this if you’d like a different filename.
- #prepare_file_headers ⇒ Object
- #send_content ⇒ Object
- #send_file_contents ⇒ Object
Instance Method Details
#content_head ⇒ Object
render an HTTP HEAD response
37 38 39 40 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 37 def content_head response.headers['Content-Length'] = file.size head :ok, content_type: download_content_type end |
#content_options ⇒ Object
Create some headers for the datastream
32 33 34 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 32 def { disposition: 'attachment', type: download_content_type, filename: file_name } end |
#download_content_type ⇒ Object
18 19 20 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 18 def download_content_type 'application/zip' end |
#file ⇒ Object
The following download code is based on github.com/samvera/hydra-head/blob/main/hydra-core/app/controllers/concerns/hydra/controller/download_behavior.rb
8 9 10 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 8 def file @file ||= File.open(file_path, 'r') end |
#file_name ⇒ String
Override this if you’d like a different filename
14 15 16 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 14 def file_name file_path.split('/').last end |
#prepare_file_headers ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 48 def prepare_file_headers send_file_headers! response.headers['Content-Type'] = download_content_type response.headers['Content-Length'] ||= file.size.to_s # Prevent Rack::ETag from calculating a digest over body response.headers['Last-Modified'] = File.mtime(file_path).utc.strftime("%a, %d %b %Y %T GMT") self.content_type = download_content_type end |
#send_content ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 22 def send_content response.headers['Accept-Ranges'] = 'bytes' if request.head? content_head else send_file_contents end end |
#send_file_contents ⇒ Object
42 43 44 45 46 |
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 42 def send_file_contents self.status = 200 prepare_file_headers send_file file end |