[preview] Adding detection of supported mime types at runtime

This commit is contained in:
Comrade DOS
2020-03-13 14:09:34 +06:00
committed by lovetox
parent 834d3713b8
commit 08cd37b814

View File

@@ -26,6 +26,7 @@ from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GLib from gi.repository import GLib
from gi.repository import Soup from gi.repository import Soup
from gi.repository import GdkPixbuf
from gajim.common import app from gajim.common import app
from gajim.common import configpaths from gajim.common import configpaths
@@ -52,6 +53,7 @@ ERROR_MSG = None
try: try:
from PIL import Image # pylint: disable=unused-import from PIL import Image # pylint: disable=unused-import
except ImportError: except ImportError:
Image = None
log.error('Pillow not available') log.error('Pillow not available')
ERROR_MSG = _('Please install python-pillow') ERROR_MSG = _('Please install python-pillow')
@@ -73,14 +75,21 @@ if ERROR_MSG is None:
from url_image_preview.utils import filename_from_uri from url_image_preview.utils import filename_from_uri
# pylint: enable=ungrouped-imports # pylint: enable=ungrouped-imports
ACCEPTED_MIME_TYPES = [ def get_accepted_mime_types():
'image/png', accepted_mime_types = set()
'image/jpeg', for fmt in GdkPixbuf.Pixbuf.get_formats():
'image/gif', for mime_type in fmt.get_mime_types():
'image/raw', accepted_mime_types.add(mime_type.lower())
'image/svg+xml', if Image is not None:
'image/x-ms-bmp', Image.init()
] for mime_type in Image.MIME.values():
accepted_mime_types.add(mime_type.lower())
return tuple(filter(
lambda mime_type: mime_type.startswith('image'),
accepted_mime_types
))
ACCEPTED_MIME_TYPES = get_accepted_mime_types()
class UrlImagePreviewPlugin(GajimPlugin): class UrlImagePreviewPlugin(GajimPlugin):