mirror of
https://github.com/Dangoware/dmp-core.git
synced 2025-04-19 13:22:54 -05:00
Fixed some search issues
This commit is contained in:
parent
e878e72a99
commit
37b393246f
2 changed files with 41 additions and 45 deletions
|
@ -730,49 +730,43 @@ impl MusicLibrary {
|
||||||
|
|
||||||
// Sort the returned list of songs
|
// Sort the returned list of songs
|
||||||
new_songs.par_sort_by(|a, b| {
|
new_songs.par_sort_by(|a, b| {
|
||||||
for opt in sort_by {
|
for sort_option in sort_by {
|
||||||
let tag_a = match opt {
|
let tag_a = match sort_option {
|
||||||
Tag::Field(field_selection) => match a.get_field(field_selection) {
|
Tag::Field(field_selection) => match a.get_field(field_selection) {
|
||||||
Some(field_value) => field_value,
|
Some(field_value) => field_value,
|
||||||
None => continue,
|
None => continue,
|
||||||
},
|
},
|
||||||
_ => match a.get_tag(&opt) {
|
_ => match a.get_tag(&sort_option) {
|
||||||
Some(tag_value) => tag_value.to_owned(),
|
Some(tag_value) => tag_value.to_owned(),
|
||||||
None => continue,
|
None => continue,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let tag_b = match opt {
|
let tag_b = match sort_option {
|
||||||
Tag::Field(field_selection) => match b.get_field(field_selection) {
|
Tag::Field(field_selection) => match b.get_field(field_selection) {
|
||||||
Some(field_value) => field_value,
|
Some(field_value) => field_value,
|
||||||
None => continue,
|
None => continue,
|
||||||
},
|
},
|
||||||
_ => match b.get_tag(&opt) {
|
_ => match b.get_tag(&sort_option) {
|
||||||
Some(tag_value) => tag_value.to_owned(),
|
Some(tag_value) => tag_value.to_owned(),
|
||||||
None => continue,
|
None => continue,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to parse the tags as f64
|
|
||||||
if let (Ok(num_a), Ok(num_b)) = (tag_a.parse::<i32>(), tag_b.parse::<i32>()) {
|
if let (Ok(num_a), Ok(num_b)) = (tag_a.parse::<i32>(), tag_b.parse::<i32>()) {
|
||||||
// If parsing succeeds, compare as numbers
|
// If parsing succeeds, compare as numbers
|
||||||
if num_a < num_b {
|
return num_a.cmp(&num_b);
|
||||||
return std::cmp::Ordering::Less;
|
|
||||||
} else if num_a > num_b {
|
|
||||||
return std::cmp::Ordering::Greater;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// If parsing fails, compare as strings
|
// If parsing fails, compare as strings
|
||||||
if tag_a < tag_b {
|
return tag_a.cmp(&tag_b);
|
||||||
return std::cmp::Ordering::Less;
|
|
||||||
} else if tag_a > tag_b {
|
|
||||||
return std::cmp::Ordering::Greater;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all tags are equal, sort by Track number
|
// If all tags are equal, sort by Track number
|
||||||
a.get_tag(&Tag::Track).cmp(&b.get_tag(&Tag::Track))
|
let path_a = PathBuf::from(a.get_field("location").unwrap());
|
||||||
|
let path_b = PathBuf::from(b.get_field("location").unwrap());
|
||||||
|
|
||||||
|
path_a.file_name().cmp(&path_b.file_name())
|
||||||
});
|
});
|
||||||
|
|
||||||
if new_songs.len() > 0 {
|
if new_songs.len() > 0 {
|
||||||
|
@ -786,22 +780,22 @@ impl MusicLibrary {
|
||||||
pub fn albums(&self) -> BTreeMap<String, Album> {
|
pub fn albums(&self) -> BTreeMap<String, Album> {
|
||||||
let mut albums: BTreeMap<String, Album> = BTreeMap::new();
|
let mut albums: BTreeMap<String, Album> = BTreeMap::new();
|
||||||
for result in &self.library {
|
for result in &self.library {
|
||||||
let title = match result.get_tag(&Tag::Album){
|
let title = match result.get_tag(&Tag::Album) {
|
||||||
Some(title) => title,
|
Some(title) => title,
|
||||||
None => continue
|
None => continue
|
||||||
};
|
};
|
||||||
let disc_num = result.get_tag(&Tag::Disk).unwrap_or(&"".to_string()).parse::<usize>().unwrap_or(1);
|
|
||||||
|
|
||||||
let norm_title = normalize(title);
|
let norm_title = normalize(title);
|
||||||
|
|
||||||
|
let disc_num = result.get_tag(&Tag::Disk).unwrap_or(&"".to_string()).parse::<usize>().unwrap_or(1);
|
||||||
match albums.get_mut(&norm_title) {
|
match albums.get_mut(&norm_title) {
|
||||||
|
// If the album is in the list, add the track to the appropriate disc in it
|
||||||
Some(album) => {
|
Some(album) => {
|
||||||
match album.discs.get_mut(&disc_num) {
|
match album.discs.get_mut(&disc_num) {
|
||||||
Some(disc) => disc.push(result),
|
Some(disc) => disc.push(result),
|
||||||
None => {
|
None => {album.discs.insert(disc_num, vec![result]);}
|
||||||
album.discs.insert(disc_num, vec![result]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// If the album is not in the list, make a new one and add it
|
||||||
None => {
|
None => {
|
||||||
let new_album = Album {
|
let new_album = Album {
|
||||||
title,
|
title,
|
||||||
|
@ -814,27 +808,29 @@ impl MusicLibrary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort the tracks in each disk in each album
|
||||||
let blank = String::from("");
|
let blank = String::from("");
|
||||||
albums.par_iter_mut().for_each(|album| {
|
albums.par_iter_mut().for_each(|album| {
|
||||||
for disc in &mut album.1.discs {
|
for disc in &mut album.1.discs {
|
||||||
disc.1.par_sort_by(|a, b| {
|
disc.1.par_sort_by(|a, b| {
|
||||||
if let (Ok(num_a), Ok(num_b)) = (
|
let a_track = a.get_tag(&Tag::Track).unwrap_or(&blank);
|
||||||
a.get_tag(&Tag::Title).unwrap_or(&blank).parse::<i32>(),
|
let b_track = b.get_tag(&Tag::Track).unwrap_or(&blank);
|
||||||
b.get_tag(&Tag::Title).unwrap_or(&blank).parse::<i32>()
|
|
||||||
) {
|
if let (Ok(num_a), Ok(num_b)) = (a_track.parse::<i32>(), b_track.parse::<i32>()) {
|
||||||
// If parsing succeeds, compare as numbers
|
// If parsing the track numbers succeeds, compare as numbers
|
||||||
match num_a < num_b {
|
num_a.cmp(&num_b)
|
||||||
true => return std::cmp::Ordering::Less,
|
} else {
|
||||||
false => return std::cmp::Ordering::Greater
|
// If parsing doesn't succeed, compare the locations
|
||||||
}
|
let path_a = PathBuf::from(a.get_field("location").unwrap());
|
||||||
}
|
let path_b = PathBuf::from(b.get_field("location").unwrap());
|
||||||
match a.get_field("location").unwrap() < b.get_field("location").unwrap() {
|
|
||||||
true => return std::cmp::Ordering::Less,
|
path_a.file_name().cmp(&path_b.file_name())
|
||||||
false => return std::cmp::Ordering::Greater
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Return the albums!
|
||||||
albums
|
albums
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
use std::io::{BufReader, BufWriter};
|
use std::io::{BufReader, BufWriter};
|
||||||
use std::{path::PathBuf, error::Error, fs};
|
use std::{path::PathBuf, error::Error, fs};
|
||||||
use flate2::Compression;
|
|
||||||
use flate2::write::ZlibEncoder;
|
|
||||||
use flate2::read::ZlibDecoder;
|
|
||||||
|
|
||||||
use snap;
|
use snap;
|
||||||
|
|
||||||
|
@ -10,37 +7,40 @@ use unidecode::unidecode;
|
||||||
use crate::music_storage::music_db::Song;
|
use crate::music_storage::music_db::Song;
|
||||||
|
|
||||||
pub fn normalize(input_string: &String) -> String {
|
pub fn normalize(input_string: &String) -> String {
|
||||||
|
// Normalize the unicode and convert everything to lowercase
|
||||||
let mut normalized = unidecode(input_string);
|
let mut normalized = unidecode(input_string);
|
||||||
|
|
||||||
// Remove non alphanumeric characters
|
// Remove non alphanumeric characters
|
||||||
normalized.retain(|c| c.is_alphabetic());
|
normalized.retain(|c| c.is_alphanumeric());
|
||||||
normalized = normalized.to_ascii_lowercase();
|
|
||||||
|
|
||||||
normalized
|
normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> {
|
pub fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||||
|
|
||||||
|
// Create a new snap reader over the database file
|
||||||
let database = fs::File::open(path)?;
|
let database = fs::File::open(path)?;
|
||||||
let reader = BufReader::new(database);
|
let reader = BufReader::new(database);
|
||||||
//let mut d = ZlibDecoder::new(reader);
|
|
||||||
|
|
||||||
let mut d = snap::read::FrameDecoder::new(reader);
|
let mut d = snap::read::FrameDecoder::new(reader);
|
||||||
|
|
||||||
|
// Decode the library from the serialized data into the vec
|
||||||
let library: Vec<Song> = bincode::serde::decode_from_std_read(&mut d, bincode::config::standard().with_little_endian().with_variable_int_encoding())?;
|
let library: Vec<Song> = bincode::serde::decode_from_std_read(&mut d, bincode::config::standard().with_little_endian().with_variable_int_encoding())?;
|
||||||
Ok(library)
|
Ok(library)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_library(library: &Vec<Song>, path: PathBuf, take_backup: bool) -> Result<(), Box<dyn Error>> {
|
pub fn write_library(library: &Vec<Song>, path: PathBuf, take_backup: bool) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
|
// Create 2 new names for the file, a temporary one for writing out, and a backup
|
||||||
let mut writer_name = path.clone();
|
let mut writer_name = path.clone();
|
||||||
writer_name.set_extension("tmp");
|
writer_name.set_extension("tmp");
|
||||||
let mut backup_name = path.clone();
|
let mut backup_name = path.clone();
|
||||||
backup_name.set_extension("bkp");
|
backup_name.set_extension("bkp");
|
||||||
|
|
||||||
|
// Create a new BufWriter on the file and make a snap frame encoer for it too
|
||||||
let writer = BufWriter::new(fs::File::create(writer_name.to_path_buf())?);
|
let writer = BufWriter::new(fs::File::create(writer_name.to_path_buf())?);
|
||||||
//let mut e = ZlibEncoder::new(writer, Compression::default());
|
|
||||||
|
|
||||||
let mut e = snap::write::FrameEncoder::new(writer);
|
let mut e = snap::write::FrameEncoder::new(writer);
|
||||||
|
|
||||||
|
// Write out the data using bincode
|
||||||
bincode::serde::encode_into_std_write(&library, &mut e, bincode::config::standard().with_little_endian().with_variable_int_encoding())?;
|
bincode::serde::encode_into_std_write(&library, &mut e, bincode::config::standard().with_little_endian().with_variable_int_encoding())?;
|
||||||
|
|
||||||
if path.exists() && take_backup {
|
if path.exists() && take_backup {
|
||||||
|
|
Loading…
Reference in a new issue