summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime “pep” Buquet <pep@bouah.net>2022-03-18 12:11:41 +0100
committerMaxime “pep” Buquet <pep@bouah.net>2022-03-19 10:31:34 +0100
commit51644e301bdcdc373871e3a6e282a3df5c6d61d1 (patch)
tree83691339c129f286a08a71a80391c02721b71b17
parentbc8af3cc6181248744057f1fb29295508d059d3e (diff)
downloadslixmpp-51644e301bdcdc373871e3a6e282a3df5c6d61d1.tar.gz
slixmpp-51644e301bdcdc373871e3a6e282a3df5c6d61d1.tar.bz2
slixmpp-51644e301bdcdc373871e3a6e282a3df5c6d61d1.tar.xz
slixmpp-51644e301bdcdc373871e3a6e282a3df5c6d61d1.zip
xep_0454: Add wrapper to xep_363's upload_file
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
-rw-r--r--slixmpp/plugins/xep_0454/__init__.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/slixmpp/plugins/xep_0454/__init__.py b/slixmpp/plugins/xep_0454/__init__.py
index a8af6190..2d3cb8f6 100644
--- a/slixmpp/plugins/xep_0454/__init__.py
+++ b/slixmpp/plugins/xep_0454/__init__.py
@@ -14,6 +14,7 @@ from typing import IO, Optional, Tuple
from os import urandom
from pathlib import Path
+from io import BytesIO, SEEK_END
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -32,6 +33,7 @@ class XEP_0454(BasePlugin):
name = 'xep_0454'
description = 'XEP-0454: OMEMO Media Sharing'
+ dependencies = {'xep_0363'}
@classmethod
def encrypt(cls, input_file: Optional[IO[bytes]] = None, filename: Optional[Path] = None) -> Tuple[bytes, str]:
@@ -73,4 +75,44 @@ class XEP_0454(BasePlugin):
raise InvalidURL
url = 'aesgcm://' + url.removeprefix('https://') + '#' + fragment
+ async def upload_file(
+ self,
+ filename: Path,
+ _size: Optional[int] = None,
+ _content_type: Optional[str] = None,
+ **kwargs,
+ ) -> str:
+ """
+ Wrapper to xep_0363 (HTTP Upload)'s upload_file method.
+
+ :param input_file: Binary file stream on the file.
+ :param filename: Path to the file to upload.
+
+ Same as `XEP_0454.encrypt`, one of input_file or filename must be
+ specified. If both are passed, input_file will be used and
+ filename ignored.
+
+ Other arguments passed in are passed to the actual
+ `XEP_0363.upload_file` call.
+ """
+ input_file = kwargs.get('input_file')
+ payload, fragment = self.encrypt(input_file, filename)
+
+ # Prepare kwargs for upload_file call
+ filename = urandom(12).hex() # Random filename to hide user-provided path
+ kwargs['filename'] = filename
+
+ input_enc = BytesIO(payload)
+ kwargs['input_file'] = input_enc
+
+ # Size must also be overriden if provided
+ size = input_enc.seek(0, SEEK_END)
+ input_enc.seek(0)
+ kwargs['size'] = size
+
+ kwargs['content_type'] = 'application/octet-stream'
+
+ url = await self.xmpp['xep_0363'].upload_file(**kwargs)
+ return self.format_url(url, fragment)
+
register_plugin(XEP_0454)