Skip to content

Commit

Permalink
Account for browser supported content types when sending files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 12, 2024
1 parent a613636 commit d1b9e9b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ext/kemal.cr
Expand Up @@ -14,7 +14,9 @@ private def restore_headers_on_rescue(response, &)
end
end

def send_file(env, file : GPhoto2::CameraFile, mime_type : String? = nil, disposition = "inline")
private BROWSER_SAFE_EXTENSIONS = %w(.jpg .jpeg .png .gif .bmp .svg .webp)

def send_file(env, file : GPhoto2::CameraFile, mime_type : String? = nil, disposition = nil)
restore_headers_on_rescue(env.response) do |response|
# WARNING: Executes extra calls to underlying camera
if file.preview?
Expand All @@ -31,6 +33,9 @@ def send_file(env, file : GPhoto2::CameraFile, mime_type : String? = nil, dispos
end
end

ext = Path[filename].extension.downcase
disposition ||= "inline" if ext.in?(BROWSER_SAFE_EXTENSIONS)

send_file env, file.to_slice,
mime_type: mime_type,
filename: filename,
Expand Down

0 comments on commit d1b9e9b

Please sign in to comment.