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:
@@ -16,6 +16,5 @@ ureq = { version = "*", features = ["cookies"] }
|
|||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
lto = true
|
lto = true
|
||||||
opt-level = "z"
|
opt-level = "z"
|
||||||
panic = "abort"
|
|
||||||
strip = true
|
strip = true
|
||||||
|
|
||||||
|
13
src/main.rs
13
src/main.rs
@@ -30,7 +30,9 @@ fn get_link(agent: ureq::Agent) -> Option<String> {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut args = std::env::args().skip(1);
|
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 bcc: Vec<String> = args.collect();
|
||||||
|
|
||||||
let username = std::env::var("MS_USER").expect("Please set MS_USER envvar");
|
let username = std::env::var("MS_USER").expect("Please set MS_USER envvar");
|
||||||
@@ -55,6 +57,11 @@ fn main() {
|
|||||||
.rsplit_once('/')
|
.rsplit_once('/')
|
||||||
.expect("Couldn't parse filename from link");
|
.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![];
|
let mut buf: Vec<u8> = vec![];
|
||||||
{
|
{
|
||||||
println!("Downloading PDF...");
|
println!("Downloading PDF...");
|
||||||
@@ -65,8 +72,6 @@ fn main() {
|
|||||||
.into_reader()
|
.into_reader()
|
||||||
.read_to_end(&mut buf) // TODO: would be better to stream the bytes
|
.read_to_end(&mut buf) // TODO: would be better to stream the bytes
|
||||||
.expect("Couldn't read bytes from server");
|
.expect("Couldn't read bytes from server");
|
||||||
|
|
||||||
std::fs::write(filename, &buf).expect("Couldn't create file");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Sending email...");
|
println!("Sending email...");
|
||||||
@@ -96,5 +101,7 @@ fn main() {
|
|||||||
|
|
||||||
sendmail.wait().expect("Sendmail execution failed");
|
sendmail.wait().expect("Sendmail execution failed");
|
||||||
|
|
||||||
|
println!("Saving file...");
|
||||||
|
std::fs::write(filename, &buf).expect("Couldn't create file");
|
||||||
println!("OK!");
|
println!("OK!");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user