Created
November 3, 2016 15:12
-
-
Save jensens/4bbbed4237cf009e15220da51b9b5d33 to your computer and use it in GitHub Desktop.
Plone Image Proxy View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from plone import api | |
from plone.namedfile.interfaces import IStableImageScale | |
from plone.namedfile.scaling import ImageScale | |
from plone.scale.storage import AnnotationStorage | |
from Products.Five import BrowserView | |
from zExceptions import NotFound | |
from zope.interface import alsoProvides | |
from zope.interface import implementer | |
from zope.publisher.interfaces import IPublishTraverse | |
import re | |
RE = re.compile('.*/@@images/(.*?)"') | |
@implementer(IPublishTraverse) | |
class ImageProxyView(BrowserView): | |
def __call__(self): | |
if len(self.subpath) > 1: | |
field = self.subpath[1] | |
if '-' in field: #we got UID | |
if '.' in field: | |
field, ext = field.rsplit('.', 1) | |
imgUid = self.subpath[0] | |
img = api.content.get(UID=imgUid) | |
storage = AnnotationStorage(img) | |
info = storage.get(field) | |
if info is not None: | |
scale_view = ImageScale(img, self.request, **info) | |
alsoProvides(scale_view, IStableImageScale) | |
return scale_view.__of__(self.context).index_html() | |
return NotFound('UID invalid') | |
else: | |
field = None | |
if len(self.subpath) > 2: | |
scale = self.subpath[2] | |
else: | |
scale = None | |
return self.image_util.scale(fieldname=field, scale= scale).index_html() | |
def publishTraverse(self, request, name): | |
if not hasattr(self, 'subpath'): | |
self.subpath = [] | |
self.subpath.append(name) | |
return self | |
@property | |
def image_util(self): | |
if len(self.subpath) < 1: | |
raise(ValueError) | |
imgUid = self.subpath[0] | |
img = api.content.get(UID=imgUid) | |
return api.content.get_view(context=img, request=self.request, name='images') | |
def tag(self, | |
fieldname=None, | |
scale=None, | |
height=None, | |
width=None, | |
direction='thumbnail', | |
uid=None, | |
**kwargs): | |
img = api.content.get(UID=uid) | |
imgutil = api.content.get_view(context=img, request=self.request, name='images') | |
tag = imgutil.tag(fieldname=fieldname, scale=scale,height=height,width=width,direction=direction,**kwargs) | |
#import pdb; pdb.set_trace() | |
match = RE.match(tag) | |
return self.context.absolute_url() + '/@@imageproxy/' + uid + '/' + match.group(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment