2013-04-11 3 views
2

ATImages 인 이미지를 업로드 할 수있는 Plone 응용 프로그램이 있습니다. 확장 파일의 유효성을 검사하고 싶습니다 (주로 PDF 파일 금지). 어떤 성공 나는 이미지와 관련된 파일 확장자로/content_type_registry 설정을 시도 http://blablba.com/createObject?type_name=Image 같은 URL 호출 (PDF 업로드 여전히 작동)Plone에서 이미지 파일 확장자를 제한하는 방법은 무엇입니까?

내가 ATImages을 확장하는 새로운 클래스를 작성할 수 추측으로이 생성하는을 만들 양식이 유효성 검사기로 작성되었지만 조금 복잡해 보였으며 content_type 레지스트리의 일부 설정이 충분할 것으로 보였습니다 (또는 다른 곳에서).

어떻게 하시겠습니까? (PDF를 금지?)

들으

+0

또한, PIL은'IO 오류를 소리 것이 "이미지"를 터치 할 때마다 이미지 file'를 식별 할 수 없습니다. 나에게 벌레처럼 들린다. – tcurvelo

+0

@tcurvelo : 예, 그렇기 때문에 사용자가 PDF를 삽입하도록하면 많은 문제가 발생합니다. 이상한 것은 Plone의 기본 동작 인 것 같습니다. (100 % 확신 할 수는 없지만, 나는이 응용 프로그램을 다른 개발자로부터 가져 왔습니다.) – Pixou

+1

가능한 복제본 http://stackoverflow.com/questions/9127630/preventing-users-to-upload-bmp-tiff-etc-images-to-imagefield-in-plone –

답변

1

우리는 비슷한 문제가 있었다.

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} 
관련 문제