Skip to content

Commit

Permalink
Access uploaded files via env.params.files.
Browse files Browse the repository at this point in the history
Kemal parses the body and makes uploaded files available when
accessing `params`. There's no need to independently parse the form
data.

Remove a check for the upload request path that served as a guard
against invoking `params`, which then made it impossible to access and
parse the form data.
  • Loading branch information
toddsundsted committed Oct 29, 2024
1 parent 1e5434f commit ae321b7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/controllers/uploads.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UploadsController
post "/uploads" do |env|
filename = nil
filepath = nil
HTTP::FormData.parse(env.request) do |part|
env.params.files.each do |name, part|
if part.filename.presence
filename = env.account.actor.id.to_s
filepath = File.join("uploads", *Tuple(String, String, String).from(UUID.random.to_s.split("-")[0..2]))
Expand All @@ -17,9 +17,7 @@ class UploadsController
filename = "#{filename}#{extension}"
end
Dir.mkdir_p(File.join(Kemal.config.public_folder, filepath))
File.open(File.join(Kemal.config.public_folder, filepath, filename), "w") do |file|
IO.copy(part.body, file)
end
part.tempfile.rename(File.join(Kemal.config.public_folder, filepath, filename))
end
end
if filename && filepath
Expand Down
2 changes: 1 addition & 1 deletion src/framework/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Ktistec
# "application/x-www-form-urlencoded" on activities. see:
# https://github.com/pixelfed/pixelfed/issues/3049
return call_next env unless env.request.method == "POST"
return call_next env if env.request.path == "/uploads" || env.request.path.ends_with?("/inbox")
return call_next env if env.request.path.ends_with?("/inbox")
return call_next env unless env.params.body["_method"]? == "delete"

# switch method and fix URL params
Expand Down

0 comments on commit ae321b7

Please sign in to comment.