Skip to content

Commit

Permalink
Added config option for guessing mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
jrods committed Sep 30, 2016
1 parent 2fa1dd1 commit d4ec8ab
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ckan/lib/uploader.py
Expand Up @@ -193,6 +193,8 @@ def upload(self, max_size=2):
class ResourceUpload(object):
def __init__(self, resource):
path = get_storage_path()
config_mimetype_guess = config.get('ckan.mimetype_guess', 'file_ext')

if not path:
self.storage_path = None
return
Expand All @@ -212,7 +214,8 @@ def __init__(self, resource):
upload_field_storage = resource.pop('upload', None)
self.clear = resource.pop('clear_upload', None)

self.mimetype = mimetypes.guess_type(url)[0]
if config_mimetype_guess == 'file_ext':
self.mimetype = mimetypes.guess_type(url)[0]

if isinstance(upload_field_storage, cgi.FieldStorage):
self.filename = upload_field_storage.filename
Expand All @@ -228,11 +231,10 @@ def __init__(self, resource):
self.upload_file.seek(0, os.SEEK_SET)

# check if the mimetype failed from guessing with the url
if not self.mimetype:
if not self.mimetype and config_mimetype_guess == 'file_ext':
self.mimetype = mimetypes.guess_type(self.filename)[0]

# check again if guessing with the filename failed
if not self.mimetype:
if not self.mimetype and config_mimetype_guess == 'file_contents':
try:
self.mimetype = magic.from_buffer(self.upload_file.read(),
mime=True)
Expand Down

0 comments on commit d4ec8ab

Please sign in to comment.