Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
7
Merge Requests
7
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Jérome Perrin
erp5
Commits
89ec739e
Commit
89ec739e
authored
Jan 28, 2024
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MimeTypesRegistry str/bytes bug ?
parent
524852fd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
0 deletions
+69
-0
product/ERP5Type/patches/MimetypesRegistry.py
product/ERP5Type/patches/MimetypesRegistry.py
+69
-0
No files found.
product/ERP5Type/patches/MimetypesRegistry.py
View file @
89ec739e
...
...
@@ -17,3 +17,72 @@ def initialize(registry):
mime
.
extensions
=
tuple
(
x
)
MimeTypesRegistry
.
initialize
=
initialize
# patched from https://github.com/plone/Products.MimetypesRegistry/blob/2.1.8/Products/MimetypesRegistry/MimeTypesRegistry.py#L305-L359
# to change type of `data`. Originally, Products.MimetypesRegistry only "native str" for data, but this data is passed
# to magic.guessMime(data), which expects bytes and later before passing it to guess_content_type
# it is .encode()'ed, which so this expectes str - which is inconsistent.
# This relaxes the data type to tolerate bytes or str on python3
from
Products.MimetypesRegistry.mime_types
import
magic
from
zope.contenttype
import
guess_content_type
from
Acquisition
import
aq_base
def
classify
(
self
,
data
,
mimetype
=
None
,
filename
=
None
):
"""Classify works as follows:
1) you tell me the rfc-2046 name and I give you an IMimetype
object
2) the filename includes an extension from which we can guess
the mimetype
3) we can optionally introspect the data
4) default to self.defaultMimetype if no data was provided
else to application/octet-stream of no filename was provided,
else to text/plain
Return an IMimetype object or None
"""
mt
=
None
if
mimetype
:
mt
=
self
.
lookup
(
mimetype
)
if
mt
:
mt
=
mt
[
0
]
elif
filename
:
mt
=
self
.
lookupExtension
(
filename
)
if
mt
is
None
:
mt
=
self
.
globFilename
(
filename
)
if
data
and
not
mt
:
for
c
in
self
.
_classifiers
():
if
c
.
classify
(
data
):
mt
=
c
break
if
not
mt
:
if
six
.
PY3
and
isinstance
(
data
,
str
):
# <<<< patch allow bytes or str
data
=
data
.
encode
()
mstr
=
magic
.
guessMime
(
data
)
if
mstr
:
_mt
=
self
.
lookup
(
mstr
)
if
len
(
_mt
)
>
0
:
mt
=
_mt
[
0
]
if
not
mt
:
if
not
data
:
mtlist
=
self
.
lookup
(
self
.
defaultMimetype
)
elif
filename
:
mtlist
=
self
.
lookup
(
'application/octet-stream'
)
else
:
failed
=
'text/x-unknown-content-type'
filename
=
filename
or
''
data
=
data
or
''
if
six
.
PY3
and
isinstance
(
data
,
str
):
# <<<< patch allow bytes or str
data
=
data
.
encode
()
ct
,
enc
=
guess_content_type
(
filename
,
data
,
None
)
if
ct
==
failed
:
ct
=
'text/plain'
mtlist
=
self
.
lookup
(
ct
)
if
len
(
mtlist
)
>
0
:
mt
=
mtlist
[
0
]
else
:
return
None
# Remove acquisition wrappers
return
aq_base
(
mt
)
MimeTypesRegistry
.
MimeTypesRegistry
.
classify
=
classify
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment