Esempio n. 1
0
def image_update_data(self, data, content_type=None, size=None):
    if isinstance(data, unicode):
        raise TypeError('Data can only be str or file-like.  '
                        'Unicode objects are expressly forbidden.')
    
    if size is None: size=len(data)

    self.size=size
    if data:
        if IBlobStorage.providedBy(self._p_jar.db().storage):
            self.data = ZODB.blob.Blob(data)
        else:
            self.data = data
    else:
        self.data = ''

    ct, width, height = getImageInfo(data)
    if ct:
        content_type = ct
    if width >= 0 and height >= 0:
        self.width = width
        self.height = height

    # Now we should have the correct content type, or still None
    if content_type is not None: self.content_type = content_type

    self.ZCacheable_invalidate()
    self.ZCacheable_set(None)
    self.http__refreshEtag()
Esempio n. 2
0
    def _update(self, portal):

        brains = portal.portal_catalog(meta_type='Naaya Photo')

        for brain in brains:
            obj = brain.getObject()
            self.log.info("Migrating object %s", obj.absolute_url())
            for extfile in obj.objectValues('ExtFile'):
                self.log.info("Migrating extfile %s", extfile.absolute_url())
                data = extfile.index_html()
                filename = extfile.id
                obj.manage_delObjects([filename])
                child_id = obj.manage_addFile(filename)
                child = obj._getOb(child_id)
                child.content_type, child.width, child.height = getImageInfo(
                    data)
                blob = child.open_write()
                blob.write(data)
                blob.seek(0)
                blob.close()
                child.size = len(data)
                self.log.info('Migrated to bfile: %s' % child)

        self.log.info('Migration done')

        return True
Esempio n. 3
0
    def update_data(self,
                    data,
                    content_type=None,
                    size=None,
                    filename='Original',
                    purge=True,
                    watermark=False):
        if purge:
            self.manage_delObjects(self.objectIds())
        filename = clean_display_id(filename)
        filename = self._getDisplayId(filename, watermark)
        if filename in self.objectIds():
            self.manage_delObjects([filename])

        child_id = self.manage_addFile(filename)
        child = self._getOb(child_id)
        if hasattr(data, '__class__') and data.__class__ is Pdata:
            data = str(data)
        elif getattr(data, 'index_html', None):
            data = data.index_html()

        if not isinstance(data, str):
            data = data.read()
        child.content_type, child.width, child.height = getImageInfo(data)

        child.manage_upload(data, child.content_type)
        return child.getId()
    def _update(self, portal):

        brains = portal.portal_catalog(
            meta_type=['Naaya Photo', 'Naaya Media File'])

        for brain in brains:
            obj = brain.getObject()
            self.log.info("Migrating object %s", obj.absolute_url())
            for extfile in obj.objectValues('ExtFile'):

                if extfile.is_broken():
                    self.log.warning(
                        "\t BROKEN EXTFILE: Couldn't migrate extfile for %s "
                        "because of broken file", extfile.absolute_url())
                    continue

                self.log.info("Migrating extfile %s", extfile.absolute_url())
                data = extfile.index_html()
                filename = extfile.id
                obj.manage_delObjects([filename])
                child_id = obj.manage_addFile(filename)
                child = obj._getOb(child_id)
                child.content_type, child.width, child.height = getImageInfo(
                    data)
                blob = child.open_write()
                blob.write(data)
                blob.seek(0)
                blob.close()
                child.size = len(data)
                self.log.info('Migrated to bfile: %s' % child)

        self.log.info('Migration done')

        return True
    def _update(self, portal):

        brains = portal.portal_catalog(meta_type=['Naaya Photo',
                                                  'Naaya Media File'])

        for brain in brains:
            obj = brain.getObject()
            self.log.info("Migrating object %s", obj.absolute_url())
            for extfile in obj.objectValues('ExtFile'):

                if extfile.is_broken():
                    self.log.warning(
                        "\t BROKEN EXTFILE: Couldn't migrate extfile for %s "
                        "because of broken file", extfile.absolute_url()
                    )
                    continue

                self.log.info("Migrating extfile %s", extfile.absolute_url())
                data = extfile.index_html()
                filename = extfile.id
                obj.manage_delObjects([filename])
                child_id = obj.manage_addFile(filename)
                child = obj._getOb(child_id)
                child.content_type, child.width, child.height = getImageInfo(
                    data)
                blob = child.open_write()
                blob.write(data)
                blob.seek(0)
                blob.close()
                child.size = len(data)
                self.log.info('Migrated to bfile: %s' % child)

        self.log.info('Migration done')

        return True
Esempio n. 6
0
    def _update(self, portal):

        brains = portal.portal_catalog(meta_type="Naaya Photo")

        for brain in brains:
            obj = brain.getObject()
            self.log.info("Migrating object %s", obj.absolute_url())
            for extfile in obj.objectValues("ExtFile"):
                self.log.info("Migrating extfile %s", extfile.absolute_url())
                data = extfile.index_html()
                filename = extfile.id
                obj.manage_delObjects([filename])
                child_id = obj.manage_addFile(filename)
                child = obj._getOb(child_id)
                child.content_type, child.width, child.height = getImageInfo(data)
                blob = child.open_write()
                blob.write(data)
                blob.seek(0)
                blob.close()
                child.size = len(data)
                self.log.info("Migrated to bfile: %s" % child)

        self.log.info("Migration done")

        return True
Esempio n. 7
0
 def _get_content_type(self, file, body, id, content_type=None):
     """ Determine the mime-type """
     ct, w, h = getImageInfo(body)
     if ct:
         content_type = ct
     else:
         content_type = super(ExtImage, self)._get_content_type(file, body, id, content_type)
     return content_type
Esempio n. 8
0
 def _readFile(self, reparse):
     data = self._data = self._readFileAsResourceOrDirect()
     if reparse or self.content_type == 'unknown/unknown':
         self.ZCacheable_invalidate()
         ct, width, height = getImageInfo(data)
         self.content_type = ct
         self.width = width
         self.height = height
     return data
Esempio n. 9
0
 def _readFile(self, reparse):
     data = self._data = self._readFileAsResourceOrDirect()
     if reparse or self.content_type == 'unknown/unknown':
         self.ZCacheable_invalidate()
         ct, width, height = getImageInfo( data )
         self.content_type = ct
         self.width = width
         self.height = height
     return data
Esempio n. 10
0
 def _get_content_type(self, file, body, id, content_type=None):
     """ Determine the mime-type """
     ct, w, h = getImageInfo(body)
     if ct:
         content_type = ct
     else:
         content_type = super(ExtImage, self)._get_content_type(
             file, body, id, content_type)
     return content_type
Esempio n. 11
0
def getImageSize(img):
    """ determine the dimensions for the given image file """
    if hasPIL:
        try:
            return iopen(img).size
        except IOError:
            return 0, 0
    else:
        data = img.read(32)
        return getImageInfo(data)[1:]
Esempio n. 12
0
 def _get_content_type(self, file, body, id, content_type=None):
     """ Determine the mime-type """
     from OFS.Image import getImageInfo
     ct, w, h = getImageInfo(body)
     if ct: 
         content_type = ct
     else:
         content_type = ExtImage.inheritedAttribute('_get_content_type')(self, 
                                                 file, body, id, content_type)
     return content_type
Esempio n. 13
0
def getImageSize(img):
    """ determine the dimensions for the given image file """
    if hasPIL:
        try:
            return iopen(img).size
        except IOError:
            return 0, 0
    else:
        data = img.read(32)
        return getImageInfo(data)[1:]
Esempio n. 14
0
def PUT_factory(self, name, typ, body):
    '''Creates ExtImage instead of plain Image.'''
    ct, w, h = getImageInfo(body)
    if ct:
        major, minor = string.split(ct, '/')
        if major == 'image':
            return ExtImage(name, '', '')
    major, minor = string.split(typ, '/')
    if major == 'image':
        return ExtImage(name, '', '')
    return None
Esempio n. 15
0
 def _readFile(self, reparse):
     fp = expandpath(self._filepath)
     file = open(fp, 'rb')
     try: data = file.read()
     finally: file.close()
     if reparse or self.content_type == 'unknown/unknown':
         ct, width, height = getImageInfo( data )
         self.content_type = ct
         self.width = width
         self.height = height
     return data
Esempio n. 16
0
def PUT_factory(self, name, typ, body):
    '''Creates ExtImage instead of plain Image.'''
    ct, w, h = getImageInfo(body)
    if ct:
        major, minor = string.split(ct, '/')
        if major == 'image':
            return ExtImage(name, '', '')
    major, minor = string.split(typ, '/')
    if major == 'image':
        return ExtImage(name, '', '')
    return None
Esempio n. 17
0
 def update_data(self, data, content_type=None, size=None):
     file = data
     data, size = self._read_data(data)
     content_type = self._get_content_type(file, data, self.__name__,
                                           'application/octet-stream')
     ct, width, height = getImageInfo(data)
     if ct:
         content_type = ct
     if width >= 0 and height >= 0:
         self.width = width
         self.height = height
     NyFSImage.inheritedAttribute('update_data')(self, data, content_type,
                                                 size)
Esempio n. 18
0
 def update_data(self, data, content_type=None, size=None, filename=""):
     file = data
     data, size = self._read_data(data)
     if not filename:
         filename = getattr(file, "filename", "")
     content_type = self._get_content_type(file, data, self.__name__, "application/octet-stream")
     ct, width, height = getImageInfo(data)
     if ct:
         content_type = ct
     if width >= 0 and height >= 0:
         self.width = width
         self.height = height
     NyFSImage.inheritedAttribute("update_data")(self, data, content_type, size, filename)
Esempio n. 19
0
    def _readFile(self, reparse):
        """Read the data from the filesystem.
        """
        file = open(self._filepath, 'rb')
        try:
            data = self._data = file.read()
        finally:
            file.close()

        if reparse or self.content_type == 'unknown/unknown':
            self.ZCacheable_invalidate()
            ct, width, height = getImageInfo( data )
            self.content_type = ct
            self.width = width
            self.height = height
        return data
