use cached pdf if MWRA returns 404 or something

This commit is contained in:
xenofem 2022-12-09 17:43:49 -05:00
parent d7dfc28587
commit 4eb510039c

View file

@ -1,8 +1,11 @@
use std::{time::{Duration, Instant, SystemTime}, path::PathBuf};
use std::{
path::PathBuf,
time::{Duration, Instant, SystemTime},
};
use futures::{sink::SinkExt, TryStreamExt};
use lazy_static::lazy_static;
use log::info;
use log::{info, warn};
use reqwest::Url;
use scraper::Selector;
use time::PrimitiveDateTime;
@ -94,11 +97,9 @@ impl PdfFetcher {
.ok_or(Error::NotFound)?;
let pdf_url = CHARTS_URL.join(pdf_href)?;
let origin_modtime = self
.client
.head(pdf_url.clone())
.send()
.await?
let head_resp = self.client.head(pdf_url.clone()).send().await?;
let origin_modtime = head_resp
.headers()
.get(reqwest::header::LAST_MODIFIED)
.and_then(|val| {
@ -115,7 +116,13 @@ impl PdfFetcher {
.zip(origin_modtime)
.map_or(true, |(cache, origin)| origin > cache);
if outdated {
if !head_resp.status().is_success() {
warn!(
"MWRA server returned unexpected response, not fetching updated PDF: {:?}",
head_resp
);
// Just use the cached PDF, assuming we have it
} else if outdated {
info!("Cached PDF is outdated, downloading latest version");
let mut pdf_stream = self