mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Add send_file to helpers
This commit is contained in:
parent
b00bc7a202
commit
5e297d86f8
2 changed files with 43 additions and 0 deletions
|
@ -40,3 +40,20 @@ end
|
|||
def headers(env, additional_headers)
|
||||
env.response.headers.merge!(additional_headers)
|
||||
end
|
||||
|
||||
# Send a file with given path and default `application/octet-stream` mime_type.
|
||||
#
|
||||
# send_file env, "./path/to/file"
|
||||
#
|
||||
# Optionally you can override the mime_type
|
||||
#
|
||||
# send_file env, "./path/to/file", "image/jpeg"
|
||||
def send_file(env, path : String, mime_type : String? = nil)
|
||||
file_path = File.expand_path(path, Dir.current)
|
||||
mime_type = "application/octet-stream" unless mime_type
|
||||
env.response.headers.add "Content-Type", mime_type
|
||||
env.response.content_length = File.size(file_path)
|
||||
File.open(file_path) do |file|
|
||||
IO.copy(file, env.response)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue