From 9f3bb1aa4dabdd5fa3ee8ad7fe3759e2b5db8aef Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 12 Nov 2019 09:48:11 -0400 Subject: [PATCH] set_avatar_from_file: raise if file isn't an image --- TODO.md | 5 +---- src/python/errors.py | 7 +++++++ src/python/matrix_client.py | 10 +++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index c814dd06..5111b510 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ - Media - Verify things work with chat.privacytools.io (subdomain weirdness) - Confirmation box after picking file to upload - - Handle upload errors: non existent path, path is a dir, file too big, etc + - Handle upload/set avatar errors: bad path, is a dir, file too big, etc - Show real progression for mxc thumbnail loadings, uploads and downloads - Show reason under broken thumbnail icons @@ -52,11 +52,9 @@ - Don't store states in delegates - [hr not working](https://bugreports.qt.io/browse/QTBUG-74342) - Terrible performance using `QT_QPA_PLATFORM=wayland-egl`, must use `xcb` - - Verify big avatars aren't downloaded uselessly - Quote links color in room subtitles (e.g. "> http://foo.orgA)" ) - UI - - Show error if uploading avatar fails or file is corrupted - Way to open context menus without a right mouse button - `smartVerticalFlick()` gradual acceleration @@ -166,7 +164,6 @@ the message with `decrypt_event()`" - poljar - [Soft logouts](https://github.com/poljar/matrix-nio/commit/aba10) - - `translated` arg for avatar upload and login errors - Check if username exists on login screen - `pyotherside.atexit()` - Logout previous session if adding an account that's already connected diff --git a/src/python/errors.py b/src/python/errors.py index 5821726d..df6dccff 100644 --- a/src/python/errors.py +++ b/src/python/errors.py @@ -62,6 +62,13 @@ class InvalidUserInContext(Exception): class UneededThumbnail(Exception): pass + @dataclass class UnthumbnailableError(Exception): exception: Optional[Exception] = None + + +@dataclass +class BadMimeType(Exception): + wanted: str = field() + got: str = field() diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index 4908b777..7ee48936 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -25,8 +25,8 @@ import nio from . import __about__, utils from .errors import ( - InvalidUserInContext, MatrixError, UneededThumbnail, UnthumbnailableError, - UserNotFound, + BadMimeType, InvalidUserInContext, MatrixError, UneededThumbnail, + UnthumbnailableError, UserNotFound, ) from .html_filter import HTML_FILTER from .models.items import ( @@ -617,7 +617,11 @@ class MatrixClient(nio.AsyncClient): async def set_avatar_from_file(self, path: Union[Path, str]) -> None: - # TODO: check if mime is image + mime = utils.guess_mime(path) + + if mime.split("/")[0] != "image": + raise BadMimeType(wanted="image/*", got=mime) + await self.set_avatar((await self.upload_file(path))[0])