Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB-4122 : Create adapter/validator to filter valid image mimetype our solutions #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Changelog
1.2.19 (unreleased)
-------------------

- Nothing changed yet.
- WEB-4122 : Create adapter/validator to filter valid image mimetype in our solutions
[boulch]


1.2.18 (2024-07-01)
Expand Down
26 changes: 26 additions & 0 deletions src/imio/smartweb/common/adapters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-

from plone.namedfile.interfaces import IAvailableSizes
from plone.namedfile.interfaces import INamedImageField
from plone.namedfile.field import InvalidImageFile
from plone.namedfile.utils import get_contenttype
from zope.component import adapter
from zope.component import getUtility
from zope.interface import Interface


class BaseCroppingProvider(object):
Expand All @@ -17,3 +22,24 @@ def get_scales(self, fieldname, request=None):
scales = list(allowed_sizes.keys())
scales.remove("banner")
return scales


@adapter(INamedImageField, Interface)
class ImageContenttypeValidator:
def __init__(self, field, value):
self.field = field
self.value = value

def __call__(self):
if self.value is None:
return
mimetype = get_contenttype(self.value)
valid_mimetypes = [
"image/gif",
"image/jpeg",
"image/png",
"image/svg+xml",
"image/webp",
]
if mimetype not in valid_mimetypes:
raise InvalidImageFile(mimetype, self.field.__name__)
10 changes: 10 additions & 0 deletions src/imio/smartweb/common/adapters.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@
factory=".adapters.BaseCroppingProvider"
/>

<unconfigure>
<adapter name="image_contenttype" factory="plone.namedfile.field.ImageContenttypeValidator" />
</unconfigure>
Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

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

I tried the adapter without the unconfigure, and for me it is working fine : your adapter and the original one are both called.
We need to check that 😉


<adapter
factory=".adapters.ImageContenttypeValidator"
provides="plone.namedfile.field.IPluggableImageFieldValidation"
name="smartweb_image_contenttype"
/>

</configure>
1 change: 1 addition & 0 deletions src/imio/smartweb/common/configure.zcml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<configure
xmlns="http://namespaces.zope.org/zope">

<include package="z3c.unconfigure" file="meta.zcml" />
<include package="collective.taxonomy" />
<include package="eea.facetednavigation" />
<include package="eea.facetednavigation" file="meta.zcml" />
Expand Down
Loading