Skip to content

Commit

Permalink
Improve contenttype detection logic for unregistered but common types.
Browse files Browse the repository at this point in the history
Change get_contenttype to support common types which are or were not registered
with IANA, like image/webp or audio/midi.

Note: image/webp is already a IANA registered type and also added by
Products.MimetypesRegistry.
  • Loading branch information
thet committed Mar 14, 2024
1 parent 591d0ae commit 514b4eb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
8 changes: 8 additions & 0 deletions news/157-2.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Improve contenttype detection logic for unregistered but common types.

Change get_contenttype to support common types which are or were not registered
with IANA, like image/webp or audio/midi.

Note: image/webp is already a IANA registered type and also added by
Products.MimetypesRegistry.
[thet]
20 changes: 0 additions & 20 deletions plone/namedfile/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,3 @@ def test_extract_media_type(self):
self.assertEqual(extract("TEXT/PLAIN"), "text/plain")
self.assertEqual(extract("text / plain"), "text/plain")
self.assertEqual(extract(" text/plain ; charset=utf-8"), "text/plain")

def test_get_contenttype(self):
self.assertEqual(
get_contenttype(NamedImage(getFile("image.gif"), contentType="image/gif")),
"image/gif",
)
self.assertEqual(
get_contenttype(NamedImage(getFile("image.gif"), filename="image.gif")),
"image/gif",
)
self.assertEqual(
get_contenttype(NamedImage(getFile("image.tif"), filename="image.tif")),
"image/tiff",
)
self.assertEqual(
get_contenttype(
NamedImage(getFile("notimage.doc"), filename="notimage.doc")
),
"application/msword",
)
64 changes: 64 additions & 0 deletions plone/namedfile/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from plone.namedfile.file import NamedImage
from plone.namedfile.tests import getFile
from plone.namedfile.utils import get_contenttype

import unittest


class TestUtils(unittest.TestCase):

def test_get_contenttype(self):
self.assertEqual(
get_contenttype(
NamedImage(
getFile("image.gif"),
contentType="image/gif",
)
),
"image/gif",
)
self.assertEqual(
get_contenttype(
NamedImage(
getFile("image.gif"),
filename="image.gif",
)
),
"image/gif",
)
self.assertEqual(
get_contenttype(
NamedImage(
getFile("image.tif"),
filename="image.tif",
)
),
"image/tiff",
)
self.assertEqual(
get_contenttype(
NamedImage(
getFile("notimage.doc"),
filename="notimage.doc",
)
),
"application/msword",
)

# Filename only detection of a non-IANA registered type.
self.assertEqual(
get_contenttype(filename="image.webp"),
"image/webp",
)

# Filename only detection of a non-IANA registered type.
self.assertEqual(
get_contenttype(filename="song.midi"),
"audio/midi",
)

# Detection of a surely not registered type.
self.assertEqual(
get_contenttype(filename="nothing.plonenamedfile"),
"application/octet-stream",
)
4 changes: 1 addition & 3 deletions plone/namedfile/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from ZPublisher.Iterators import IStreamIterator

import mimetypes
import os.path
import piexif
import PIL.Image
import re
Expand Down Expand Up @@ -126,8 +125,7 @@ def get_contenttype(file=None, filename=None, default="application/octet-stream"

filename = getattr(file, "filename", filename)
if filename:
extension = os.path.splitext(filename)[1].lower()
return mimetypes.types_map.get(extension, "application/octet-stream")
return mimetypes.guess_type(filename, strict=False)[0] or default

return default

Expand Down

0 comments on commit 514b4eb

Please sign in to comment.