2013-04-11 47 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} 
相關問題