NamedBlobFile vs. NamedBlobImage

I've got the exact same issue again in 2020 with plone.scale-3.1.1. The images is show properly with @@images/myfield but not with @@images/myfield/small or other sizes. This happens for example if I change the status of the object from private to published.

Unfortunately there is also no exception showing up.

I solved it by using plone.namedfile.field.NamedBlobImage instead of plone.namedfile.field.NamedBlobFile as the field with the image. Unfortunately after the code change you have to migrate all the field values manually or it will result in an exception if you try to edit an object of that type. I created a small code snippet for that:

from plone import api
from my.addon.content.book import IBook
from plone.namedfile.file import NamedBlobImage
import logging
logger = logging.getLogger(__name__)
def fixBook():
    """ Iterates over all book objects and changes the NamedBlobFile to NamedBlobImage if neccessary
    """

    books = api.content.find(object_provides = IBook)
    for book in books:
        bookObj = book.getObject()

        if not hasattr(bookObj, 'cover'):
            logger.warning("Book has no cover field: {book.getURL()}")
            continue

        if bookObj.cover and not isinstance(bookObj.cover, NamedBlobImage):
            bookObj.cover = NamedBlobImage(
                data = bookObj.cover.data,
                contentType = bookObj.cover.contentType,
                filename = bookObj.cover.filename)
            logger.info("Fixed cover for {book.getURL()}")

Indeed, NamedBlobFile is jst the wrong field.

I just overlooked that because it worked fine since weeks. It was just old code. Someone created the field as NamedBlobFile and it somehow worked. But since a few days it does not anymore. :smiley: Very weird that it worked in the first place.
I just wanted you to know and make the solution public.

1 Like