diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index 8613cacf..186901e0 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -225,9 +225,14 @@ class MatrixClient(nio.AsyncClient): from .media_cache import Media, Thumbnail path = Path(path) - size = path.resolve().stat().st_size encrypt = room_id in self.encrypted_rooms + try: + size = path.resolve().stat().st_size + except (PermissionError, FileNotFoundError): + # This error will be caught again by the try block later below + size = 0 + task = asyncio.Task.current_task() upload_item = Upload(item_uuid, task, path, total_size=size) self.models[Upload, room_id][upload_item.uuid] = upload_item @@ -236,7 +241,7 @@ class MatrixClient(nio.AsyncClient): url, mime, crypt_dict = await self.upload( path, filename=path.name, encrypt=encrypt, ) - except MatrixError as err: + except (MatrixError, OSError) as err: upload_item.status = UploadStatus.Error upload_item.error = type(err) upload_item.error_args = err.args @@ -257,7 +262,7 @@ class MatrixClient(nio.AsyncClient): "body": path.name, "info": { "mimetype": mime, - "size": size, + "size": upload_item.total_size, }, } diff --git a/src/python/models/items.py b/src/python/models/items.py index 23f6bdb5..ee59f0a1 100644 --- a/src/python/models/items.py +++ b/src/python/models/items.py @@ -126,11 +126,6 @@ class Upload(ModelItem): start_date: datetime = field(init=False, default_factory=datetime.now) - def __post_init__(self) -> None: - if not self.total_size: - self.total_size = self.filepath.resolve().stat().st_size - - def __lt__(self, other: "Upload") -> bool: # Sort from newest upload to oldest. return self.start_date > other.start_date diff --git a/src/qml/Chat/UploadsBar.qml b/src/qml/Chat/UploadsBar.qml index d70ba493..b269b3a3 100644 --- a/src/qml/Chat/UploadsBar.qml +++ b/src/qml/Chat/UploadsBar.qml @@ -60,7 +60,7 @@ Rectangle { } Layout.preferredWidth: theme.baseElementsHeight - Layout.preferredHeight: Layout.preferredWidth + Layout.fillHeight: true } HLabel { @@ -93,8 +93,20 @@ Rectangle { qsTr("Too large for this server: %1") .arg(fileName) : + model.error === "IsADirectoryError" ? + qsTr("Can't upload folders: %1") + .arg(filePath) : + + model.error === "FileNotFoundError" ? + qsTr("Non-existant file: %1") + .arg(filePath) : + + model.error === "PermissionError" ? + qsTr("No permission to read this file: %1") + .arg(filePath) : + qsTr("Unknown error for %1: %2 - %3") - .arg(fileName) + .arg(filePath) .arg(model.error) .arg(model.error_args) ) : @@ -113,6 +125,9 @@ Rectangle { readonly property string fileName: model.filepath.split("/").slice(-1)[0] + + readonly property string filePath: + model.filepath.replace(/^file:\/\//, "") } HSpacer {}