annotate libervia/backend/plugins/plugin_xep_0446.py @ 4284:3a550e9a2b55

black reformatting
author Goffi <goffi@goffi.org>
date Sun, 14 Jul 2024 16:47:45 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # it under the terms of the GNU Affero General Public License as published by
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # (at your option) any later version.
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # GNU Affero General Public License for more details.
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # You should have received a copy of the GNU Affero General Public License
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 from logging import exception
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Optional, Union, Tuple, Dict, Any
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pathlib import Path
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.xish import domish
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 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
25 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.tools import utils
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log = getLogger(__name__)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_NAME: "File Metadata Element",
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_IMPORT_NAME: "XEP-0446",
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_TYPE: "XEP",
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_MODES: C.PLUG_MODE_BOTH,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_PROTOCOLS: ["XEP-0446"],
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_DEPENDENCIES: ["XEP-0300"],
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_MAIN: "XEP_0446",
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_HANDLER: "no",
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_DESCRIPTION: _("""Implementation of XEP-0446 (File Metadata Element)"""),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 NS_FILE_METADATA = "urn:xmpp:file:metadata:0"
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 class XEP_0446:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, host):
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 log.info(_("XEP-0446 (File Metadata Element) plugin initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
52 host.register_namespace("file-metadata", NS_FILE_METADATA)
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._hash = host.plugins["XEP-0300"]
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def get_file_metadata_elt(
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 name: Optional[str] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 media_type: Optional[str] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 desc: Optional[str] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 size: Optional[int] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 file_hash: Optional[Tuple[str, str]] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 date: Optional[Union[float, int]] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 width: Optional[int] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 height: Optional[int] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 length: Optional[int] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 thumbnail: Optional[str] = None,
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 ) -> domish.Element:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Generate the element describing a file
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @param name: name of the file
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @param media_type: media-type
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param desc: description
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @param size: size in bytes
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @param file_hash: (algo, hash)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param date: timestamp of the last modification datetime
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @param width: image width in pixels
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @param height: image height in pixels
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @param length: video length in seconds
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @param thumbnail: URL to a thumbnail
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @return: ``<file/>`` element
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 """
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if name:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 name = Path(name).name
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 file_elt = domish.Element((NS_FILE_METADATA, "file"))
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 for name, value in (
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 ("name", name),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 ("media-type", media_type),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 ("desc", desc),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 ("size", size),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 ("width", width),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 ("height", height),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 ("length", length),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 ):
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if value is not None:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 file_elt.addElement(name, content=str(value))
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if file_hash is not None:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 hash_algo, hash_ = file_hash
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
98 file_elt.addChild(self._hash.build_hash_elt(hash_, hash_algo))
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if date is not None:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 file_elt.addElement("date", utils.xmpp_date(date))
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if thumbnail is not None:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 # TODO: implement thumbnails
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 log.warning("thumbnail is not implemented yet")
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return file_elt
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def parse_file_metadata_elt(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
107 self, file_metadata_elt: domish.Element
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 ) -> Dict[str, Any]:
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
109 """Parse <file/> element
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
111 @param file_metadata_elt: <file/> element
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 a parent element can also be used
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
113 @return: file metadata. It's a dict whose keys correspond to
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 [get_file_metadata_elt] parameters
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
115 @raise exceptions.NotFound: no <file/> element has been found
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
118 if file_metadata_elt.name != "file":
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 file_metadata_elt = next(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
121 file_metadata_elt.elements(NS_FILE_METADATA, "file")
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 )
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 except StopIteration:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 raise exceptions.NotFound
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 data: Dict[str, Any] = {}
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 for key, type_ in (
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 ("name", str),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 ("media-type", str),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 ("desc", str),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 ("size", int),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 ("date", "timestamp"),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 ("width", int),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 ("height", int),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 ("length", int),
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 ):
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 elt = next(file_metadata_elt.elements(NS_FILE_METADATA, key), None)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if elt is not None:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if type_ in (str, int):
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 content = str(elt)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if key == "name":
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 # we avoid malformed names or names containing path elements
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 content = Path(content).name
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 elif key == "media-type":
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 key = "media_type"
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 data[key] = type_(content)
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 elif type == "timestamp":
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 data[key] = utils.parse_xmpp_date(str(elt))
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 else:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 raise exceptions.InternalError
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
153 algo, hash_ = self._hash.parse_hash_elt(file_metadata_elt)
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 except exceptions.NotFound:
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 pass
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 except exceptions.DataError:
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
157 from libervia.backend.tools.xml_tools import p_fmt_elt
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
158
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
159 log.warning("invalid <hash/> element:\n{p_fmt_elt(file_metadata_elt)}")
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 else:
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3896
diff changeset
161 data["file_hash"] = (algo, hash_)
3896
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 # TODO: thumbnails
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
dbf0c7faaf49 plugin XEP-0446: File Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 return data