Skip to content

Commit

Permalink
handle base64 artUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
qxb3 committed Jan 13, 2025
1 parent fe526b6 commit 8c769ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ name = "fum"
path = "./src/main.rs"

[dependencies]
base64 = "0.22.1"
clap = { version = "4.5.23", features = ["derive"] }
crossterm = "0.28.1"
expanduser = "1.2.2"
Expand Down
25 changes: 24 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn restore() {
pub mod player {
use std::{fs, io::Cursor, str::FromStr, time::Duration};

use base64::{prelude::BASE64_STANDARD, Engine};
use image::ImageReader;
use mpris::{Metadata, PlaybackStatus, Player, PlayerFinder};
use ratatui_image::picker::Picker;
Expand Down Expand Up @@ -135,7 +136,7 @@ pub mod player {
}
}

// Handle if artUrl is on file:// scheme
// Handle file:// scheme
if art_url.starts_with("file://") {
let art_path = Url::from_str(&art_url)
.map_err(|err| format!("Failed to parse url: {art_url}: {err}"))?
Expand All @@ -157,6 +158,28 @@ pub mod player {
})
}

// Handle base64
if art_url.starts_with("data:") {
let base64_data = art_url
.split_once("base64,")
.ok_or("Invalid base64 url format")?
.1;

let bytes = BASE64_STANDARD.decode(base64_data)
.map_err(|err| format!("Failed to decode base64 data: {err}"))?;

let cover_art = ImageReader::new(Cursor::new(bytes))
.with_guessed_format()
.map_err(|_| "Unknown image file_type".to_string())?
.decode()
.map_err(|_| "Failed to decode image".to_string())?;

return Ok(CoverArt {
url: art_url.to_string(),
image: picker.new_resize_protocol(cover_art),
});
}

let client = reqwest::blocking::Client::new();
let resp = client
.get(art_url)
Expand Down

0 comments on commit 8c769ae

Please sign in to comment.