How to limit image file extension to Plone?

I have a Plone application in which I can upload images that are ATImages. I want to check the extension file (mainly to prohibit PDF files). Created using a url like http://blablba.com/createObject?type_name=Image I tried to install / content _type_registry with image-related file extensions without any success (uploading PDFs still works)

I think I could write a new class that extends ATImages, create a form using a validator, but it looks a little more complicated, and it seemed that some parameters in the content_type registry would be enough (or elsewhere).

How do you do this? (disable pdf?)

thanks

+2
source share
1 answer

We had a similar problem.

Archetypes trigger several events during his magic, among other things, the "mail check event" (IObjectPostValidation). Thus, we added a check for the content type.

Subscriber (zcml):

<subscriber provides="Products.Archetypes.interfaces.IObjectPostValidation" factory=".subscribers.ImageFieldContentValidator" /> 

quick and dirty implementation:

 from Products.Archetypes.interfaces.field import IImageField from plone.app.blob.interfaces import IBlobImageField from Products.Archetypes.interfaces import IObjectPostValidation from zope.interface import implements from zope.component import adapts # import your message factory as _ ALLOWED_IMAGETYPES = ['image/png', 'image/jpeg', 'image/gif', 'image/pjpeg', 'image/x-png'] class ImageFieldContentValidator(object): """Validate that the ImageField really contains a Imagefile. Show a Errormessage if it doesn't. """ implements(IObjectPostValidation) adapts(IBaseObject) img_interfaces = [IBlobImageField, IImageField] msg = _(u"error_not_image", default="The File you wanted to upload is no image") def __init__(self, context): self.context = context def __call__(self, request): for fieldname in self.context.Schema().keys(): field = self.context.getField(fieldname) if True in [img_interface.providedBy(field) \ for img_interface in self.img_interfaces]: item = request.get(fieldname + '_file', None) if item: header = item.headers ct = header.get('content-type') if ct in ALLOWED_IMAGETYPES: return else: return {fieldname: self.msg} 
+1
source

Source: https://habr.com/ru/post/1394593/


All Articles