Esempio n. 20
0
  def _update_image_info(self):
    """
      This method tries to determine the content type of an image and
      its geometry. It uses currently OFS.Image for this purpose.
      However, this method is known to be too simplistic.

      TODO:
      - use image magick or PIL
    """
    self.size = len(self.data)
    content_type, width, height = getImageInfo(self.data)
    if not content_type:
      if self.size >= 30 and self.data[:2] == 'BM':
        header = struct.unpack('<III', self.data[14:26])
        if header[0] >= 12:
          content_type = 'image/x-bmp'
          width, height = header[1:]
    self.height = height
    self.width = width
    self._setContentType(content_type or 'application/unknown')
Esempio n. 21
0
    def _update_image_info(self):
        """
      This method tries to determine the content type of an image and
      its geometry. It uses currently OFS.Image for this purpose.
      However, this method is known to be too simplistic.

      TODO:
      - use image magick or PIL
    """
        self.size = len(self.data)
        content_type, width, height = getImageInfo(self.data)
        if not content_type:
            if self.size >= 30 and self.data[:2] == 'BM':
                header = struct.unpack('<III', self.data[14:26])
                if header[0] >= 12:
                    content_type = 'image/x-bmp'
                    width, height = header[1:]
        self.height = height
        self.width = width
        self._setContentType(content_type or 'application/unknown')
Esempio n. 22
0
    def update_data(self, data, content_type=None, size=None, filename='Original', purge=True, watermark=False):
        if purge:
            self.manage_delObjects(self.objectIds())
        filename = clean_display_id(filename)
        filename = self._getDisplayId(filename, watermark)
        if filename in self.objectIds():
            self.manage_delObjects([filename])

        child_id = self.manage_addFile(filename)
        child = self._getOb(child_id)
        if hasattr(data, '__class__') and data.__class__ is Pdata:
            data = str(data)
        elif getattr(data, 'index_html', None):
            data = data.index_html()
        
        if not isinstance(data, str):
            data = data.read()
        child.content_type, child.width, child.height = getImageInfo(data)
        
        child.manage_upload(data, child.content_type)
        return child.getId()
Esempio n. 23
0
    def _readFile(self, reparse):
        """Read the data from the filesystem.
        """
        file = open(self._filepath, 'rb')
        try:
            data = self._data = file.read()
        finally:
            file.close()

        if reparse or self.content_type == 'unknown/unknown':
            try:
                mtime = os.stat(self._filepath).st_mtime
            except:
                mtime = 0.0
            if mtime != self._file_mod_time or mtime == 0.0:
                self.ZCacheable_invalidate()
                self._file_mod_time = mtime
            ct, width, height = getImageInfo(data)
            self.content_type = ct
            self.width = width
            self.height = height
        return data
Esempio n. 24
0
    def update_data(self,
                    data,
                    content_type=None,
                    size=None,
                    filename='Original',
                    purge=True,
                    watermark=False):

        if hasattr(data, '__class__') and data.__class__ is Pdata:
            data = str(data)
        elif getattr(data, 'index_html', None):
            data = data.index_html()

        if not isinstance(data,
                          str):  #dealing with a blobstreamiterator or StringIO
            data = data.read()

        if purge:
            self.manage_delObjects(self.objectIds())

        filename = clean_display_id(filename)
        filename = self._getDisplayId(filename, watermark)

        if filename in self.objectIds():
            self.manage_delObjects([filename])

        child_id = self.manage_addFile(filename)
        child = self._getOb(child_id)
        child.content_type, child.width, child.height = getImageInfo(data)

        #ZZZ: migration
        #child.manage_upload(data, child.content_type)
        blob = child.open_write()
        blob.write(data)
        blob.seek(0)
        blob.close()
        child.size = len(data)
        return child.getId()
Esempio n. 25
0
 def _update_image_info(self):
     """
   This method tries to determine the content type of an image and
   its geometry.
 """
     self.size = len(self.data)
     content_type, width, height = getImageInfo(self.data)
     if not content_type:
         try:
             image = PIL.Image.open(StringIO(str(self.data)))
         except IOError:
             width = height = -1
             content_type = 'application/unknown'
         else:
             width, height = image.size
             content_type = image.get_format_mimetype()
             # normalize the mimetype using the registry
             mimetype_list = self.getPortalObject(
             ).mimetypes_registry.lookup(content_type)
             if mimetype_list:
                 content_type = mimetype_list[0].normalized()
     self.height = height
     self.width = width
     self._setContentType(content_type)
Esempio n. 26
0
def validateLogoIsImage(value):
    ct, w, h = getImageInfo(value)
    if ct == '':
        raise ImageValidationError(value)
    return True
def validateIsImage(value):
    ct, w, h = getImageInfo(value)
    if ct == '':
        raise ImageValidationError(value)
    return True