Skip to content

Commit

Permalink
Allow downloading images by passing download=true query param
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Mar 4, 2024
1 parent 69b2643 commit 18544fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ext/kemal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end

private MAGICKLOAD_EXTENSIONS = %w(.arw .cin .cr2 .crw .nef .orf .raf .x3f)

def send_file_as_jpeg(env, file : GPhoto2::CameraFile, width : Int? = nil, height : Int? = nil, disposition = "inline")
def send_file_as_jpeg(env, file : GPhoto2::CameraFile, width : Int? = nil, height : Int? = nil, disposition = nil)
file_path = Path[file.path]

if file_path.extension.downcase.in?(MAGICKLOAD_EXTENSIONS)
Expand All @@ -70,6 +70,8 @@ def send_file_as_jpeg(env, file : GPhoto2::CameraFile, width : Int? = nil, heigh
end
end

disposition ||= "inline"

send_file env, image.jpegsave_buffer(strip: true),
mime_type: "image/jpeg",
filename: "#{file_path.stem}.jpg",
Expand Down
9 changes: 7 additions & 2 deletions src/gphoto2/web/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ get "/cameras/:id/blob/*filepath" do |env|
width = env.params.query["width"]?.try(&.to_i)
height = env.params.query["height"]?.try(&.to_i)

download = env.params.query["download"]? == "true"
disposition = "attachment" if download

GPhoto2::Web.camera_by_id(id) do |camera|
fs = camera / path.dirname
file = fs.open(path.basename)
Expand All @@ -146,9 +149,11 @@ get "/cameras/:id/blob/*filepath" do |env|
if as_jpeg || width
send_file_as_jpeg env, file,
width: width,
height: height
height: height,
disposition: disposition
else
send_file env, file
send_file env, file,
disposition: disposition
end
end
end
Expand Down

0 comments on commit 18544fe

Please sign in to comment.