diff --git a/Cargo.lock b/Cargo.lock index b645694..5f417c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -844,7 +844,6 @@ dependencies = [ "reqwest", "serde", "serde_json", - "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 030413f..36a80da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] maud = "*" knuffel = "3" -url = "2.3" miette = { version = "5.7", features = ["fancy"] } reqwest = { version = "0.11", features = ["blocking"] } serde = { version = "1.0", features = ["derive"] } diff --git a/src/cache.rs b/src/cache.rs index 5afa23d..8669aa3 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -2,12 +2,15 @@ // we download badges to publish them alongside the website for // cache / archive / cross-site perfs reasons -use std::{error::Error, fs::File}; +use std::{ + error::Error, + fs::File, + hash::{DefaultHasher, Hasher}, +}; use reqwest::header::CONTENT_TYPE; -use url::Url; -pub fn download(url: Url, output_path: &str) -> Result> { +pub fn download(url: &str, label: &str) -> Result> { println!(":: grabbing the badge at {}", url); let mut res = reqwest::blocking::get(url)?; @@ -35,9 +38,13 @@ pub fn download(url: Url, output_path: &str) -> Result> { } }; - let mut file = File::create(format!("{}.{}", output_path, extension))?; + // Generating a proper filename + let mut hasher = DefaultHasher::new(); + hasher.write(label.as_bytes()); + let output_file = format!("badge.{}.{}", hasher.finish().to_string(), extension); + let mut file = File::create(format!("public/{}", output_file))?; res.copy_to(&mut file)?; - Ok(String::from(extension)) + Ok(output_file) } diff --git a/src/config.rs b/src/config.rs index a33f256..1ab8785 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,4 @@ -use std::{collections::hash_map::DefaultHasher, hash::Hasher}; - use serde::Serialize; -use url::Url; // Neighbor is both webring neighbor and peer (individual entity) neighbor #[derive(knuffel::Decode, Serialize, Debug, Clone)] @@ -22,7 +19,6 @@ pub struct Bio { #[derive(knuffel::Decode, Serialize, Debug, Clone)] pub struct Node { - pub extension: String, #[knuffel(argument)] pub url: String, #[knuffel(property)] @@ -34,8 +30,11 @@ pub struct Node { pub social: Vec, #[knuffel(children(name = "bio"))] pub bio: Vec, + + pub cached_badge_url: Option, } +// TODO: want to rework how the nodes we ref are made cuz its kinda messy #[derive(knuffel::Decode, Serialize, Debug, Clone)] pub struct Social { #[knuffel(argument)] @@ -45,44 +44,12 @@ pub struct Social { } impl<'a> Node { - // This generates a hash based on the label. - // it's notably used for associating the in-cache badge image to the node - pub fn get_id(&'a self) -> String { - let mut hasher = DefaultHasher::new(); - hasher.write(self.get_label().as_bytes()); - hasher.finish().to_string() - } - - pub fn get_url(&'a self) -> Url { - Url::parse(&self.url).expect(format!("invalid url: {}", &self.url).as_str()) - } - - pub fn get_badge(&'a self) -> Option { - if let Some(badge) = &self.badge { - Some(Url::parse(badge).expect(format!("invalid url: {}", badge).as_str())) - } else { - None - } - } - - pub fn get_cached_badge(&'a self) -> Option { - if let Some(_) = &self.get_badge() { - Some(format!("{}.badge.{}", self.get_id(), self.extension)) - } else { - None - } + pub fn get_badge(&'a self) -> Option<&'a String> { + self.cached_badge_url.as_ref().or(self.badge.as_ref()) } - pub fn get_label(&'a self) -> String { - if let Some(label) = &self.label { - String::from(label) - } else { - String::from( - self.get_url() - .host_str() - .expect("node url should have a domain fragment"), - ) - } + pub fn get_label(&'a self) -> &'a str { + self.label.as_ref().unwrap_or(&self.url) } } diff --git a/src/html/page.rs b/src/html/page.rs index 2c57bd2..b67e5b0 100644 --- a/src/html/page.rs +++ b/src/html/page.rs @@ -22,13 +22,13 @@ pub fn make_page(data: &Ring) -> Markup { @for node in &data.nodes { article.badged[node.badge.is_some()].node { - @if let Some(badge) = &node.get_cached_badge() { + @if let Some(badge) = &node.get_badge() { header.badge { img src=(badge) alt=(node.get_label()); } } main { - h3.label { a href=(node.get_url().as_str()) { (node.get_label()) } } + h3.label { a href=(node.url) { (node.get_label()) } } @if node.bio.len() != 0 { section.bio { diff --git a/src/main.rs b/src/main.rs index 2edbe80..14540bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,14 @@ fn main() -> miette::Result<()> { println!(":: grabbing the lil badge thingies"); for node in &mut ring.nodes { if let Some(badge_url) = node.get_badge() { - let proper_extension = download(badge_url, &format!("public/{}.badge", node.get_id())) - .expect("somehow failed to grab the lil badge thingies"); - node.extension = proper_extension; + match download(badge_url, &node.get_label()) { + Ok(cached_name) => node.cached_badge_url = Some(cached_name), + Err(e) => println!( + "failed to grab the lil badge thingy for {}: {:?}", + node.get_label(), + e + ), + }; } }