-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
73 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters