Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/controllers/file_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ def show
ip: request.remote_ip
)

send_file current_file.path, filename: current_file.file_name, disposition:
send_file current_file.path, filename: current_file.file_name, type: file_type, disposition:
end
# rubocop:enable Metrics/AbcSize

def file_type
mimetype = current_file.cocina.find_file(current_file.file_name)['hasMimeType']
return mimetype unless mimetype.starts_with?('text')

content = File.read(current_file.path)
encoding = content.encoding.name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding is a property of the environment, not the file.

for example:

➜ ruby -e 'p File.read("Gemfile").encoding'
#<Encoding:UTF-8>
➜  ruby -E ISO-8859-1 -e 'p File.read("Gemfile").encoding'
#<Encoding:ISO-8859-1>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, there may be multiple valid encodings that overlap (as is true in this file with UTF-8 and ISO-8859-1)

mimetype += "; charset=#{encoding}"

mimetype
end

def options
response.headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Range'
Expand Down
Loading