Compare commits

...

3 Commits

Author SHA1 Message Date
f534687292 refactor (#24)
All checks were successful
Rust Build / Check (push) Successful in 37s
Rust Build / Check (pull_request) Successful in 31s
Release Tagging / release (push) Successful in 41s
Rust Build / Test Suite (push) Successful in 41s
Rust Build / Rustfmt (push) Successful in 31s
Rust Build / Clippy (push) Successful in 38s
Rust Build / build (push) Successful in 42s
Rust Build / Test Suite (pull_request) Successful in 37s
Rust Build / Rustfmt (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Successful in 33s
Rust Build / build (pull_request) Successful in 43s
Reviewed-on: #24
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-04-19 01:31:57 +00:00
c80bc7d7e1 remove_picture (#23)
All checks were successful
Release Tagging / release (push) Successful in 36s
Rust Build / Check (push) Successful in 42s
Rust Build / Check (pull_request) Successful in 39s
Rust Build / Test Suite (push) Successful in 47s
Rust Build / Rustfmt (push) Successful in 31s
Rust Build / Clippy (push) Successful in 40s
Rust Build / build (push) Successful in 46s
Rust Build / Test Suite (pull_request) Successful in 40s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 34s
Rust Build / build (pull_request) Successful in 46s
Reviewed-on: #23
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-04-19 01:07:49 +00:00
805dfb269b Contains pictures (#22)
All checks were successful
Release Tagging / release (push) Successful in 38s
Rust Build / Check (push) Successful in 33s
Rust Build / Check (pull_request) Successful in 35s
Rust Build / Test Suite (push) Successful in 43s
Rust Build / Rustfmt (push) Successful in 35s
Rust Build / Clippy (push) Successful in 38s
Rust Build / build (push) Successful in 43s
Rust Build / Test Suite (pull_request) Successful in 33s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Successful in 34s
Rust Build / build (pull_request) Successful in 40s
Reviewed-on: #22
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-04-19 00:52:14 +00:00
3 changed files with 221 additions and 89 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "icarus_meta"
version = "0.1.0"
version = "0.1.20"
edition = "2024"
rust-version = "1.86"

View File

@@ -1,20 +1,3 @@
use lofty::{file::AudioFile, tag::Accessor};
use crate::types;
fn get_type(t: types::Type) -> Result<String, std::io::Error> {
match t {
types::Type::Title => Ok("TITLE".to_owned()),
types::Type::Artist => Ok("ARTIST".to_owned()),
types::Type::Album => Ok("ALBUM".to_owned()),
types::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
types::Type::Genre => Ok("GENRE".to_owned()),
types::Type::Date => Ok("DATE".to_owned()),
types::Type::Track => Ok("TRACKNUMBER".to_owned()),
types::Type::Disc => Ok("DISCNUMBER".to_owned()),
}
}
pub mod coverart {
use lofty::{file::AudioFile, ogg::OggPictureStorage};
@@ -88,92 +71,163 @@ pub mod coverart {
Err(err) => Err(err),
}
}
}
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
match std::fs::File::open(filepath) {
Ok(mut content) => {
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
{
Ok(flac_file) => match flac_file.vorbis_comments() {
Some(vb) => {
let type_str: String = get_type(t).unwrap();
match vb.get(&type_str) {
Some(val) => Ok(val.to_owned()),
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Could not get tag data",
)),
pub fn contains_coverart(song_filepath: &String) -> Result<(bool, usize), std::io::Error> {
match std::fs::File::open(song_filepath) {
Ok(mut file) => {
match lofty::flac::FlacFile::read_from(
&mut file,
lofty::config::ParseOptions::new(),
) {
Ok(flac_file) => {
let pictures = flac_file.pictures();
if pictures.is_empty() {
Ok((false, 0))
} else {
let res = pictures.to_vec();
if res.is_empty() {
Ok((false, 0))
} else {
Ok((true, res.len()))
}
}
}
None => Err(std::io::Error::new(
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No tags found",
err.to_string(),
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
}
}
Err(err) => Err(err),
}
}
pub fn remove_coverart(song_filepath: &String) -> Result<Vec<u8>, std::io::Error> {
match std::fs::File::open(song_filepath) {
Ok(mut file) => {
match lofty::flac::FlacFile::read_from(
&mut file,
lofty::config::ParseOptions::new(),
) {
Ok(mut flac_file) => {
let pictures = flac_file.pictures();
let res = pictures.to_vec();
if !res.is_empty() {
let picture = &res[0];
flac_file.remove_picture(0);
Ok(picture.clone().0.into_data())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No pictures found",
))
}
}
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
}
}
Err(err) => Err(err),
}
Err(err) => Err(err),
}
}
pub fn set_meta(
t: types::Type,
filepath: &String,
value: &String,
) -> Result<String, std::io::Error> {
match std::fs::File::open(filepath) {
Ok(mut content) => {
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
{
Ok(mut flac_file) => match flac_file.vorbis_comments_mut() {
Some(vb) => {
let pre_value = value.clone();
match t {
types::Type::Album => {
vb.set_album(pre_value);
}
types::Type::AlbumArtist => {
vb.insert(get_type(t).unwrap(), pre_value);
}
types::Type::Artist => {
vb.set_artist(pre_value);
}
types::Type::Date => {
vb.insert(get_type(t).unwrap(), pre_value);
}
types::Type::Disc => {
vb.set_disk(pre_value.parse().unwrap());
}
types::Type::Genre => {
vb.set_genre(pre_value);
}
types::Type::Title => {
vb.set_title(pre_value);
}
types::Type::Track => {
vb.set_track(pre_value.parse().unwrap());
}
};
pub mod metadata {
use crate::types;
use lofty::file::AudioFile;
use lofty::tag::Accessor;
Ok(value.to_owned())
}
None => Err(std::io::Error::new(
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
match std::fs::File::open(filepath) {
Ok(mut content) => {
match lofty::flac::FlacFile::read_from(
&mut content,
lofty::config::ParseOptions::new(),
) {
Ok(flac_file) => match flac_file.vorbis_comments() {
Some(vb) => {
let type_str: String = types::access::get_type(t).unwrap();
match vb.get(&type_str) {
Some(val) => Ok(val.to_owned()),
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Could not get tag data",
)),
}
}
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No tags found",
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No tags found",
err.to_string(),
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
}
}
Err(err) => Err(err),
}
}
pub fn set_meta(
t: types::Type,
filepath: &String,
value: &String,
) -> Result<String, std::io::Error> {
match std::fs::File::open(filepath) {
Ok(mut content) => {
match lofty::flac::FlacFile::read_from(
&mut content,
lofty::config::ParseOptions::new(),
) {
Ok(mut flac_file) => match flac_file.vorbis_comments_mut() {
Some(vb) => {
let pre_value = value.clone();
match t {
types::Type::Album => {
vb.set_album(pre_value);
}
types::Type::AlbumArtist => {
vb.insert(types::access::get_type(t).unwrap(), pre_value);
}
types::Type::Artist => {
vb.set_artist(pre_value);
}
types::Type::Date => {
vb.insert(types::access::get_type(t).unwrap(), pre_value);
}
types::Type::Disc => {
vb.set_disk(pre_value.parse().unwrap());
}
types::Type::Genre => {
vb.set_genre(pre_value);
}
types::Type::Title => {
vb.set_title(pre_value);
}
types::Type::Track => {
vb.set_track(pre_value.parse().unwrap());
}
};
Ok(value.to_owned())
}
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No tags found",
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
}
}
Err(err) => Err(err),
}
Err(err) => Err(err),
}
}
@@ -254,6 +308,7 @@ mod tests {
}
mod get {
use super::metadata::get_meta;
use super::*;
use crate::types;
@@ -458,7 +513,9 @@ mod tests {
}
mod set {
use super::metadata::{get_meta, set_meta};
use super::*;
use crate::types;
#[test]
fn test_set_title() {
@@ -944,5 +1001,64 @@ mod tests {
}
};
}
#[test]
fn test_picture_exists() {
let filename = util::get_filename(1);
let dir = String::from(util::TESTFILEDIRECTORY);
match file_exists(&dir, &filename) {
Ok(_) => {
let filepath = get_full_path(&dir, &filename).unwrap();
match coverart::contains_coverart(&filepath) {
Ok((exists, pictures)) => {
assert!(exists, "File should have a cover art");
assert!((pictures > 0), "No cover art was found in the file");
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
}
Err(err) => {
assert!(false, "Error: File does not exist {:?}", err.to_string());
}
};
}
#[test]
fn test_remove_picture() {
let filename = util::get_filename(1);
let dir = String::from(util::TESTFILEDIRECTORY);
let temp_file = tempfile::tempdir().expect("Could not create test directory");
let test_dir = String::from(temp_file.path().to_str().unwrap());
let test_filename = String::from("track09.flac");
let new_filepath = get_full_path(&test_dir, &test_filename).unwrap();
match file_exists(&dir, &filename) {
Ok(_) => {
let filepath = get_full_path(&dir, &filename).unwrap();
match util::copy_file(&filepath, &new_filepath) {
Ok(_o) => match coverart::remove_coverart(&new_filepath) {
Ok(bytes) => {
assert_eq!(false, bytes.is_empty(), "This should not be empty");
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
},
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
}
Err(err) => {
assert!(false, "Error: File does not exist {:?}", err.to_string());
}
};
}
}
}

View File

@@ -8,3 +8,19 @@ pub enum Type {
Track,
Disc,
}
pub mod access {
pub fn get_type(t: super::Type) -> Result<String, std::io::Error> {
match t {
super::Type::Title => Ok("TITLE".to_owned()),
super::Type::Artist => Ok("ARTIST".to_owned()),
super::Type::Album => Ok("ALBUM".to_owned()),
super::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
super::Type::Genre => Ok("GENRE".to_owned()),
super::Type::Date => Ok("DATE".to_owned()),
super::Type::Track => Ok("TRACKNUMBER".to_owned()),
super::Type::Disc => Ok("DISCNUMBER".to_owned()),
}
}
}