Use Crystal built-in MIME registry (#516)

This commit is contained in:
Sijawusz Pur Rahnama 2019-02-05 08:26:45 +01:00 committed by Serdar Dogruyol
parent dcffd7b3f9
commit b7bf9d771f
3 changed files with 5 additions and 23 deletions

View file

@ -16,5 +16,7 @@ targets:
kemal:
main: src/kemal.cr
crystal: 0.27.1
author:
- Serdar Dogruyol <dogruyolserdar@gmail.com>

View file

@ -1,3 +1,5 @@
require "mime"
# Adds given `Kemal::Handler` to handlers chain.
# There are 5 handlers by default and all the custom handlers
# goes between the first 4 and the last `Kemal::RouteHandler`.
@ -118,7 +120,7 @@ end
def send_file(env : HTTP::Server::Context, path : String, mime_type : String? = nil, *, filename : String? = nil, disposition : String? = nil)
config = Kemal.config.serve_static
file_path = File.expand_path(path, Dir.current)
mime_type ||= Kemal::Utils.mime_type(file_path)
mime_type ||= MIME.from_filename(file_path, "application/octet-stream")
env.response.content_type = mime_type
env.response.headers["Accept-Ranges"] = "bytes"
env.response.headers["X-Content-Type-Options"] = "nosniff"

View file

@ -9,27 +9,5 @@ module Kemal
def self.zip_types(path : String) # https://github.com/h5bp/server-configs-nginx/blob/master/nginx.conf
ZIP_TYPES.includes? File.extname(path)
end
def self.mime_type(path : String)
case File.extname(path)
when ".txt" then "text/plain"
when ".htm", ".html" then "text/html"
when ".css" then "text/css"
when ".js" then "application/javascript"
when ".png" then "image/png"
when ".jpg", ".jpeg" then "image/jpeg"
when ".gif" then "image/gif"
when ".svg" then "image/svg+xml"
when ".ico" then "image/x-icon"
when ".xml" then "application/xml"
when ".json" then "application/json"
when ".otf", ".ttf" then "application/font-sfnt"
when ".woff" then "application/font-woff"
when ".woff2" then "font/woff2"
when ".mp4" then "video/mp4"
when ".webm" then "video/webm"
else "application/octet-stream"
end
end
end
end