Module: Bulkrax::DownloadBehavior

Included in:
ExportersController, ImportersController
Defined in:
app/controllers/concerns/bulkrax/download_behavior.rb

Instance Method Summary collapse

Instance Method Details

#content_headObject

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_optionsObject

Create some headers for the datastream



32
33
34
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 32

def content_options
  { disposition: 'attachment', type: download_content_type, filename: file_name }
end

#download_content_typeObject



18
19
20
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 18

def download_content_type
  'application/zip'
end

#fileObject



8
9
10
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 8

def file
  @file ||= File.open(file_path, 'r')
end

#file_nameString

Override this if you’d like a different filename

Returns:

  • (String)

    the filename



14
15
16
# File 'app/controllers/concerns/bulkrax/download_behavior.rb', line 14

def file_name
  file_path.split('/').last
end

#prepare_file_headersObject



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! content_options
  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_contentObject



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_contentsObject



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