Skip to content

Commit

Permalink
Cover filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed Oct 14, 2024
1 parent 20c7117 commit 61a1ce8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
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.

8 changes: 8 additions & 0 deletions client/src/components/AutotaggerAdvanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@
class='input'
></q-input>

<!-- Cover filename -->
<q-input
v-model='$1t.config.value.coverFilename'
filled
label='Cover filename template'
class='input monospace'
></q-input>


<div class='row justify-center q-mt-sm' v-if='$1t.config.value.matchDuration'>
<q-slider
Expand Down
1 change: 1 addition & 0 deletions client/src/scripts/autotagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AutotaggerConfig {
fetchAllResults: boolean = false;
albumTagging: boolean = false;
albumTaggingRatio: number = 0.5;
coverFilename: string | undefined = undefined;

spotify?: SpotifyConfig;

Expand Down
1 change: 1 addition & 0 deletions crates/onetagger-autotag/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ onetagger-tag = { path = "../onetagger-tag" }
onetagger-tagger = { path = "../onetagger-tagger" }
onetagger-player = { path = "../onetagger-player" }
onetagger-shared = { path = "../onetagger-shared" }
onetagger-renamer = { path = "../onetagger-renamer" }
onetagger-platforms = { path = "../onetagger-platforms" }
48 changes: 38 additions & 10 deletions crates/onetagger-autotag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::collections::HashMap;
use anyhow::Error;
use onetagger_renamer::{Renamer, RenamerConfig, TemplateParser};
use rand::seq::SliceRandom;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -275,6 +276,7 @@ impl TrackImpl for Track {
}

// Album art
let mut cover_data = None;
if (config.overwrite_tag(SupportedTag::AlbumArt) || tag.get_art().is_empty()) && self.art.is_some() && config.tag_enabled(SupportedTag::AlbumArt) {
info!("Downloading art: {:?}", self.art);
match self.download_art(self.art.as_ref().unwrap()) {
Expand All @@ -289,15 +291,7 @@ impl TrackImpl for Track {
}

tag.set_art(CoverType::CoverFront, "image/jpeg", Some("Cover"), data.clone());
// Save to file
if config.album_art_file {
let path = path.as_ref().parent().unwrap().join("cover.jpg");
if !path.exists() {
if let Ok(mut file) = File::create(path) {
file.write_all(&data).ok();
}
}
}
cover_data = Some(data);
},
None => warn!("Invalid album art!")
}
Expand Down Expand Up @@ -327,7 +321,24 @@ impl TrackImpl for Track {
}

// Save
tag.save_file(path.as_ref())?;
tag.save_file(&path.as_ref())?;

// Cover file
if let Some(cover_data) = cover_data {
match AudioFileInfo::load_file(&path, None, None) {
Ok(info) => {
let cover_path = get_cover_path(&info, path.as_ref().parent().unwrap(), config);
match std::fs::write(&cover_path, cover_data) {
Ok(_) => debug!("Cover written to: {}", cover_path.display()),
Err(e) => error!("Failed to write cover file: {e}"),
}
},
Err(e) => {
error!("Failed generating cover path: {e}");
}
}
}

Ok(())
}

Expand Down Expand Up @@ -379,6 +390,23 @@ impl TrackImpl for Track {

}

/// Get path to cover file
fn get_cover_path(info: &AudioFileInfo, folder: impl AsRef<Path>, config: &TaggerConfig) -> PathBuf {
let mut path = folder.as_ref().join("cover.jpg");

if let Some(template) = config.cover_filename.as_ref() {
if !template.trim().is_empty() {
// Generate new filename
let renamer_config = RenamerConfig::default_with_paths(&folder, template);
let mut renamer = Renamer::new(TemplateParser::parse(template));
let new_path = renamer.generate_name(folder.as_ref(), info, &renamer_config);
path = new_path.with_extension("jpg");
}
}

path
}

pub trait AudioFileInfoImpl {
/// Load audio file info from path
fn load_file(path: impl AsRef<Path>, filename_template: Option<Regex>, title_regex: Option<Regex>) -> Result<AudioFileInfo, Error>;
Expand Down
16 changes: 16 additions & 0 deletions crates/onetagger-renamer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ pub struct RenamerConfig {
pub keep_subfolders: bool,
}

impl RenamerConfig {
/// Create new instance with default properties and paths
pub fn default_with_paths(path: impl AsRef<Path>, template: &str) -> RenamerConfig {
RenamerConfig {
path: path.as_ref().to_path_buf(),
out_dir: None,
template: template.to_string(),
copy: false,
overwrite: false,
keep_subfolders: false,
separator: ", ".to_owned(),
subfolders: true,
}
}
}

/// HTML generation test
#[test]
fn generate_html() {
Expand Down
2 changes: 1 addition & 1 deletion crates/onetagger-tagger/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::{Record, Level, RecordBuilder};
use crate::TrackMatch;

/// Version of supported custom platform
pub const CUSTOM_PLATFORM_COMPATIBILITY: i32 = 44;
pub const CUSTOM_PLATFORM_COMPATIBILITY: i32 = 45;

/// Logging from plugins
#[no_mangle]
Expand Down
5 changes: 4 additions & 1 deletion crates/onetagger-tagger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct TaggerConfig {
pub album_tagging: bool,
/// % of tracks that have to be from one album to be considered as the correct
pub album_tagging_ratio: f32,
/// Renamer template
pub cover_filename: Option<String>,

/// Platform specific. Format: `{ platform: { custom_option: value }}`
pub custom: PlatformTaggerConfig,
Expand Down Expand Up @@ -166,7 +168,8 @@ impl Default for TaggerConfig {
id3_comm_lang: None,
fetch_all_results: false,
album_tagging: false,
album_tagging_ratio: 0.5
album_tagging_ratio: 0.5,
cover_filename: None
}
}
}
Expand Down

0 comments on commit 61a1ce8

Please sign in to comment.