I have the following problem. Sounds are hidden from the public folder, cause there are only certain Users who should have access to the sound files. So I made a certain met
I used Garrett's answer and modified it (including one or two bug fixes). I also used send_data instead of reading from a file:
def stream_data data, options={}
range_start = 0
file_size = data.length
range_end = file_size - 1
status_code = "200"
if request.headers["Range"]
status_code = "206"
request.headers['range'].match(/bytes=(\d+)-(\d*)/).try do |match|
range_start = match[1].to_i
range_end = match[2].to_i unless match[2]&.empty?
end
response.header["Content-Range"] = "bytes #{range_start}-#{range_end}/#{file_size}"
end
response.header["Content-Length"] = (range_end - range_start + 1).to_s
response.header["Accept-Ranges"] = "bytes"
send_data(data[range_start, range_end],
filename: options[:filename],
type: options[:type],
disposition: "inline",
status: status_code)
end