우리는 비슷한 문제가 있었다.
Archetypes는 마법의 과정에서 여러 이벤트를 발생시키고 다른 이벤트 중에서는 "유효성 검사 후 이벤트"(IObjectPostValidation)를 발생시킵니다. 이렇게하면 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}
또한, PIL은'IO 오류를 소리 것이 "이미지"를 터치 할 때마다 이미지 file'를 식별 할 수 없습니다. 나에게 벌레처럼 들린다. – tcurvelo
@tcurvelo : 예, 그렇기 때문에 사용자가 PDF를 삽입하도록하면 많은 문제가 발생합니다. 이상한 것은 Plone의 기본 동작 인 것 같습니다. (100 % 확신 할 수는 없지만, 나는이 응용 프로그램을 다른 개발자로부터 가져 왔습니다.) – Pixou
가능한 복제본 http://stackoverflow.com/questions/9127630/preventing-users-to-upload-bmp-tiff-etc-images-to-imagefield-in-plone –