def _setData(self, data): '''Set the image data. Nicked from the Zope 3 Image and File classes. ARGUMENTS data: A string containing the data to set. RETURNS None. SIDE EFFECTS Sets "self._data" to contain the data. Sets "self._size" to the length of the data. Sets "self._contentType" to the content type of the image. Sets "self._width" to the width of the image. Sets "self._height" to the height of the image.''' if type(data) == str: _d = data elif type(data) == file: _d = data.read() else: m = 'Data is a {0} not a string or file'.format(type(data)) raise TypeError(m) assert isinstance(_d, str) self._data = _d self._size = len(_d) self._contentType, self._width, self._height = getImageInfo(_d) self._pilImage = None assert isinstance(self._width, int) assert isinstance(self._height, int)
def __call__(self, name, content_type, data): if not content_type and data: content_type, width, height = getImageInfo(data) if not content_type: content_type, encoding = guess_content_type(name, '', '') res = LocalFsFile( name, os.path.join(self.context.abspath, name), content_type) res.__parent__ = self.context return res
def test_getImageInfo_bmp(self): from zope.app.file.image import getImageInfo t, w, h = getImageInfo('BMl\x05\x00\x00\x00\x00\x00\x006\x04\x00\x00(' '\x00\x00\x00\x10\x00\x00\x00\x10\x00\x00\x00' '\x01\x00\x08\x00\x01\x00\x00\x006\x01\x00\x00' '\x12\x0b\x00\x00\x12\x0b\x00\x00\x00\x01\x00' '... and so on ...') self.assertEqual(t, "image/x-ms-bmp") self.assertEqual(w, 16) self.assertEqual(h, 16)
def __init__(self, path='', contentType=''): self.path = path _data = open(os.path.normpath(self.path), 'rb').read() self._size = len(_data) self._fileMode = 'rb' self._encoding = DEFAULT_ENCODING if contentType=='': content_type, encoding = guess_content_type(self.path, _data, '') if content_type.startswith('image/'): self.contentType, width, height = getImageInfo(_data) else: self.contentType = content_type if self.contentType.startswith('text/'): self._fileMode = 'r' if encoding: self._encoding = encoding
def __init__(self, path='', contentType=''): self.path = path with open(os.path.normpath(self.path), 'rb') as f: _data = f.read() self._size = len(_data) self._fileMode = 'rb' encoding = None if contentType == '': content_type, encoding = guess_content_type(self.path, _data, '') if content_type.startswith('image/'): self.contentType, _width, _height = getImageInfo(_data) else: self.contentType = content_type self._encoding = encoding or DEFAULT_ENCODING if self.contentType.startswith('text/'): self._fileMode = 'r'
def _info(self, data): from zope.app.file.image import getImageInfo return getImageInfo(data)
def test_getImageInfo(self): from zope.app.file.image import getImageInfo t, w, h = getImageInfo("\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C")
def f(self, REQUEST, RESPONSE): """ A really short name for a file, enabling fetching a file like: /SOMEFILESAREA/f/FILEID/FILE_NAME Security is effectively handed off to get_file, so use extreme caution. Images may also be handled like this: /SOMEFILESAREA/f/FILEID/resize/WIDTH/HEIGHT/FILE_NAME eg. /myfilearea/f/12211/resize/640/480/myimg.jpg """ requestInfo = RequestInfo(REQUEST) REQUEST.form['id'] = requestInfo.fileId if (self.has_public_access(requestInfo.fileId)): m = 'Allowing access, because "{0}" is still under the '\ 'public-access period.' msg = m.format(requestInfo.fileId) log.info(msg) elif (getSecurityManager().checkPermission('View', self)): m = 'The user has permission to view "{0}".' msg = m.format(requestInfo.fileId) log.info(msg) else: # Lacking permission and the public-access period has passed. # FIXME: handle Forbidden errors better # m = 'You do not have permission to view the file "{0}"' # raise Forbidden(m.format(fileId)) # # For reasons to do with aquisition being awful use the redirector # as the came_from u = '/login.html?came_from=/r/file/{0}' uri = u.format(requestInfo.fileId) m = 'Redirecting to <{0}> because there the public-access for '\ 'file "{1}" has passed, and the user lacks permission.' msg = m.format(uri, requestInfo.fileId) log.info(msg) return self.REQUEST.RESPONSE.redirect(uri) try: if (requestInfo.isSquare or requestInfo.isResize): # we set data_only=True in case the backend uses sendfile data = self.get_file(REQUEST, RESPONSE, data_only=True) else: data = self.get_file(REQUEST, RESPONSE) except Hidden as h: # lint:ok # --=mpj17=-- The post that includes this file is hidden. # By extension, this means the file is hidden. We *could* # just pass up a zExceptions.Unauthorized error up, but # that error would confuse the admins. Instead we will show # a page that explains what is going on. self.REQUEST.form['q'] = self.REQUEST.URL self.REQUEST.form['f'] = self.REQUEST.form.get('id', '') ctx = self.aq_parent.files #ctx = self.Scripts.get.group_object().files retval = getMultiAdapter((ctx, self.REQUEST), name="file_hidden.html")() return retval if not data: fid = self.REQUEST.form.get('id', '') log.warn("No data found for %s." % fid) raise NotFound(self, fid, self.REQUEST) if requestInfo.isImageRequest: content_type, img_width, img_height = getImageInfo(data) img = None if (content_type): # Check to see if we are actually and image sendfile_header = self.get_xsendfile_header(REQUEST, RESPONSE) if ((requestInfo.width == img_width) and (requestInfo.height == img_height)): log.debug("Not resizing image, existing height and width " "were the same as requested size") elif requestInfo.isResize: handler = ImageHandler(requestInfo.width, requestInfo.height, sendfile_header) img = handler.get_image_response(data, REQUEST, RESPONSE) elif requestInfo.isSquare: handler = SquareImageHandler(requestInfo.squareSize, sendfile_header) img = handler.get_image_response(data, REQUEST, RESPONSE) data = data if img is None else img return data
def test_getImageInfo(self): from zope.app.file.image import getImageInfo t, w, h = getImageInfo( "\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C" )