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.
This commit is contained in:
Benjamin Große 2023-01-31 12:04:10 +00:00 committed by Ulyssa
parent c9c547acc1
commit 8eef8787cc
No known key found for this signature in database
GPG key ID: 1B3965A3D18B9B64

View file

@ -215,17 +215,7 @@ impl ChatState {
}, },
}; };
if filename.exists() { if !filename.exists() || flags.contains(DownloadFlags::FORCE) {
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);
}
let req = MediaRequest { source, format: MediaFormat::File }; let req = MediaRequest { source, format: MediaFormat::File };
let bytes = let bytes =
@ -234,6 +224,14 @@ impl ChatState {
fs::write(filename.as_path(), bytes.as_slice())?; fs::write(filename.as_path(), bytes.as_slice())?;
msg.downloaded = true; 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) { let info = if flags.contains(DownloadFlags::OPEN) {