Skip to content

Commit 2d76e2b

Browse files
model/views: Handle Invalid File Location and other possible errors.
If a passed file location is invalid then the get_file_upload_uri returns None and using that the error and reported.
1 parent 9165970 commit 2d76e2b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

zulipterminal/model.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import itertools
66
import json
7+
import os
78
import time
89
from collections import defaultdict
910
from concurrent.futures import Future, ThreadPoolExecutor, wait
@@ -557,10 +558,16 @@ def send_stream_message(self, stream: str, topic: str, content: str) -> bool:
557558
notify_if_message_sent_outside_narrow(composition, self.controller)
558559
return message_was_sent
559560

560-
def get_file_upload_uri(self, file_location: str) -> str:
561-
with open(file_location, "rb") as fp:
562-
result = self.client.upload_file(fp)
563-
return result["uri"]
561+
def get_file_upload_uri(self, file_location: str) -> Optional[str]:
562+
if os.path.exists(file_location):
563+
with open(file_location, "rb") as fp:
564+
result = self.client.upload_file(fp)
565+
if result["result"] == "success":
566+
return result["uri"]
567+
else:
568+
return None
569+
else:
570+
return None
564571

565572
def update_private_message(self, msg_id: int, content: str) -> bool:
566573
request: PrivateMessageUpdateRequest = {

zulipterminal/ui_tools/views.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,13 @@ def __init__(
11941194

11951195
def _handle_file_upload(self, file_location: str) -> None:
11961196
self.uri = self.model.get_file_upload_uri(file_location)
1197-
file_path = Path(file_location)
1198-
file_name = file_path.name
1199-
self.write_box.append_uri_and_filename(file_name, self.uri)
1197+
if self.uri is not None:
1198+
file_path = Path(file_location)
1199+
file_name = file_path.name
1200+
self.write_box.append_uri_and_filename(file_name, self.uri)
1201+
else:
1202+
self.controller.report_error(["ERROR: Unable to get the URI"])
1203+
self.controller.exit_popup()
12001204

12011205
def keypress(self, size: urwid_Size, key: str) -> str:
12021206
if is_command_key("ENTER", key):

0 commit comments

Comments
 (0)