Use the PDF's existence to avoid sending duplicate emails

If the PDF is already on disk, it's a good sign that we've already sent
the email, so skip in that case.

To make the signal a bit more reliable, make writing the PDF to disk
the last action taken, in case sending the email fails.
This commit is contained in:
2023-04-03 12:40:08 +01:00
parent 41d2923fa9
commit 3d303aebf6
2 changed files with 10 additions and 4 deletions

View File

@@ -16,6 +16,5 @@ ureq = { version = "*", features = ["cookies"] }
codegen-units = 1
lto = true
opt-level = "z"
panic = "abort"
strip = true

View File

@@ -30,7 +30,9 @@ fn get_link(agent: ureq::Agent) -> Option<String> {
fn main() {
let mut args = std::env::args().skip(1);
let to: String = args.next().expect("Provide To: on the command line");
let to: String = args
.next()
.expect("Usage: MS_USER=... MS_PASS=... msme <to-address> [bcc-address]...");
let bcc: Vec<String> = args.collect();
let username = std::env::var("MS_USER").expect("Please set MS_USER envvar");
@@ -55,6 +57,11 @@ fn main() {
.rsplit_once('/')
.expect("Couldn't parse filename from link");
if std::path::Path::new(filename).exists() {
println!("We already have {}, skipping", filename);
return;
}
let mut buf: Vec<u8> = vec![];
{
println!("Downloading PDF...");
@@ -65,8 +72,6 @@ fn main() {
.into_reader()
.read_to_end(&mut buf) // TODO: would be better to stream the bytes
.expect("Couldn't read bytes from server");
std::fs::write(filename, &buf).expect("Couldn't create file");
}
println!("Sending email...");
@@ -96,5 +101,7 @@ fn main() {
sendmail.wait().expect("Sendmail execution failed");
println!("Saving file...");
std::fs::write(filename, &buf).expect("Couldn't create file");
println!("OK!");
}