119 lines
2.5 KiB
Rust
119 lines
2.5 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use crate::utils::format_detection::FileFormat;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Tags {
|
|
pub title: String,
|
|
pub artist: String,
|
|
}
|
|
|
|
impl Default for Tags {
|
|
fn default() -> Self {
|
|
Tags {
|
|
title: "".to_string(),
|
|
artist: "".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ReplayGainData {
|
|
pub track_gain: String,
|
|
pub track_peak: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ReplayGainRawData {
|
|
pub track_gain: f64,
|
|
pub track_peak: f64,
|
|
}
|
|
|
|
impl ReplayGainRawData {
|
|
pub fn to_normal(&self, is_ogg_opus: bool) -> ReplayGainData {
|
|
if is_ogg_opus {
|
|
ReplayGainData {
|
|
track_gain: format!("{:.6}", (self.track_gain * 256.0).ceil()),
|
|
track_peak: "".to_string(), // Not Required
|
|
}
|
|
} else {
|
|
ReplayGainData {
|
|
track_gain: format!("{:.2} dB", self.track_gain),
|
|
track_peak: format!("{:.6}", self.track_peak),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Debug, Clone)]
|
|
pub struct AudioFileInfo {
|
|
pub tags: Tags,
|
|
pub contains_replaygain: bool,
|
|
pub supports_replaygain: bool,
|
|
pub format: Option<FileFormat>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct File {
|
|
pub filename: String,
|
|
pub extension: String,
|
|
|
|
// relative path to file's folder
|
|
pub path_to: PathBuf,
|
|
// path to folder from source
|
|
pub path_from_source: PathBuf,
|
|
|
|
pub extra_files: Vec<File>,
|
|
|
|
pub info: AudioFileInfo,
|
|
}
|
|
|
|
impl File {
|
|
pub fn from_path(source_dir: String, full_path: PathBuf) -> File {
|
|
let full_file_path = PathBuf::from(&source_dir).join(full_path);
|
|
|
|
let filename_without_extension = full_file_path
|
|
.file_stem()
|
|
.expect("filename invalid")
|
|
.to_string_lossy()
|
|
.to_string();
|
|
|
|
let extension = full_file_path.extension();
|
|
|
|
let extension = if let Some(extension) = extension {
|
|
extension.to_string_lossy().to_string()
|
|
} else {
|
|
"".to_string()
|
|
};
|
|
|
|
let path_from_src = full_file_path
|
|
.strip_prefix(&source_dir)
|
|
.expect("couldn't get path relative to source");
|
|
|
|
let mut folder_path_from_src = path_from_src.to_path_buf();
|
|
folder_path_from_src.pop();
|
|
|
|
let path_to = PathBuf::from(&source_dir).join(&folder_path_from_src);
|
|
|
|
File {
|
|
filename: filename_without_extension,
|
|
extension,
|
|
path_from_source: folder_path_from_src,
|
|
path_to,
|
|
extra_files: Vec::new(),
|
|
info: AudioFileInfo::default(),
|
|
}
|
|
}
|
|
|
|
pub fn join_filename(&self) -> String {
|
|
format!("{}.{}", self.filename, self.extension)
|
|
}
|
|
pub fn join_path_to(&self) -> PathBuf {
|
|
PathBuf::from(&self.path_to).join(self.join_filename())
|
|
}
|
|
pub fn join_path_from_source(&self) -> PathBuf {
|
|
PathBuf::from(&self.path_from_source).join(self.join_filename())
|
|
}
|
|
}
|