4teamwork / ftw.zipexport

Zip Export for Plone
2 stars 6 forks source link

Enhancement: FileSystem representation #43

Open mtrebron opened 7 years ago

mtrebron commented 7 years ago

I have a use case where pdf files on the filesystem need to be zipped:

browser/configure.zcml

    <adapter factory=".filesystem.FileZipRepresentation" />

interfaces.py

    enabled_dotted_names = schema.List(
        title=u"Interface dotted-names on which zipexport is enabled.",
        default=[u"Products.CMFCore.interfaces._content.IContentish"
               , u"Products.CMFCore.interfaces._tools.IDirectoryView"],
        value_type=schema.TextLine())

representations/filesystem.py

from ftw.zipexport.representations.general import NullZipRepresentation
from OFS.interfaces import IItem
from StringIO import StringIO
from Products.CMFPlone.utils import safe_unicode
from zope.component import adapts
from zope.interface import implements
from zope.interface import Interface

class FileZipRepresentation(NullZipRepresentation):
    """
    filesystem zip export implementation
    note, todo: returns img src link for image, not image itself
    """
    implements(IZipRepresentation)
    adapts(IItem, Interface)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def get_files(self, path_prefix=u"", recursive=True, toplevel=True):
        filename = safe_unicode(self.context.getId() or self.context.id)
        yield self.get_file_tuple(filename, path_prefix)

    def get_file_tuple(self, filename, path_prefix):
        path = u'{0}/{1}'.format(safe_unicode(path_prefix),
                                 safe_unicode(filename))
        stream_data = StringIO(self.context)
        return (path, stream_data)