Skip to content

Commit

Permalink
expand logging
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Apr 26, 2024
1 parent cc05986 commit 3777621
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 141 deletions.
138 changes: 137 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "talecast"
version = "0.1.37"
version = "0.1.38"
edition = "2021"
description = "Simple CLI podcatcher"
license = "MIT"
Expand Down Expand Up @@ -33,5 +33,5 @@ dateparser = "0.2.1"
sanitize-filename = "0.5.0"
percent-encoding = "2.3.1"
fnv = "1.0.7"
log = "0.4"
log = { version = "0.4", features = ["kv_serde"] }
fern = "0.6"
47 changes: 38 additions & 9 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::display::DownloadBar;
use crate::utils;
use std::fs;
use std::io::{self, Read};
Expand Down Expand Up @@ -40,17 +41,36 @@ fn hashed_url(url: &str) -> String {
format!("{:x}", hash)
}

fn cached_image(url: &str) -> Option<Vec<u8>> {
fn cached_image(url: &str, ui: &DownloadBar) -> Option<Vec<u8>> {
let hash = hashed_url(url);
let path = utils::cache_dir().join(hash);
read_file_to_vec(&path).ok()
let image = read_file_to_vec(&path).ok();

if image.is_some() {
ui.log_debug("loaded cached image");
} else {
ui.log_debug("failed to load cached image");
}

image
}

async fn write_image(url: &str) -> Option<()> {
async fn write_image(url: &str, ui: &DownloadBar) -> Option<()> {
use std::io::Write;

let hashed = hashed_url(url);
let response = reqwest::get(url).await.ok()?;
let response = match reqwest::get(url).await {
Ok(res) => {
ui.log_info("connected to image url");
res
}

Err(e) => {
ui.log_error(&format!("failed to connect to image url: {:?}", e));
return None;
}
};

if response.status().is_success() {
let mime_type = response
.headers()
Expand All @@ -63,23 +83,32 @@ async fn write_image(url: &str) -> Option<()> {
let mut file = fs::File::create(&path).ok()?;
file.write_all(&data).ok()?;
MimeMap::append(url, &mime_type)?;
}
} else {
ui.log_error("response status to image url connection not successful");
};
Some(())
}

pub async fn get_image(
url: &str,
picture_type: id3::frame::PictureType,
ui: &DownloadBar,
) -> Option<id3::frame::Frame> {
let data = match cached_image(url) {
let data = match cached_image(url, ui) {
Some(data) => data,
None => {
write_image(url).await?;
cached_image(url)?
write_image(url, ui).await?;
cached_image(url, ui)?
}
};

let mime_type = MimeMap::get_mime(url)?;
let mime_type = match MimeMap::get_mime(url) {
Some(mime) => mime,
None => {
ui.log_warn(&format!("failed to load mime for: {:?}", url));
return None;
}
};

let pic = id3::frame::Picture {
data,
Expand Down
Loading

0 comments on commit 3777621

Please sign in to comment.