# HG changeset patch # User Thierry Florac # Date 1529420977 -7200 # Node ID 49e132e15bbc1cbdd52065089b507d3ba5b81c80 # Parent ee6515f0d9ae833a5d985c48d9d412525c4de611 Added support to store SVG files as images diff -r ee6515f0d9ae -r 49e132e15bbc src/pyams_file/file.py --- a/src/pyams_file/file.py Tue Jun 19 17:06:49 2018 +0200 +++ b/src/pyams_file/file.py Tue Jun 19 17:09:37 2018 +0200 @@ -25,7 +25,7 @@ from PIL import Image # import interfaces -from pyams_file.interfaces import IFile, IImage, IVideo, IAudio, IFileInfo, FileModifiedEvent +from pyams_file.interfaces import IFile, IImage, ISVGImage, IVideo, IAudio, IFileInfo, FileModifiedEvent from zope.copy.interfaces import ICopyHook, ResumeCopy from zope.location.interfaces import IContained @@ -255,6 +255,11 @@ request.registry.notify(FileModifiedEvent(self)) +@implementer(ISVGImage) +class SVGImageFile(File): + """SVG image file persistent object""" + + @implementer(IVideo) class VideoFile(File): """Video file persistent object""" @@ -292,7 +297,9 @@ content-type recognition """ content_type = get_magic_content_type(data) - if content_type.startswith('image/'): + if content_type == 'image/svg': + factory = SVGImageFile + elif content_type.startswith('image/'): factory = ImageFile elif content_type.startswith('video/'): factory = VideoFile diff -r ee6515f0d9ae -r 49e132e15bbc src/pyams_file/interfaces/__init__.py --- a/src/pyams_file/interfaces/__init__.py Tue Jun 19 17:06:49 2018 +0200 +++ b/src/pyams_file/interfaces/__init__.py Tue Jun 19 17:09:37 2018 +0200 @@ -68,7 +68,11 @@ """Multimedia file""" -class IImage(IMediaFile): +class IBaseImage(IMediaFile): + """Base image interface""" + + +class IImage(IBaseImage): """Image object interface""" def get_image_size(self): @@ -81,12 +85,16 @@ """Crop image to given coordinates""" +class ISVGImage(IBaseImage): + """SVG file interface""" + + class IResponsiveImage(Interface): """Responsive image marker interface""" class IVideo(IMediaFile): - """Video object interface""" + """Video file interface""" class IAudio(IMediaFile): diff -r ee6515f0d9ae -r 49e132e15bbc src/pyams_file/schema.py --- a/src/pyams_file/schema.py Tue Jun 19 17:06:49 2018 +0200 +++ b/src/pyams_file/schema.py Tue Jun 19 17:09:37 2018 +0200 @@ -15,8 +15,8 @@ # import standard library # import interfaces -from pyams_file.interfaces import IFile, IFileField, IMediaFile, IMediaField, IImage, IImageField, IVideo, IVideoField, \ - IAudio, IAudioField, IThumbnailMediaField, IThumbnailImageField, IThumbnailVideoField, DELETED_FILE +from pyams_file.interfaces import IFile, IFileField, IMediaFile, IMediaField, IBaseImage, IImageField, IVideo, \ + IVideoField, IAudio, IAudioField, IThumbnailMediaField, IThumbnailImageField, IThumbnailVideoField, DELETED_FILE from z3c.form.interfaces import NOT_CHANGED from zope.schema.interfaces import WrongType, RequiredMissing @@ -66,7 +66,7 @@ class ImageField(MediaField): """Custom field used to handle image properties""" - schema = IImage + schema = IBaseImage @implementer(IThumbnailImageField) diff -r ee6515f0d9ae -r 49e132e15bbc src/pyams_file/thumbnail.py --- a/src/pyams_file/thumbnail.py Tue Jun 19 17:06:49 2018 +0200 +++ b/src/pyams_file/thumbnail.py Tue Jun 19 17:09:37 2018 +0200 @@ -25,6 +25,7 @@ # import packages from persistent.dict import PersistentDict from pyams_file.file import FileFactory +from pyams_file.zmi.image import render_image from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config, get_annotation_adapter from pyams_utils.registry import query_utility from pyams_utils.request import check_request @@ -243,9 +244,27 @@ @adapter_config(name='thumbnails', context=(Interface, Interface, Interface), provides=ITALESExtension) class ThumbnailsExtension(ContextRequestViewAdapter): - """extension:thumbnails(image) TALES extension""" + """extension:thumbnails(image) TALES extension + + This TALES extension returns the IThumbnails adapter of given image. + """ def render(self, context=None): if context is None: context = self.context return IThumbnail(context, None) + + +@adapter_config(name='thumbnail', context=(Interface, Interface, Interface), provides=ITALESExtension) +class ThumbnailExtension(ContextRequestViewAdapter): + """extension:thumbnail(image, width, height) TALES extension + + This TALES extension doesn't return an adapter but HTML code matching given image and dimensions. + If image is a classic image, an "img" tag with source to thumbnail of required size is returned. + If image in an SVG image, a "div" is returned containing whole SVG data of given image. + """ + + def render(self, context=None, width=None, height=None, css_class=''): + if context is None: + context = self.context + return render_image(context, width, height, self.request, css_class) diff -r ee6515f0d9ae -r 49e132e15bbc src/pyams_file/widget/templates/media-input.pt --- a/src/pyams_file/widget/templates/media-input.pt Tue Jun 19 17:06:49 2018 +0200 +++ b/src/pyams_file/widget/templates/media-input.pt Tue Jun 19 17:09:37 2018 +0200 @@ -19,7 +19,32 @@ Delete content - + +
+ Thumbnail +
+
+ Current value: + +  –  + +
+ +
+
+ + +