Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop to master #89

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ckanext/s3filestore/tests/example.docx
Binary file not shown.
12 changes: 9 additions & 3 deletions ckanext/s3filestore/tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,24 @@ def _test_dataset(self, private=False, title='Test Dataset', author='test'):
author=author,
owner_org=self.organisation['id'])

def _upload_test_resource(self, dataset=None):
def _upload_test_resource(self, dataset=None, filename='data.csv'):
''' Creates a test resource in the specified dataset
by uploading a file.
'''
if not dataset:
dataset = self._test_dataset()
file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
file_path = os.path.join(os.path.dirname(__file__), filename)
return helpers.call_action(
'resource_create',
package_id=dataset['id'],
upload=FlaskFileStorage(io.open(file_path, 'rb')),
url='data.csv')
url=filename)

def test_resource_upload(self):
'''Test a basic resource file upload'''
file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
resource = self._upload_test_resource()
assert_equal(resource['mimetype'], 'text/csv')

key = _get_object_key(resource)

Expand Down Expand Up @@ -499,6 +500,11 @@ def test_ignoring_non_uploads(self):
mock_upload_to_key.assert_not_called()
mock_update_visibility.assert_called_once_with(resource['id'])

def test_detect_office_document_type(self):
dataset = self._test_dataset()
resource = self._upload_test_resource(dataset, 'example.docx')
assert_equal(resource['mimetype'], 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')

if toolkit.check_ckan_version(max_version='2.8.99'):

def test_resource_upload_then_clear(self):
Expand Down
14 changes: 12 additions & 2 deletions ckanext/s3filestore/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,18 @@ def __init__(self, resource):
self.mimetype = resource.get('mimetype')
if not self.mimetype:
try:
# 512 bytes should be enough for a mimetype check
self.mimetype = resource['mimetype'] = mime.from_buffer(self.upload_file.read(512))
try:
# Get type from file extension if we recognise it
self.mimetype = mimetypes.guess_type(self.filename, strict=False)[0]
except Exception:
pass
if not self.mimetype:
try:
# 2048 bytes are needed to detect Office docs
self.mimetype = mime.from_buffer(self.upload_file.read(2048))
except Exception:
pass
resource['mimetype'] = self.mimetype

# additional check on text/plain mimetypes for
# more reliable result, if None continue with text/plain
Expand Down