def publishTraverse(self, request, name): """ used for traversal via publisher, i.e. when using as a url """ stack = request.get('TraversalRequestNameStack') if stack and stack[-1] not in self._ignored_stacks: # field and scale name were given... scale = stack.pop() image = self.scale(name, scale) # this is aq-wrapped elif '.' in name: # we got a uid... uid, ext = name.rsplit('.', 1) storage = AnnotationStorage(self.context) info = storage.get(uid) image = None if info is not None: image = self.make(info).__of__(self.context) alsoProvides(image, IStableImageScale) else: # otherwise `name` must refer to a field... scale_view = self.get_image_scale_view() if not scale_view: raise NotFound(self, name, self.request) return scale_view.scale() if image is not None: return image raise NotFound(self, name, self.request)
def publishTraverse(self, request, name): """ used for traversal via publisher, i.e. when using as a url """ stack = request.get('TraversalRequestNameStack') image = None if stack and stack[-1] not in self._ignored_stacks: # field and scale name were given... scale = stack.pop() image = self.scale(name, scale) # this is aq-wrapped elif '-' in name: # we got a uid... if '.' in name: name, ext = name.rsplit('.', 1) storage = AnnotationStorage(self.context) info = storage.get(name) if info is not None: scale_view = ImageScale(self.context, self.request, **info) alsoProvides(scale_view, IStableImageScale) return scale_view.__of__(self.context) else: # otherwise `name` must refer to a field... if '.' in name: name, ext = name.rsplit('.', 1) value = getattr(self.context, name) scale_view = ImageScale( self.context, self.request, data=value, fieldname=name) return scale_view.__of__(self.context) if image is not None: return image raise NotFound(self, name, self.request)
def publishTraverse(self, request, name): """ used for traversal via publisher, i.e. when using as a url """ stack = request.get('TraversalRequestNameStack') image = None if stack and stack[-1] not in self._ignored_stacks: # field and scale name were given... scale = stack.pop() image = self.scale(name, scale) # this is aq-wrapped elif '-' in name: # we got a uid... if '.' in name: name, ext = name.rsplit('.', 1) storage = AnnotationStorage(self.context) info = storage.get(name) if info is not None: scale_view = ImageScale(self.context, self.request, **info) alsoProvides(scale_view, IStableImageScale) return scale_view.__of__(self.context) else: # otherwise `name` must refer to a field... if '.' in name: name, ext = name.rsplit('.', 1) value = getattr(self.context, name) scale_view = ImageScale(self.context, self.request, data=value, fieldname=name) return scale_view.__of__(self.context) if image is not None: return image raise NotFound(self, name, self.request)
def publishTraverse(self, request, name): """ used for traversal via publisher, i.e. when using as a url """ stack = request.get('TraversalRequestNameStack') if stack: # field and scale name were given... scale = stack.pop() image = self.scale(name, scale) # this is aq-wrapped elif '.' in name: # we got a uid... uid, ext = name.rsplit('.', 1) storage = AnnotationStorage(self.context) info = storage.get(uid) image = None if info is not None: image = self.make(info).__of__(self.context) else: # otherwise `name` must refer to a field... field = self.field(name) image = field.get(self.context) # this is aq-wrapped if image is not None: return image raise NotFound(self, name, self.request)
def publishTraverse(self, request, name): """ used for traversal via publisher, i.e. when using as a url """ stack = request.get('TraversalRequestNameStack') if stack: # field and scale name were given... scale = stack.pop() image = self.scale(name, scale) # this is aq-wrapped elif '.' in name: # we got a uid... uid, ext = name.rsplit('.', 1) storage = AnnotationStorage(self.context) info = storage.get(uid) image = None if info is not None: image = self.make(info).__of__(self.context) else: # otherwise `name` must refer to a field... field = self.field(name) image = field.get(self.context) # this is aq-wrapped if image is not None: return image raise NotFound(self, name, self.request)
def _deploy_resources(self, urls, base_path): """ Deploy resources linked in HTML or CSS. """ portal_url = getToolByName(self.context, "portal_url")() for url in urls: url = url.strip() scheme, netloc, path, query, fragment = urlsplit(url) if not path: ## internal anchor continue if netloc and netloc != portal_url: ## external link continue elif path.startswith("image/svg+xml;base64") or path.startswith("image/png;base64"): ## images defined in css continue if path.startswith("/"): objpath = path[1:] else: objpath = os.path.join(base_path, path) if isinstance(objpath, unicode): objpath = objpath.encode("utf-8") # PloneSite with id 'plone' case problems during # restrictedTraverse() so we cut it objpath_spl = objpath.split("/", 1) if objpath_spl[0] == "plone" and len(objpath_spl) > 1: objpath = objpath_spl[1] # fix "../" in paths objpath = os.path.normpath(objpath).replace("%20", " ") if objpath in self.deployed_resources: continue obj = self.context.unrestrictedTraverse(objpath, None) if objpath.rsplit("/", 1)[-1].split(".")[0] == "image": obj = self.context.restrictedTraverse(objpath.rsplit(".", 1)[0], None) if not obj: obj = self.context.restrictedTraverse(unquote(objpath), None) if not obj: parent_obj = self.context.restrictedTraverse(unquote(objpath.rsplit("/", 1)[0]), None) if parent_obj: image_name = objpath.rsplit("/", 1)[-1] if hasattr(parent_obj, "schema"): for field in parent_obj.schema.fields(): fieldname = field.getName() if image_name.startswith(fieldname): scalename = image_name[len(fieldname) + 1 :] obj = field.getScale(parent_obj, scalename) objpath = os.path.join(objpath, "image.jpg") break else: # didn't find it, just go for field name now... # could be added with archetypes.schemaextender parts = image_name.split("_") fieldname = parts[0] field = parent_obj.getField(fieldname) if field and len(parts) == 2: scalename = parts[1] obj = field.getScale(parent_obj, scalename) objpath = os.path.join(objpath, "image.jpg") add_path = True if not obj: if "/@@images/" in objpath: parent_path, image_name = objpath.split("/@@images/") parent_obj = self.context.unrestrictedTraverse(unquote(parent_path), None) if parent_obj: spl_img_name = image_name.split("/") if len(spl_img_name) == 1: # no scalename in path fieldname = spl_img_name[0] scalename = None objpath = "/".join((parent_path, "image.jpg")) else: fieldname, scalename = spl_img_name objpath = os.path.join(parent_path, "_".join((fieldname, scalename)), "image.jpg") try: images_view = getMultiAdapter((parent_obj, self.request), name="images") field = images_view.field(fieldname) if field: obj = field.getScale(parent_obj, scalename) else: # need to try and get it from the uid uid, ext = fieldname.rsplit(".", 1) from plone.scale.storage import AnnotationStorage storage = AnnotationStorage(parent_obj) info = storage.get(uid) if info is not None: obj = images_view.make(info).__of__(parent_obj) # using the exported scale now instead objpath = "/".join((parent_path, fieldname)) add_path = False except ComponentLookupError: pass if not obj: log.warning("Unable to deploy resource '%s'!" % objpath) continue if ( isinstance(obj, ATImage) or hasattr(obj, "getBlobWrapper") and "image" in obj.getBlobWrapper().getContentType() and add_path ): # create path to dump ATImage in original size if objpath.rsplit(".", 1)[-1] in ("png", "jpg", "gif", "jpeg"): objpath = os.path.join(objpath, "image.%s" % (objpath.rsplit(".", 1)[-1])) else: objpath = os.path.join(objpath, "image.jpg") try: content = self._render_obj(obj) except AttributeError: # XXX this can happen with CachedResource? # can't figure out how but let's not error if so... log.warning("Unable to deploy resource '%s'!" % objpath) continue if content is None: continue if ( isinstance(obj, (FSImage, OFSImage, ATImage)) or hasattr(obj, "getBlobWrapper") and "image" in obj.getBlobWrapper().getContentType() ): objpath, content = self._apply_image_transforms(objpath, content) self._write(objpath, content) self.deployed_resources.append(objpath)
def save_images(self, context): """Save images from ZODB to temp directory """ portal = getSite() portal_url = portal.absolute_url() if not portal_url.endswith('/'): portal_url += '/' portal_path = '/'.join(portal.getPhysicalPath()) reference_tool = getToolByName(portal, 'reference_catalog') mtool = getToolByName(portal, 'portal_membership') for filename, image in self.images: size = None # Traverse methods mess with unicode if type(image) is unicode: image = str(image) path = image.replace(portal_url, '') item = None # using uid if 'resolveuid' in image: # uid is the traversed value coming after "resolveuid/" resolveuidpath = image.split('/') resolveuid_idx = resolveuidpath.index('resolveuid') try: uuid = resolveuidpath[resolveuid_idx + 1] except IndexError: logger.error("Failed to get image uid from %s", image) continue item = reference_tool.lookupObject(uuid) if len(resolveuidpath) >= resolveuid_idx + 2: size = resolveuidpath[resolveuid_idx + 2] logger.debug("Get image from uid %s", uuid) if not item: # relative url try: item = context.restrictedTraverse(image) logger.debug("Get image from context") except Unauthorized: logger.warning("Unauthorized to get image from context path %s", item) except: logger.debug("Failed to get image from context path %s", image) if not item: # plone.app.imaging if '@@images' in path and AnnotationStorage: context_path, images_str, uid_filename = path.rsplit('/', 2) image_context = portal.restrictedTraverse(context_path) uid, ext = uid_filename.rsplit('.', 1) storage = AnnotationStorage(image_context) info = storage.get(uid) if info is not None: request = TestRequest() scale_view = ImageScale(image_context, request, **info) item = scale_view.__of__(image_context) if not item: # absolute url image_path = '/'.join((portal_path, path)) try: item = portal.restrictedTraverse(image_path) logger.debug("Get image from portal") except Unauthorized: logger.warning("Unauthorized to get from context path %s", image_path) except: logger.error("Failed to get image from portal path %s", image_path) continue if not mtool.checkPermission('View', item): logger.warning("Unauthorized to get image %s", item) continue if item and size: try: item = item.restrictedTraverse(size) except Unauthorized: logger.warning("Unauthorized to get size %s from image %s", size, image) except: logger.error("Failed to get size %s from image %s", size, image) pass # Eek, we should put an adapter for various image providers (overkill ?). data = get_image_data(item) if data: _write_file(data, self.fsinfo, filename) return
def save_images(self, context): """Save images from ZODB to temp directory """ portal = getSite() portal_url = portal.absolute_url() if not portal_url.endswith('/'): portal_url += '/' portal_path = '/'.join(portal.getPhysicalPath()) reference_tool = getToolByName(portal, 'reference_catalog') mtool = getToolByName(portal, 'portal_membership') for filename, image in self.images: size = None # Traverse methods mess with unicode if type(image) is unicode: image = str(image) path = image.replace(portal_url, '') item = None # using uid if 'resolveuid' in image: # uid is the traversed value coming after "resolveuid/" resolveuidpath = image.split('/') resolveuid_idx = resolveuidpath.index('resolveuid') try: uuid = resolveuidpath[resolveuid_idx + 1] except IndexError: logger.error("Failed to get image uid from %s", image) continue item = reference_tool.lookupObject(uuid) if len(resolveuidpath) >= resolveuid_idx + 2: size = resolveuidpath[resolveuid_idx + 2] logger.debug("Get image from uid %s", uuid) if not item: # relative url try: item = context.restrictedTraverse(image) logger.debug("Get image from context") except Unauthorized: logger.warning( "Unauthorized to get image from context path %s", item) except: logger.debug("Failed to get image from context path %s", image) if not item: # plone.app.imaging if '@@images' in path and AnnotationStorage: context_path, images_str, uid_filename = path.rsplit( '/', 2) image_context = portal.restrictedTraverse(context_path) uid, ext = uid_filename.rsplit('.', 1) storage = AnnotationStorage(image_context) info = storage.get(uid) if info is not None: request = TestRequest() scale_view = ImageScale(image_context, request, **info) item = scale_view.__of__(image_context) if not item: # absolute url image_path = '/'.join((portal_path, path)) try: item = portal.restrictedTraverse(image_path) logger.debug("Get image from portal") except Unauthorized: logger.warning("Unauthorized to get from context path %s", image_path) except: logger.error("Failed to get image from portal path %s", image_path) continue if not mtool.checkPermission('View', item): logger.warning("Unauthorized to get image %s", item) continue if item and size: try: item = item.restrictedTraverse(size) except Unauthorized: logger.warning("Unauthorized to get size %s from image %s", size, image) except: logger.error("Failed to get size %s from image %s", size, image) pass # Eek, we should put an adapter for various image providers (overkill ?). data = get_image_data(item) if data: _write_file(data, self.fsinfo, filename) return