Skip to content

Commit

Permalink
fix(tags): correctly handle vinyl edge cases for track_number tags
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYukine committed Apr 17, 2023
1 parent 5521e25 commit b53f16d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
40 changes: 34 additions & 6 deletions src/main.rs
Expand Up @@ -12,6 +12,7 @@ use lazy_static::lazy_static;
use redacted::util::create_description;
use regex::Regex;
use strum::IntoEnumIterator;
use tags::util::valid_tags;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use tokio::task::JoinSet;
Expand All @@ -22,7 +23,7 @@ use crate::config::models::RedOxideConfig;
use crate::fs::util::count_files_with_extension;
use crate::redacted::api::client::RedactedApi;
use crate::redacted::api::constants::TRACKER_URL;
use crate::redacted::models::{Category, ReleaseType};
use crate::redacted::models::{Category, Media, ReleaseType};
use crate::redacted::upload::TorrentUploadData;
use crate::redacted::util::perma_link;
use crate::transcode::util::copy_other_allowed_files;
Expand Down Expand Up @@ -155,8 +156,15 @@ async fn transcode(mut cmd: TranscodeCommand) -> anyhow::Result<()> {
SUCCESS, index_response.username
))?;

for url in &cmd.urls {
handle_url(url, &term, &mut api, &cmd, index_response.passkey.clone()).await?;
for url in cmd.urls.clone() {
handle_url(
url.as_str(),
&term,
&mut api,
&mut cmd,
index_response.passkey.clone(),
)
.await?;
}

Ok(())
Expand All @@ -166,7 +174,7 @@ async fn handle_url(
url: &str,
term: &Term,
api: &mut RedactedApi,
cmd: &TranscodeCommand,
cmd: &mut TranscodeCommand,
passkey: String,
) -> anyhow::Result<()> {
lazy_static! {
Expand Down Expand Up @@ -326,7 +334,17 @@ async fn handle_url(
return Ok(());
}

if !tags::util::valid_tags(&flac_path).await? {
let media = Media::from(&*torrent.media);

let (valid, invalid_track_number_vinyl) = valid_tags(&flac_path, &media).await?;

if !valid && invalid_track_number_vinyl {
term.write_line(&format!(
"{} Release is Vinyl and has either no set track number or in a non standard format (e.g. A1, A2 etc), you will be prompted once transcode is done to manually check & adjust the transcode tags as needed!", WARNING
))?;

cmd.automatic_upload = false;
} else if !valid {
term.write_line(&format!(
"{} Torrent {} in group {} has FLAC files with invalid tags, skipping...\n You might be able to trump it.",
WARNING, torrent_id, group_id
Expand Down Expand Up @@ -446,6 +464,16 @@ async fn handle_url(
m.println(format!("{} Transcoding Done!", SUCCESS))?;
m.clear()?;

if invalid_track_number_vinyl {
let mut prompt = Confirm::new();

prompt
.with_prompt(format!("{} Please check tags of trancoded media and adjust as needed (release is vinyl and has either no track number or in an non standard format e.g. A1, A2 etc which the audiotags library used can't parse), continue?", WARNING))
.default(true);

prompt.interact()?;
}

for (path, format, command) in &path_format_command_triple {
let release_name = path.file_name().unwrap().to_str().unwrap();

Expand Down Expand Up @@ -492,7 +520,7 @@ async fn handle_url(
.await?;

term.write_line(&format!(
"{} Moved transcoded release to content directory",
"{} Moved transcode release to content directory",
SUCCESS,
))?;
}
Expand Down
30 changes: 18 additions & 12 deletions src/tags/util.rs
@@ -1,5 +1,7 @@
use std::path::PathBuf;

use crate::redacted::models::Media;
use crate::redacted::models::Media::Vinyl;
use async_recursion::async_recursion;
use audiotags::{Tag, TagType};
use tokio::fs;
Expand All @@ -14,50 +16,54 @@ pub async fn copy_tags_to_mp3(from: &PathBuf, to: &PathBuf) -> anyhow::Result<()
}

#[async_recursion]
pub async fn valid_tags(flac_dir_path: &PathBuf) -> anyhow::Result<bool> {
pub async fn valid_tags(flac_dir_path: &PathBuf, media: &Media) -> anyhow::Result<(bool, bool)> {
let mut dir = fs::read_dir(flac_dir_path).await?;

while let Some(entry) = dir.next_entry().await? {
let path = entry.path();

if path.is_dir() {
if valid_tags(&path).await? {
return Ok(true);
let (valid, is_vinyl) = validate_tags_of_file(path, media)?;

if !valid {
return Ok((true, is_vinyl));
}
} else {
let file_name = path.file_name().unwrap().to_str().unwrap();

if file_name.ends_with(".flac") {
if !validate_tags_of_file(path) {
return Ok(false);
let (valid, is_vinyl) = validate_tags_of_file(path, media)?;

if !valid {
return Ok((false, is_vinyl));
}
}
}
}

return Ok(true);
return Ok((true, false));
}

pub fn validate_tags_of_file(path: PathBuf) -> bool {
pub fn validate_tags_of_file(path: PathBuf, media: &Media) -> anyhow::Result<(bool, bool)> {
let tag = Tag::new().read_from_path(&path).unwrap();

if tag.artist().is_none() {
return false;
return Ok((false, false));
}

if tag.album().is_none() {
return false;
return Ok((false, false));
}

if tag.title().is_none() {
return false;
return Ok((false, false));
}

let (track_number, total_tracks) = tag.track();

if track_number.is_none() {
return false;
return Ok((false, media == &Vinyl));
}

return true;
return Ok((true, false));
}

0 comments on commit b53f16d

Please sign in to comment.