我有一个Plone应用程序,我可以在其中上传图像,即ATImages。我想验证扩展文件(主要是禁止pdf文件)。创建了一个像http://blablba.com/createObject?type_name=Image这样的网址调用 我尝试将/ content_type_registry设置为与图像相关联的文件扩展名,但没有成功(pdf上传仍然有效)
我想我可以编写一个扩展ATImages的新类,使用验证器创建一个表单,但它看起来有点复杂,似乎content_type注册表上的某些设置就足够了(或其他地方)。
你会怎么做? (禁止pdf?)
thx
答案 0 :(得分:1)
我们遇到了类似的问题。
Archetypes在其魔术期间发射了几个事件,其中包括“后验证事件”(IObjectPostValidation)。这样我们就添加了对内容类型的检查。
订阅者(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}