skip to navigation
skip to content

z3c.blobfile 0.1.5

File and Image Using Blob Support of ZODB -- Zope 3 Content Components

Blob File

This package provides an implementation of which uses the Blob support introduced in ZODB 3.8. It’s main purpose is to provide an easy migration path for existing instances. For more advanced file implementations see zope.file and z3c.extfile.

The standard implementation in uses chunk objects to break big files into manageable parts. These chunks flow the server caches whereas blobs are directly consumed by the publisher. The main difference between this blob implementation and the old implementation can be seen in a replacement of the chunk objects by Blobs.

Detailed Documentation

Blob File Implementation

This implementation of takes advantage of the new ZODB blob support and tries to be completely backward compatible to the existing file implementation in

Compatibility with

The following tests mimic exactly the tests of the package.

Let’s test the constructor:

>>> from import File, Image
>>> file = File()
>>> file.contentType
>>> file = File('Foobar')
>>> file.contentType
>>> file = File('Foobar', 'text/plain')
>>> file.contentType
>>> file = File(data='Foobar', contentType='text/plain')
>>> file.contentType

Let’s test the mutators:

>>> file = File()
>>> file.contentType = 'text/plain'
>>> file.contentType
>>> = 'Foobar'
>>> = None
Traceback (most recent call last):
TypeError: Cannot set None data on a file.

Let’s test large data input:

>>> file = File()

Insert as string:

>>> = 'Foobar'*60000
>>> file.getSize()
>>> == 'Foobar'*60000

Insert data as FileChunk:

>>> from import FileChunk
>>> fc = FileChunk('Foobar'*4000)
>>> = fc
>>> file.getSize()
>>> == 'Foobar'*4000

Insert data from file object:

>>> import cStringIO
>>> sio = cStringIO.StringIO()
>>> sio.write('Foobar'*100000)
>>> = sio
>>> file.getSize()
>>> == 'Foobar'*100000

Last, but not least, verify the interface:

>>> from zope.interface.verify import verifyClass
>>> verifyClass(, File)

Test of Filerepresentation Adapters

>>> from import FileReadFile
>>> file = File()
>>> content = "This is some file\\ncontent."
>>> = content
>>> file.contentType = "text/plain"
>>> FileReadFile(file).read() == content
>>> FileReadFile(file).size() == len(content)
>>> from import FileWriteFile
>>> file = File()
>>> content = "This is some file\\ncontent."
>>> FileWriteFile(file).write(content)
>>> == content

Evolution of Existing Files

Please note that the following code is experimental. The code should not be used in production without careful testing. Backup your Data.fs and uncomment the following lines in the configure.zcml if you want to replace exiting instances.

component=”.generations.BlobFileSchemaManager” name=”z3c.blobfile” />

Let’s assume that you have already an existing database with content types:

>>> from z3c.blobfile import testing
>>> root = getRootFolder()
>>> root[u'file'] = File('A text file', contentType='text/plain')
>>> root[u'image'] = Image(testing.zptlogo)

Since the evolve step looks for implementations of IFile we also must provide a way to recognize other implementations than and Image. Let’s add a nonsense implementation as an example:

>>> root[u'custom'] = testing.MyFile()

Note that we cannot assume that these objects exist in isolation. Many of them probably are annotated, indexed, some even may be registered as utility etc. The evolution step throws the standard events when the objects are replaced and it’s up the application that this replacement is recognized accordingly. If your application has special needs you may subscribe to the FileReplacedEvent.

We will not test all relations to all other objects, since this is largely application dependent. Here we only take the ZopeDublinCore timestamps as an example that our evolution step leaves as many things untouched as possible.

>>> from zope.dublincore.interfaces import IZopeDublinCore
>>> import datetime
>>> IZopeDublinCore(root[u'file']).created = datetime.datetime.utcnow()
>>> t1 = IZopeDublinCore(root[u'file']).created
>>> IZopeDublinCore(root[u'file']).title = u'No evolution'

Now we perform the basic evolution steps. Since we expect some warning logs we need

>>> from zope.testing.loggingsupport import InstalledHandler
>>> import logging
>>> log_handler = InstalledHandler('z3c.blobfile.generations')
>>> from z3c.blobfile.generations.evolve1 import evolveZopeAppFile
>>> evolveZopeAppFile(root)
>>> transaction.commit()
>>> for record in log_handler.records:
...     print record.getMessage()
Unknown ...interfaces.IFile implementation z3c.blobfile.testing.MyFile

After the evolution step the class types have changed to the z3c.blobfile implementations:

>>> import z3c.blobfile
>>> isinstance(root[u'file'], z3c.blobfile.file.File)
>>> isinstance(root[u'image'], z3c.blobfile.image.Image)

Only the custom implementations remain untouched:

>>> isinstance(root[u'custom'], testing.MyFile)

The file data remain the same …

>>> root[u'file'].data
'A text file'
>>> root[u'file'].contentType
>>> root[u'image'].data == testing.zptlogo

and so do the annotations:

>>> IZopeDublinCore(root[u'file']).created == t1
>>> IZopeDublinCore(root[u'file']).title
u'No evolution'

Even implementation details like the _data attribute still work:

>>> root[u'file']._data
'A text file'


Let’s check that blob files can be copied successfully. This functionality requires the new “zope.copy” package and the version of zope.copypastemove that uses zope.copy (>=3.5.1).

>>> from zope.copy import copy
>>> file = z3c.blobfile.file.File()
>>> = u'hello, world'
>>> image = z3c.blobfile.file.File()
>>> = 'some image bytes'
>>> transaction.commit()
>>> file_copy = copy(file)
>>> ==
>>> image_copy = copy(image)
>>> ==


0.1.5 (2011-02-11)

  • Put a ceiling (64k) on the number of info bytes read and read those bytes in one shot.
  • Bug: Correctly detect the dimensions of JPEG images with the dimensions in a position greater than IMAGE_INFO_BYTES.
  • Made tests compatible with ZODB 3.9.

0.1.4 (2009-03-03)

  • Add copy hook for zope.copy to fix blob file/image copying.
  • Change package’s mailing list address to zope-dev at instead of retired zope3-dev.

0.1.3 (2008-10-20)

  • Bug: Don’t get/set image data twice on Image initialization.
  • Cache file size and invalidate it on write access.
  • Download file/image using new openDetached method that provides read-only file-like access to the blob, detached from the database connection.

0.1.2 (2008-09-11)

  • Bug: Set i18n domain in “overrides.zcml” to avoid warnings.
  • Don’t expose Blob implementation details in add/upload forms. Also, “zope” translation domain has no translations for “Upload a File (Blob)”, it just has “Upload a File” translations, so use it.

0.1.1 (2008-09-08)

  • Bug: Register as an adapter for BlobFile, making optimized file views really work.
  • Bug: Also use optimized file view for z3c.blobfile.image.Image.
  • Bug: Make blob versions of File and Image implement IBlobFile and IBlobImage interfaces respectively. This enables z3c.blobfile’s views.
  • Bug: Use local ZopeMessageFactory. This fixes import error on Zope 2.10

0.1.0 (2008-02-27)

  • Initial Release
File Type Py Version Uploaded on Size (md5) Source 2011-02-11 39KB