use {
std::{
collections::HashMap,
error::Error,
fs
},
bytes::Bytes,
log::debug,
reqwest::Client,
serde::Deserialize,
crate::RqResult,
};
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub enum Extension {
#[serde(rename = "jpg")]
Jpeg,
#[serde(rename = "png")]
Png,
#[serde(rename = "gif")]
Gif,
#[serde(rename = "swf")]
Swf,
#[serde(rename = "webm")]
WebM
}
#[derive(Debug, Deserialize)]
pub struct Alternates {
pub r#type: String,
pub width: u32,
pub height: u32,
pub urls: Vec<String>
}
#[derive(Debug, Deserialize)]
pub struct PostFile {
pub width: u16,
pub height: u16,
pub ext: Extension,
pub size: u64, pub md5: String,
pub url: String,
}
#[derive(Debug, Deserialize)]
pub struct PostPreview {
pub width: u16,
pub height: u16,
pub url: String,
}
#[derive(Debug, Deserialize)]
pub struct PostSample {
pub has: bool,
pub height: u16,
pub width: u16,
pub url: String,
pub alternates: HashMap<String, Alternates>
}
impl PostFile {
pub async fn get_image_data(&self, client: Client)
-> RqResult<Bytes> {
let res = client.get(&self.url).send().await?.bytes().await?;
debug!("got the bytes");
Ok(res)
}
pub async fn write(&self, client: Client, filepath: &str) -> Result<(), Box<dyn Error>> {
debug!("writing {filepath} using {:#?}", client);
fs::write(filepath, self.get_image_data(client).await?.as_ref())?;
Ok(())
}
}