Added response type recognition to local file urls

--HG--
extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40790
This commit is contained in:
elpolilla 2009-01-28 11:45:39 +00:00
parent e9fbce4ee2
commit bc4fcbeab7
2 changed files with 7 additions and 6 deletions

View File

@ -104,5 +104,9 @@ def download_file(request, spider) :
"""Return a deferred for a file download."""
filepath = request.url.split("file://")[1]
with open(filepath) as f:
response = Response(url=request.url, body=f.read())
body = f.read()
respcls = responsetypes.from_filename(filepath)
respcls = responsetypes.from_body(body) if respcls is Response else respcls
response = respcls(url=request.url, body=body)
return defer_succeed(response)

View File

@ -67,13 +67,10 @@ class ResponseTypes(object):
return self.from_mimetype(mimetypes.guess_type(url)[0])
def from_body(self, body):
"""Try to guess the appropiate response based on the body content.
"""Try to guess the appropiate response based on the body content.
This method is a bit magic and could be improved in the future, but
it's not meant to be used except for special cases where response types
cannot be guess using more straightforward methods.
"""
cannot be guess using more straightforward methods."""
chunk = body[:5000]
if isbinarytext(chunk):
return self.from_mimetype('application/octet-stream')