Skip to content

Commit

Permalink
Merge pull request #102 from 4dn-dcic/attachment_mimetypes
Browse files Browse the repository at this point in the history
Release 1.2.1
  • Loading branch information
carlvitzthum authored Aug 12, 2019
2 parents e52f7fe + bf88c76 commit 48b2eea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

setup(
name='snovault',
version='1.2.0',
version='1.2.1',
description='Snovault Hybrid Object Relational Database Framework',
long_description=README + '\n\n' + CHANGES,
packages=find_packages('src'),
Expand Down
33 changes: 22 additions & 11 deletions src/snovault/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,28 @@ def parse_data_uri(uri):
return mime_type, charset, data


def mimetypes_are_equal(m1, m2):
major1 = m1.split('/')[0]
major2 = m2.split('/')[0]
if major1 == 'text' and major2 == 'text':
return True
return m1 == m2


class ItemWithAttachment(Item):
""" Item base class with attachment blob
"""
Item base class with attachment blob.
Handles validation, storage, and downloading of attachments given in the
`attachment` field of the given item.
The `mimetype_map` attribute can be used to override mimetype comparisons,
which may be required in some cases with python-magic. Use with care!
"""
# specify in form: {some mimetype: [one or more equivalent mimetypes]}
mimetype_map = {}

@classmethod
def mimetypes_are_equal(cls, m1, m2):
if m2 in cls.mimetype_map.get(m1, []):
return True

major1 = m1.split('/')[0]
major2 = m2.split('/')[0]
if major1 == 'text' and major2 == 'text':
return True
return m1 == m2

def _process_downloads(self, prop_name, properties, downloads):
attachment = properties[prop_name]
Expand All @@ -89,7 +100,7 @@ def _process_downloads(self, prop_name, properties, downloads):
if mime_type_from_filename is None:
mime_type_from_filename = 'application/octet-stream'
if mime_type:
if not mimetypes_are_equal(mime_type, mime_type_from_filename):
if not self.mimetypes_are_equal(mime_type, mime_type_from_filename):
raise ValidationFailure(
'body', [prop_name, 'href'],
'Wrong file extension for %s mimetype.' % mime_type)
Expand All @@ -103,7 +114,7 @@ def _process_downloads(self, prop_name, properties, downloads):
except AttributeError:
mime_type_detected = magic.from_buffer(data, mime=True)

if not mimetypes_are_equal(mime_type, mime_type_detected):
if not self.mimetypes_are_equal(mime_type, mime_type_detected):
msg = "Incorrect file type. (Appears to be %s)" % mime_type_detected
raise ValidationFailure('body', [prop_name, 'href'], msg)

Expand Down

0 comments on commit 48b2eea

Please sign in to comment.