Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Refactored unknown content type scenario.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Oct 27, 2011
1 parent 7f3700c commit ce8ece5
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions composer/writer.py
Expand Up @@ -23,6 +23,11 @@ def __init__(self, index):
self.index = index

def _guess_content_type(self, path):
filename = os.path.basename(path)

if '.' not in filename: # Assume html
return 'text/html'

return mimetypes.guess_type(path)[0]

def render_route(self, route):
Expand Down Expand Up @@ -51,7 +56,12 @@ def __call__(self, environ, start_response):
start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
return ['Not Found']

content_type = self._guess_content_type(path) or 'text/html'
content_type = self._guess_content_type(path)
if not content_type:
content_type = 'application/octet-stream'
log.warn("Serving literal file of unknown content type: /%s "
"(Hint: Add / suffix to treat it as a directory)", path)

start_response('200 OK', [('Content-Type', content_type)])
return [content]

Expand Down Expand Up @@ -83,14 +93,6 @@ def _get_materialize_path(self, url, default_index_file='index.html'):
url_path = os.path.join(self.build_path, url)
return url_path, index_file

def _is_unknown_content_type(self, url):
url = os.path.basename(url)

if '.' not in url:
return False # Assume html

return self._guess_content_type(url) == None

def _prepare_dir(self, path):
if not os.path.exists(path):
os.makedirs(path)
Expand All @@ -103,7 +105,7 @@ def _write_file(self, path, content):
def materialize_url(self, url, content=None, default_index_file='index.html'):
url = url.lstrip('/')

if self._is_unknown_content_type(url):
if not self._guess_content_type(url):
log.warn("Materializing literal file of unknown content type: /%s "
"(Hint: Add / suffix to treat it as a directory)", url)

Expand Down

0 comments on commit ce8ece5

Please sign in to comment.