mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 10:55:30 -04:00
25 lines
520 B
Crystal
25 lines
520 B
Crystal
require "kemal"
|
|
require "../util"
|
|
|
|
class UploadHandler < Kemal::Handler
|
|
def initialize(@upload_dir : String)
|
|
end
|
|
|
|
def call(env)
|
|
unless request_path_startswith(env, [UPLOAD_URL_PREFIX]) &&
|
|
env.request.method == "GET"
|
|
return call_next env
|
|
end
|
|
|
|
ary = env.request.path.split(File::SEPARATOR).select { |part| !part.empty? }
|
|
ary[0] = @upload_dir
|
|
path = File.join ary
|
|
|
|
if File.exists? path
|
|
send_file env, path
|
|
else
|
|
env.response.status_code = 404
|
|
end
|
|
end
|
|
end
|