use crate::prelude::*;
use crate::utils::{convert_to_id3v2, fix_track_numbering, get_vorbis_tags};
use claxon::FlacReader;
use claxon::metadata::StreamInfo;
use lofty::tag::Tag;
use once_cell::sync::OnceCell;
pub struct FlacFile {
pub path: PathBuf,
pub file_name: String,
pub sub_dir: PathBuf,
id3_tags: OnceCell<Tag>,
pub disc_context: Option<DiscContext>,
}
impl FlacFile {
#[must_use]
pub fn new(path: PathBuf, source_dir: &PathBuf) -> Self {
let sub_dir = path
.strip_prefix(source_dir)
.expect("Flac file path should start with the source directory")
.parent()
.expect("Flac file path should have a parent directory")
.to_path_buf();
let file_name = path
.file_name()
.expect("Flac file should have a name")
.to_os_string()
.to_string_lossy()
.strip_suffix(".flac")
.expect("Flac file should .flac extension")
.to_owned();
FlacFile {
path,
file_name,
sub_dir,
id3_tags: OnceCell::new(),
disc_context: None,
}
}
pub fn id3_tags(&self) -> Result<&Tag, Failure<TranscodeAction>> {
self.id3_tags.get_or_try_init(|| {
let mut tags =
get_vorbis_tags(self).map_err(Failure::wrap(TranscodeAction::GetTags))?;
convert_to_id3v2(&mut tags);
let _ = fix_track_numbering(&mut tags);
Ok(tags)
})
}
#[must_use]
pub fn get_path_string(&self) -> String {
self.path.to_string_lossy().into_owned()
}
pub fn get_stream_info(&self) -> Result<StreamInfo, claxon::Error> {
let reader = FlacReader::open(&self.path)?;
Ok(reader.streaminfo())
}
}