Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Improve handling of URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon committed Jan 15, 2024
1 parent 9602198 commit 7ab33da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/controllers/paste_controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ module Paste69

if form.has_key?("file")
filename, body = form["file"]
end

if form.has_key?("ext")
_, ext = form["ext"]
ext = String.new(ext).lstrip(".")
filename = filename ? File.basename(filename, File.extname(filename)) : "file"
filename = "#{filename}.#{ext}"
end
if form.has_key?("ext")
_, ext = form["ext"]
ext = String.new(ext).lstrip(".")
filename = filename ? File.basename(filename, File.extname(filename)) : "file"
filename = "#{filename}.#{ext}"
end

if body
@utils.store_file(
body,
content_type,
Expand All @@ -157,6 +159,8 @@ module Paste69
_, body = form["url"]
@utils.store_url(
String.new(body),
filename,
expires ? String.new(expires).to_i64 : nil,
remote_addr,
user_agent,
!!secret,
Expand Down
5 changes: 3 additions & 2 deletions src/services/utils_service.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module Paste69
res
end

def store_url(url : String, addr : String? = nil, ua : String? = nil, secret : Bool = false)
def store_url(url : String, filename : String? = nil, requested_expiration : Int64? = nil, addr : String? = nil, ua : String? = nil, secret : Bool = false)
if is_fhost_url?(url)
raise ATH::Exceptions::BadRequest.new("Invalid URL")
end
Expand All @@ -102,7 +102,8 @@ module Paste69
if res.headers.has_key?("Content-Length")
l = res.headers["Content-Length"].to_i64
if l <= @config.get("max_content_length").as_i64
return store_file(res.body.to_slice, res.headers["Content-Type"], nil, nil, addr, ua, secret)
filename ||= File.basename(url)
return store_file(res.body.to_slice, res.headers["Content-Type"], filename, nil, addr, ua, secret)
else
raise Exceptions::ContentTooLarge.new("Content-Length was too large")
end
Expand Down

0 comments on commit 7ab33da

Please sign in to comment.