Skip to content

Commit

Permalink
implement fnmatch style pattern matching of mimetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
djay committed Feb 25, 2013
1 parent 7b822d9 commit 6bd19df
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/plone/app/blob/content.py
Expand Up @@ -12,6 +12,7 @@
from zope.component import getUtility
from plone.app.content.interfaces import ISiteContentSettings
from plone.registry.interfaces import IRegistry
from fnmatch import fnmatch

try:
from Products.LinguaPlone.public import registerType
Expand Down Expand Up @@ -98,15 +99,20 @@ def index_html(self, REQUEST, RESPONSE):
if IATBlobImage.providedBy(self):
return field.index_html(self, REQUEST, RESPONSE)
elif policySettings.file_mimetype_behaviour is not None:
behaviour = policySettings.file_mimetype_behaviour.get(mimetype, None)
if behaviour is None:
behaviour = policySettings.file_mimetype_behaviour('','attachment')
if behaviour == 'view':
matches = [(pattern, behaviour) for pattern, behaviour in policySettings.file_mimetype_behaviour.items() \
if fnmatch(mimetype, pattern)]
# pick the longest pattern that matches ie between */* and application/* the latter will win
matches.sort(key=lambda kv: len(kv[0]))
behaviour = matches[-1][1] if matches else None
if behaviour == 'view' and self.getLayout():
return self.restrictedTraverse(self.getLayout())()
elif behaviour == 'inline':
return field.index_html(self, REQUEST, RESPONSE)
elif behaviour == 'attachment':
return field.download(self, REQUEST, RESPONSE)
else:
return field.download(self, REQUEST, RESPONSE)

elif mimetype in ATFile.inlineMimetypes:
return field.index_html(self, REQUEST, RESPONSE)
else:
Expand Down

1 comment on commit 6bd19df

@mister-roboto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing:

Please sign in to comment.