2013-04-11 70 views
2

我有一个Plone应用程序,我可以上传图像,这是ATImages。我想验证扩展文件(主要是禁止PDF文件)。有一个URL调用,比如http://blablba.com/createObject?type_name=Image 我已经尝试设置与图像相关联的文件扩展名/ content_type_registry,没有成功(PDF上传仍然有效)如何限制Plone上的图像文件扩展名?

我想我可以写一个新的类扩展ATImages创建,创建一个验证器的形式,但它看起来有点复杂,似乎content_type注册表上的一些设置将足够(或其他地方)。

你会怎么做? (禁止pdf?)

thx

+0

此外,PIL会大喊'IO错误:每当这个 “形象” 被触摸无法识别图像file'。听起来像是一个bug。 – tcurvelo 2013-04-11 15:59:21

+0

@tcurvelo:是的,这就是为什么我让用户插入PDF时存在很多问题。奇怪的是,它似乎是默认的Plone行为。 (不是100%肯定的,我已经从其他开发者那里获得了这个应用程序)。 – Pixou 2013-04-11 16:08:26

+1

可能的重复http://stackoverflow.com/questions/9127630/preventing-users-to-upload-bmp-tiff-etc-images-to-imagefield-in-plone – 2013-04-11 19:22:12

回答

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}