Handle OSError happening in send_file()

This commit is contained in:
miruka 2019-12-05 09:51:31 -04:00
parent b3f93b969c
commit 3aff20006c
3 changed files with 25 additions and 10 deletions

View File

@ -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,
},
}

View File

@ -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

View File

@ -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 {}