From eb4baeb1fae09658770fa05c374ee871a334a08c Mon Sep 17 00:00:00 2001 From: dnoneill Date: Thu, 4 Sep 2025 16:11:43 -0400 Subject: [PATCH] encode inline text files for the browser --- app/controllers/file_controller.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/file_controller.rb b/app/controllers/file_controller.rb index 76639788..c6eb66d6 100644 --- a/app/controllers/file_controller.rb +++ b/app/controllers/file_controller.rb @@ -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 + mimetype += "; charset=#{encoding}" + + mimetype + end + def options response.headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS' response.headers['Access-Control-Allow-Headers'] = 'Range'