annotate libervia/backend/plugins/plugin_xep_0264.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0264.py@524856bd7b19
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3346
4f1fcee83d36 plugin XEP-0264: only log "fix orientation" when the option is used
Goffi <goffi@goffi.org>
parents: 3330
diff changeset
3 # SàT plugin for managing xep-0264
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3346
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import threads
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.python.failure import Failure
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
28 from zope.interface import implementer
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import disco, iwokkel
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 from libervia.backend.core import exceptions
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import hashlib
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 try:
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
36 from PIL import Image, ImageOps
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 except:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 raise exceptions.MissingModule(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 "Missing module pillow, please download/install it from https://python-pillow.github.io"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 )
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 #  cf. https://stackoverflow.com/a/23575424
2521
327bbbe793ce plugin XEP-0264: handle truncated files
Goffi <goffi@goffi.org>
parents: 2513
diff changeset
43 from PIL import ImageFile
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44
2521
327bbbe793ce plugin XEP-0264: handle truncated files
Goffi <goffi@goffi.org>
parents: 2513
diff changeset
45 ImageFile.LOAD_TRUNCATED_IMAGES = True
327bbbe793ce plugin XEP-0264: handle truncated files
Goffi <goffi@goffi.org>
parents: 2513
diff changeset
46
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 from twisted.words.protocols.xmlstream import XMPPHandler
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 except ImportError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 from wokkel.subprotocols import XMPPHandler
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
53 MIME_TYPE = "image/jpeg"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
54 SAVE_FORMAT = "JPEG" # (cf. Pillow documentation)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 NS_THUMBS = "urn:xmpp:thumbs:1"
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 PLUGIN_INFO = {
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 C.PI_NAME: "XEP-0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60 C.PI_IMPORT_NAME: "XEP-0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61 C.PI_TYPE: "XEP",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 C.PI_MODES: C.PLUG_MODE_BOTH,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 C.PI_PROTOCOLS: ["XEP-0264"],
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 C.PI_DEPENDENCIES: ["XEP-0234"],
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 C.PI_MAIN: "XEP_0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 C.PI_DESCRIPTION: _("""Thumbnails handling"""),
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 }
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 class XEP_0264(object):
3329
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
72 SIZE_SMALL = (320, 320)
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
73 SIZE_MEDIUM = (640, 640)
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
74 SIZE_BIG = (1280, 1280)
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
75 SIZE_FULL_SCREEN = (2560, 2560)
3699
2dab494e56fc plugin XEP-0264: replace `SIZE_FULL_SCREEN` by `SIZE_BIG`:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 # FIXME: SIZE_FULL_SCREEN is currently discarded as the resulting files are too big
2dab494e56fc plugin XEP-0264: replace `SIZE_FULL_SCREEN` by `SIZE_BIG`:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
77 # for BoB
2dab494e56fc plugin XEP-0264: replace `SIZE_FULL_SCREEN` by `SIZE_BIG`:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
78 # TODO: use an other mechanism than BoB for bigger files
2dab494e56fc plugin XEP-0264: replace `SIZE_FULL_SCREEN` by `SIZE_BIG`:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
79 SIZES = (SIZE_SMALL, SIZE_MEDIUM, SIZE_BIG)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
82 log.info(_("Plugin XEP_0264 initialization"))
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
84 host.trigger.add("XEP-0234_buildFileElement", self._add_file_thumbnails)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
85 host.trigger.add("XEP-0234_parseFileElement", self._get_file_thumbnails)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
87 def get_handler(self, client):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return XEP_0264_handler()
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 ## triggers ##
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
92 def _add_file_thumbnails(self, client, file_elt, extra_args):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
94 thumbnails = extra_args["extra"][C.KEY_THUMBNAILS]
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 except KeyError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 for thumbnail in thumbnails:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 thumbnail_elt = file_elt.addElement((NS_THUMBS, "thumbnail"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
99 thumbnail_elt["uri"] = "cid:" + thumbnail["id"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 thumbnail_elt["media-type"] = MIME_TYPE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 width, height = thumbnail["size"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
102 thumbnail_elt["width"] = str(width)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 thumbnail_elt["height"] = str(height)
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2521
diff changeset
104 return True
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
106 def _get_file_thumbnails(self, client, file_elt, file_data):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
107 thumbnails = []
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 for thumbnail_elt in file_elt.elements(NS_THUMBS, "thumbnail"):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 uri = thumbnail_elt["uri"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 if uri.startswith("cid:"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 thumbnail = {"id": uri[4:]}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 width = thumbnail_elt.getAttribute("width")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 height = thumbnail_elt.getAttribute("height")
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if width and height:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 try:
3329
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
116 thumbnail["size"] = (int(width), int(height))
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
117 except ValueError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
118 pass
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 thumbnail["mime_type"] = thumbnail_elt["media-type"]
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
121 except KeyError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 pass
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
123 thumbnails.append(thumbnail)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
124
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if thumbnails:
3329
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
126 # we want thumbnails ordered from smallest to biggest
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
127 thumbnails.sort(key=lambda t: t.get('size', (0, 0)))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 file_data.setdefault("extra", {})[C.KEY_THUMBNAILS] = thumbnails
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2521
diff changeset
129 return True
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
130
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
131 ## thumbnails generation ##
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
132
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
133 def get_thumb_id(self, image_uid, size):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 """return an ID unique for image/size combination
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
136 @param image_uid(unicode): unique id of the image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
137 can be a hash
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
138 @param size(tuple(int)): requested size of thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
139 @return (unicode): unique id for this image/size
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
140 """
3273
4230aaeab9a7 plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
141 return hashlib.sha256(repr((image_uid, size)).encode()).hexdigest()
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
142
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
143 def _blocking_gen_thumb(
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
144 self, source_path, size=None, max_age=None, image_uid=None,
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
145 fix_orientation=True):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 """Generate a thumbnail for image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
147
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
148 This is a blocking method and must be executed in a thread
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
149 params are the same as for [generate_thumbnail]
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
150 """
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if size is None:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
152 size = self.SIZE_SMALL
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
153 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
154 img = Image.open(source_path)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
155 except IOError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
156 return Failure(exceptions.DataError("Can't open image"))
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
157
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
158 img.thumbnail(size)
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
159 if fix_orientation:
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
160 img = ImageOps.exif_transpose(img)
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
161
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
162 uid = self.get_thumb_id(image_uid or source_path, size)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
163
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
164 with self.host.common_cache.cache_data(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 PLUGIN_INFO[C.PI_IMPORT_NAME], uid, MIME_TYPE, max_age
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
166 ) as f:
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
167 img.save(f, SAVE_FORMAT)
3346
4f1fcee83d36 plugin XEP-0264: only log "fix orientation" when the option is used
Goffi <goffi@goffi.org>
parents: 3330
diff changeset
168 if fix_orientation:
4f1fcee83d36 plugin XEP-0264: only log "fix orientation" when the option is used
Goffi <goffi@goffi.org>
parents: 3330
diff changeset
169 log.debug(f"fixed orientation for {f.name}")
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
170
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
171 return img.size, uid
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
172
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
173 def generate_thumbnail(
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
174 self, source_path, size=None, max_age=None, image_uid=None, fix_orientation=True):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
175 """Generate a thumbnail of image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
176
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
177 @param source_path(unicode): absolute path to source image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
178 @param size(int, None): max size of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
179 can be one of self.SIZE_*
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
180 None to use default value (i.e. self.SIZE_SMALL)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
181 @param max_age(int, None): same as for [memory.cache.Cache.cache_data])
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param image_uid(unicode, None): unique ID to identify the image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
183 use hash whenever possible
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
184 if None, source_path will be used
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
185 @param fix_orientation(bool): if True, fix orientation using EXIF data
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
186 @return D(tuple[tuple[int,int], unicode]): tuple with:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
187 - size of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
188 - unique Id of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
190 d = threads.deferToThread(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3699
diff changeset
191 self._blocking_gen_thumb, source_path, size, max_age, image_uid=image_uid,
3330
7b47f48d31f3 plugin XEP-0264: fix orientation of thumbnails:
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
192 fix_orientation=fix_orientation
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 d.addErrback(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
195 lambda failure_: log.error("thumbnail generation error: {}".format(failure_))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 )
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
197 return d
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
198
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
199
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
200 @implementer(iwokkel.IDisco)
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
201 class XEP_0264_handler(XMPPHandler):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
202
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return [disco.DiscoFeature(NS_THUMBS)]
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
205
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
206 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return []