Skip to content

Commit

Permalink
Drop dependency on autotag for renamer
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed Oct 14, 2024
1 parent 71fe5f1 commit 20c7117
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions crates/onetagger-autotag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ pub trait AudioFileInfoImpl {
fn shazam(path: impl AsRef<Path>) -> Result<AudioFileInfo, Error>;
/// Get list of all files in with supported extensions
fn get_file_list(path: impl AsRef<Path>, subfolders: bool) -> Vec<PathBuf>;
/// Get iterator of all audio files in path
fn load_files_iter(path: impl AsRef<Path>, subfolders: bool, filename_template: Option<Regex>, title_regex: Option<Regex>) -> impl Iterator<Item = Result<AudioFileInfo, Error>>;
}

impl AudioFileInfoImpl for AudioFileInfo {
Expand Down Expand Up @@ -550,6 +552,11 @@ impl AudioFileInfoImpl for AudioFileInfo {
}
}
}

fn load_files_iter(path: impl AsRef<Path>, subfolders: bool, filename_template: Option<Regex>, title_regex: Option<Regex>) -> impl Iterator<Item = Result<AudioFileInfo, Error>> {
let files = Self::get_file_list(path, subfolders);
files.into_iter().map(move |f| Self::load_file(&f, filename_template.clone(), title_regex.clone()))
}
}


Expand Down
5 changes: 3 additions & 2 deletions crates/onetagger-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,18 @@ fn main() {
keep_subfolders: *keep_subfolders,
};
let mut renamer = Renamer::new(TemplateParser::parse(&template));
let files = AudioFileInfo::load_files_iter(&config.path, config.subfolders, None, None);
let names = renamer.generate(files, &config).expect("Failed generating filenames!");

// Only preview
if *preview {
let names = renamer.generate(&config, 0).expect("Failed generating filenames!");
for (i, (from, to)) in names.iter().enumerate() {
println!("{}. {:?} -> {:?}", i + 1, from, to);
}
return;
}

renamer.rename(&config).expect("Failed renaming!");
renamer.rename(&names, &config).expect("Failed renaming!");
},
// Server mode
Actions::Server { expose, path, browser } => {
Expand Down
1 change: 0 additions & 1 deletion crates/onetagger-renamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ serde = { version = "1.0", features = ["derive"] }

onetagger-tag = { path = "../onetagger-tag" }
onetagger-tagger = { path = "../onetagger-tagger" }
onetagger-autotag = { path = "../onetagger-autotag" }
27 changes: 13 additions & 14 deletions crates/onetagger-renamer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use std::path::{Path, PathBuf};
use anyhow::Error;
use onetagger_autotag::AudioFileInfoImpl;
use onetagger_tagger::AudioFileInfo;
use serde::{Serialize, Deserialize};

Expand Down Expand Up @@ -42,7 +41,10 @@ impl Renamer {


/// Generate names - output: [(from, to),...]
pub fn generate(&mut self, config: &RenamerConfig, limit: usize) -> Result<Vec<(PathBuf, PathBuf)>, Error> {
pub fn generate<I>(&mut self, files: I, config: &RenamerConfig) -> Result<Vec<(PathBuf, PathBuf)>, Error>
where
I: IntoIterator<Item = Result<AudioFileInfo, Error>>
{
let input_path = dunce::canonicalize(&config.path)?;
if !input_path.exists() {
return Err(anyhow!("Invalid path!"));
Expand All @@ -54,16 +56,17 @@ impl Renamer {
out_dir = config.path.to_owned();
}

let files = AudioFileInfo::get_file_list(&config.path, config.subfolders);
let mut output = vec![];
for (i, file) in files.iter().enumerate() {
let info = match AudioFileInfo::load_file(&file, None, None) {
Ok(info) => info,
for file in files.into_iter() {
// Load files
let info = match file {
Ok(i) => i,
Err(e) => {
warn!("Failed loading: {file:?}. Skipping! {e}");
warn!("Failed loading file: {e}");
continue;
}
},
};
let file = &info.path;

// Get output dir
let mut output_dir = Path::new(&out_dir).to_owned();
Expand All @@ -84,16 +87,12 @@ impl Renamer {

let new_name = self.generate_name(output_dir, &info, config);
output.push((file.to_owned(), new_name));
if limit != 0 && i >= limit {
break
}
}
Ok(output)
}

/// Rename files
pub fn rename(&mut self, config: &RenamerConfig) -> Result<(), Error> {
let files = self.generate(config, 0)?;
/// Rename files, files = output from generate
pub fn rename(&mut self, files: &[(PathBuf, PathBuf)], config: &RenamerConfig) -> Result<(), Error> {
for (from, to) in files {
// Don't overwrite
if !config.overwrite && to.exists() {
Expand Down
7 changes: 5 additions & 2 deletions crates/onetagger-ui/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ async fn handle_message(text: &str, websocket: &mut WebSocket, context: &mut Soc
// Generate new names but don't rename
Action::RenamerPreview { config } => {
let mut renamer = Renamer::new(TemplateParser::parse(&config.template));
let files = renamer.generate(&config, 3).unwrap_or(vec![]);
let files = AudioFileInfo::load_files_iter(&config.path, config.subfolders, None, None);
let files = renamer.generate(files.take(3), &config).unwrap_or(vec![]);
send_socket(websocket, json!({
"action": "renamerPreview",
"files": files,
Expand All @@ -548,7 +549,9 @@ async fn handle_message(text: &str, websocket: &mut WebSocket, context: &mut Soc
// Start renamer
Action::RenamerStart { config } => {
let mut renamer = Renamer::new(TemplateParser::parse(&config.template));
renamer.rename(&config)?;
let files = AudioFileInfo::load_files_iter(&config.path, config.subfolders, None, None);
let files = renamer.generate(files, &config)?;
renamer.rename(&files, &config)?;
send_socket(websocket, json!({
"action": "renamerDone",
})).await.ok();
Expand Down

0 comments on commit 20c7117

Please sign in to comment.