1
0
Fork 0
forked from github/pelican

Merge pull request #1515 from foresto/serve-mime-type

Guess mime type with python-magic if available. Fixes #1514.
This commit is contained in:
Justin Mayer 2015-03-27 17:14:16 +09:00
commit 8a479733f0

View file

@ -12,6 +12,11 @@ try:
except ImportError:
import socketserver # NOQA
try:
from magic import from_file as magic_from_file
except ImportError:
magic_from_file = None
PORT = len(sys.argv) in (2, 3) and int(sys.argv[1]) or 8000
SERVER = len(sys.argv) == 3 and sys.argv[2] or ""
SUFFIXES = ['', '.html', '/index.html']
@ -39,6 +44,18 @@ class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
logging.warning("Unable to find `%s` or variations.",
self.original_path)
def guess_type(self, path):
"""Guess at the mime type for the specified file.
"""
mimetype = srvmod.SimpleHTTPRequestHandler.guess_type(self, path)
# If the default guess is too generic, try the python-magic library
if mimetype == 'application/octet-stream' and magic_from_file:
mimetype = magic_from_file(path, mime=True)
return mimetype
Handler = ComplexHTTPRequestHandler
socketserver.TCPServer.allow_reuse_address = True