Skip to content

Commit 61a1ce8

Browse files
committed
Cover filename
1 parent 20c7117 commit 61a1ce8

File tree

8 files changed

+70
-12
lines changed

8 files changed

+70
-12
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/src/components/AutotaggerAdvanced.vue

+8
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@
295295
class='input'
296296
></q-input>
297297

298+
<!-- Cover filename -->
299+
<q-input
300+
v-model='$1t.config.value.coverFilename'
301+
filled
302+
label='Cover filename template'
303+
class='input monospace'
304+
></q-input>
305+
298306

299307
<div class='row justify-center q-mt-sm' v-if='$1t.config.value.matchDuration'>
300308
<q-slider

client/src/scripts/autotagger.ts

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class AutotaggerConfig {
8282
fetchAllResults: boolean = false;
8383
albumTagging: boolean = false;
8484
albumTaggingRatio: number = 0.5;
85+
coverFilename: string | undefined = undefined;
8586

8687
spotify?: SpotifyConfig;
8788

crates/onetagger-autotag/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ onetagger-tag = { path = "../onetagger-tag" }
3030
onetagger-tagger = { path = "../onetagger-tagger" }
3131
onetagger-player = { path = "../onetagger-player" }
3232
onetagger-shared = { path = "../onetagger-shared" }
33+
onetagger-renamer = { path = "../onetagger-renamer" }
3334
onetagger-platforms = { path = "../onetagger-platforms" }

crates/onetagger-autotag/src/lib.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::collections::HashMap;
66
use anyhow::Error;
7+
use onetagger_renamer::{Renamer, RenamerConfig, TemplateParser};
78
use rand::seq::SliceRandom;
89
use std::sync::atomic::{AtomicBool, Ordering};
910
use std::sync::{Arc, Mutex};
@@ -275,6 +276,7 @@ impl TrackImpl for Track {
275276
}
276277

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

291293
tag.set_art(CoverType::CoverFront, "image/jpeg", Some("Cover"), data.clone());
292-
// Save to file
293-
if config.album_art_file {
294-
let path = path.as_ref().parent().unwrap().join("cover.jpg");
295-
if !path.exists() {
296-
if let Ok(mut file) = File::create(path) {
297-
file.write_all(&data).ok();
298-
}
299-
}
300-
}
294+
cover_data = Some(data);
301295
},
302296
None => warn!("Invalid album art!")
303297
}
@@ -327,7 +321,24 @@ impl TrackImpl for Track {
327321
}
328322

329323
// Save
330-
tag.save_file(path.as_ref())?;
324+
tag.save_file(&path.as_ref())?;
325+
326+
// Cover file
327+
if let Some(cover_data) = cover_data {
328+
match AudioFileInfo::load_file(&path, None, None) {
329+
Ok(info) => {
330+
let cover_path = get_cover_path(&info, path.as_ref().parent().unwrap(), config);
331+
match std::fs::write(&cover_path, cover_data) {
332+
Ok(_) => debug!("Cover written to: {}", cover_path.display()),
333+
Err(e) => error!("Failed to write cover file: {e}"),
334+
}
335+
},
336+
Err(e) => {
337+
error!("Failed generating cover path: {e}");
338+
}
339+
}
340+
}
341+
331342
Ok(())
332343
}
333344

@@ -379,6 +390,23 @@ impl TrackImpl for Track {
379390

380391
}
381392

393+
/// Get path to cover file
394+
fn get_cover_path(info: &AudioFileInfo, folder: impl AsRef<Path>, config: &TaggerConfig) -> PathBuf {
395+
let mut path = folder.as_ref().join("cover.jpg");
396+
397+
if let Some(template) = config.cover_filename.as_ref() {
398+
if !template.trim().is_empty() {
399+
// Generate new filename
400+
let renamer_config = RenamerConfig::default_with_paths(&folder, template);
401+
let mut renamer = Renamer::new(TemplateParser::parse(template));
402+
let new_path = renamer.generate_name(folder.as_ref(), info, &renamer_config);
403+
path = new_path.with_extension("jpg");
404+
}
405+
}
406+
407+
path
408+
}
409+
382410
pub trait AudioFileInfoImpl {
383411
/// Load audio file info from path
384412
fn load_file(path: impl AsRef<Path>, filename_template: Option<Regex>, title_regex: Option<Regex>) -> Result<AudioFileInfo, Error>;

crates/onetagger-renamer/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ pub struct RenamerConfig {
170170
pub keep_subfolders: bool,
171171
}
172172

173+
impl RenamerConfig {
174+
/// Create new instance with default properties and paths
175+
pub fn default_with_paths(path: impl AsRef<Path>, template: &str) -> RenamerConfig {
176+
RenamerConfig {
177+
path: path.as_ref().to_path_buf(),
178+
out_dir: None,
179+
template: template.to_string(),
180+
copy: false,
181+
overwrite: false,
182+
keep_subfolders: false,
183+
separator: ", ".to_owned(),
184+
subfolders: true,
185+
}
186+
}
187+
}
188+
173189
/// HTML generation test
174190
#[test]
175191
fn generate_html() {

crates/onetagger-tagger/src/custom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use log::{Record, Level, RecordBuilder};
44
use crate::TrackMatch;
55

66
/// Version of supported custom platform
7-
pub const CUSTOM_PLATFORM_COMPATIBILITY: i32 = 44;
7+
pub const CUSTOM_PLATFORM_COMPATIBILITY: i32 = 45;
88

99
/// Logging from plugins
1010
#[no_mangle]

crates/onetagger-tagger/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pub struct TaggerConfig {
8585
pub album_tagging: bool,
8686
/// % of tracks that have to be from one album to be considered as the correct
8787
pub album_tagging_ratio: f32,
88+
/// Renamer template
89+
pub cover_filename: Option<String>,
8890

8991
/// Platform specific. Format: `{ platform: { custom_option: value }}`
9092
pub custom: PlatformTaggerConfig,
@@ -166,7 +168,8 @@ impl Default for TaggerConfig {
166168
id3_comm_lang: None,
167169
fetch_all_results: false,
168170
album_tagging: false,
169-
album_tagging_ratio: 0.5
171+
album_tagging_ratio: 0.5,
172+
cover_filename: None
170173
}
171174
}
172175
}

0 commit comments

Comments
 (0)