Skip to content

Commit

Permalink
Do not break with unknown MIME types
Browse files Browse the repository at this point in the history
Fixes #197
  • Loading branch information
ale-rt committed May 25, 2020
1 parent 0409d15 commit 3f5b501
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/197.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not break with unknown MIME types [ale-rt]
16 changes: 13 additions & 3 deletions plone/app/content/browser/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from Products.CMFPlone.utils import safe_unicode
from Products.Five import BrowserView
from Products.MimetypesRegistry.MimeTypeItem import guess_icon_path
from Products.MimetypesRegistry.MimeTypeItem import PREFIX
from types import FunctionType
from z3c.form.interfaces import ISubForm
from zope.component import getUtility
Expand Down Expand Up @@ -250,9 +251,18 @@ def __call__(self):
getattr(vocab_item, 'mime_type', None))
if contenttype:
ctype = mtt.lookup(contenttype)
item[key] = '/'.join([
base_path,
guess_icon_path(ctype[0])])
if ctype:
item[key] = '/'.join([
base_path,
guess_icon_path(ctype[0])])
else:
item[key] = "/".join(
[
base_path,
PREFIX.rstrip("/"),
"unknown.png",
]
)
items.append(item)
else:
items = [{'id': item.value,
Expand Down
35 changes: 35 additions & 0 deletions plone/app/content/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,41 @@ def testUntranslatableMetadata(self):
# portal_type is never translated
self.assertEqual(data['results'][0]['portal_type'], u'Document')

def testGetMimeIcon(self):
""" Check if the returned icon is correct
"""
self.request.form.update(
{
"name": "plone.app.vocabularies.Catalog",
"attributes": ["getMimeIcon"],
}
)
view = VocabularyView(self.portal, self.request)

# Check an empty file
self.portal.invokeFactory("File", id="my-file", title="My file")
obj = self.portal["my-file"]
obj.reindexObject()

self.assertListEqual(
json.loads(view())["results"], [{"getMimeIcon": None}]
)

# mock a pdf
obj.file = mock.Mock(contentType="application/pdf")
obj.reindexObject()
self.assertListEqual(
json.loads(view())["results"],
[{"getMimeIcon": "/plone/++resource++mimetype.icons/pdf.png"}],
)

# mock something unknown
obj.file = mock.Mock(contentType="x-foo/x-bar")
obj.reindexObject()
self.assertListEqual(
json.loads(view())["results"],
[{"getMimeIcon": "/plone/++resource++mimetype.icons/unknown.png"}],
)

class FunctionalBrowserTest(unittest.TestCase):

Expand Down

0 comments on commit 3f5b501

Please sign in to comment.