mirror of
https://github.com/hkalexling/Mango.git
synced 2026-01-24 00:03:14 -05:00
27 lines
535 B
Crystal
27 lines
535 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 do |part|
|
|
!part.empty?
|
|
end
|
|
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
|