如何限制Plone上的图像文件扩展名? [英] How to restrict image file extension on Plone?
问题描述
我有一个Plone应用程序,可以在其中上传图像,即ATImages.我想验证扩展文件(主要是禁止pdf文件).其中创建的网址调用类似于 http://blablba.com/createObject?type_name=Image 我尝试将/content_type_registry设置为与图片相关联的文件扩展名,但没有成功(pdf上传仍然有效)
I have a Plone application in which I can upload images, which are ATImages. I want to validate the extension file (mainly to forbid pdf files). There are created with a url call like http://blablba.com/createObject?type_name=Image I have tried setting the /content_type_registry with file extensions associated with images, with no success (pdf upload still work)
我想我可以写一个扩展ATImages的新类,创建一个带有验证器的表单,但是看起来有点复杂,而且似乎content_type注册表上的某些设置就足够了(或其他地方).
I guess I could write a new class extending ATImages, create a form with a validator, but it looks a little bit complicated and it seemed that some settings on content_type registry would be enough (or elsewhere).
您将如何做? (禁止使用pdf吗?)
How would you do that ? (forbid pdf ?)
thx
推荐答案
我们遇到了类似的问题.
We had a similar problem.
Archetypes在魔术期间会触发多个事件,其中包括验证后事件"(IObjectPostValidation).这样,我们为内容类型添加了检查.
Archetypes fires several events during its magic, amongst others a "post validation event" (IObjectPostValidation). This way we added a check for the content-type.
订户(zcml):
<subscriber provides="Products.Archetypes.interfaces.IObjectPostValidation"
factory=".subscribers.ImageFieldContentValidator" />
快速而肮脏的实现
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}
这篇关于如何限制Plone上的图像文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!