From 8eef8787cc2533fa1ed7f9b0e19049b0eb9cb284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gro=C3=9Fe?= Date: Tue, 31 Jan 2023 12:04:10 +0000 Subject: [PATCH] fix: attachment download flags + exists check (#34) Fix files never downloading (unless it has been downloaded in the past and using `!` force flag). The logic should be: * If file does not exist, or `!` force flag used, then download it * Else if neither `!` or `:open` flag used, then error out and then return downloaded-message or open-and-message. I.e. `:open` should still open the file if it has already been downloaded. Otherwise the only way to open it is to use `!` and re-download it. --- src/windows/room/chat.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/windows/room/chat.rs b/src/windows/room/chat.rs index b8a34c0..c1cc56d 100644 --- a/src/windows/room/chat.rs +++ b/src/windows/room/chat.rs @@ -215,17 +215,7 @@ impl ChatState { }, }; - if filename.exists() { - if !flags.contains(DownloadFlags::FORCE) { - let msg = format!( - "The file {} already exists; add ! to end of command to overwrite it.", - filename.display() - ); - let err = UIError::Failure(msg); - - return Err(err); - } - + if !filename.exists() || flags.contains(DownloadFlags::FORCE) { let req = MediaRequest { source, format: MediaFormat::File }; let bytes = @@ -234,6 +224,14 @@ impl ChatState { fs::write(filename.as_path(), bytes.as_slice())?; msg.downloaded = true; + } else if !flags.contains(DownloadFlags::OPEN) { + let msg = format!( + "The file {} already exists; add ! to end of command to overwrite it.", + filename.display() + ); + let err = UIError::Failure(msg); + + return Err(err); } let info = if flags.contains(DownloadFlags::OPEN) {