made prepatory changes to the library

This commit is contained in:
MrDulfin 2024-02-16 22:24:02 -05:00
parent 3c93110037
commit c6b561c7af
5 changed files with 75 additions and 27 deletions

View file

@ -4,7 +4,7 @@ use std::{
io::{Error, Write, Read}, sync::{Arc, RwLock},
};
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use serde_json::to_string_pretty;
use thiserror::Error;
use uuid::Uuid;
@ -126,6 +126,23 @@ impl Config {
Ok(())
}
pub fn save_backup(&self) -> Result<(), Box<dyn std::error::Error>> {
match &self.backup_folder {
Some(path) => {
let mut writer = path.clone();
writer.set_extension("tmp");
let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(&writer)?;
let config = to_string_pretty(self)?;
// dbg!(&config);
file.write_all(config.as_bytes())?;
fs::rename(writer, self.path.as_path())?;
Ok(())
},
None => Err(ConfigError::NoBackupLibrary.into())
}
}
pub fn read_file(path: PathBuf) -> Result<Self, Error> {
let mut file: File = File::open(path)?;
let mut bun: String = String::new();
@ -144,6 +161,8 @@ pub enum ConfigError {
//TODO: do something about playlists
#[error("Please provide a better m3u8 Playlist")]
BadPlaylist,
#[error("No backup Config folder present")]
NoBackupLibrary,
}
@ -166,16 +185,16 @@ fn config_test() {
};
config.write_file();
let arc = Arc::new(RwLock::from(config));
MusicLibrary::init(arc.clone(), lib_a.uuid.clone()).unwrap();
MusicLibrary::init(arc.clone(), lib_b.uuid.clone()).unwrap();
MusicLibrary::init(arc.clone(), lib_c.uuid.clone()).unwrap();
MusicLibrary::init(arc.clone(), lib_a.uuid).unwrap();
MusicLibrary::init(arc.clone(), lib_b.uuid).unwrap();
MusicLibrary::init(arc.clone(), lib_c.uuid).unwrap();
}
#[test]
fn test2() {
let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap();
let uuid = config.libraries.get_default().unwrap().uuid.clone();
let uuid = config.libraries.get_default().unwrap().uuid;
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap();
lib.scan_folder("test-config/music/").unwrap();
lib.save(config.clone()).unwrap();
@ -187,7 +206,7 @@ fn test2() {
fn test3() {
let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap();
let uuid = config.libraries.get_default().unwrap().uuid;
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap();
let lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap();
dbg!(lib);
}

View file

@ -52,12 +52,12 @@ pub struct Controller {
// queues: Vec<Queue>,
config: Arc<RwLock<Config>>,
// library: MusicLibrary,
controller_mail: MailMan<ControllerCommand, ControllerResponse>,
db_mail: MailMan<DatabaseCommand, DatabaseResponse>,
queue_mail: Vec<MailMan<QueueCommand, QueueResponse>>,
controller_mail: MailMan<ControllerCmd, ControllerResponse>,
db_mail: MailMan<DatabaseCmd, DatabaseResponse>,
queue_mail: Vec<MailMan<QueueCmd, QueueResponse>>,
}
#[derive(Debug)]
pub enum ControllerCommand {
pub enum ControllerCmd {
Default,
Test
}
@ -65,12 +65,12 @@ pub enum ControllerCommand {
#[derive(Debug)]
enum ControllerResponse {
Empty,
QueueMailMan(MailMan<QueueCommand, QueueResponse>),
QueueMailMan(MailMan<QueueCmd, QueueResponse>),
}
#[derive(Debug)]
pub enum DatabaseCommand {
pub enum DatabaseCmd {
Default,
Test,
GetSongs,
@ -88,7 +88,7 @@ enum DatabaseResponse {
}
#[derive(Debug)]
enum QueueCommand {
enum QueueCmd {
Default,
Test,
Play,
@ -128,7 +128,7 @@ impl<T, U> MailMan<T, U> {
}
pub fn send(&self, mail: T) -> Result<(), Box<dyn Error>> {
&self.tx.send(mail).unwrap();
self.tx.send(mail).unwrap();
Ok(())
}
@ -150,7 +150,7 @@ impl Controller {
let (out_thread_controller, in_thread) = MailMan::double();
let monitor_thread = spawn(move || {
use ControllerCommand::*;
use ControllerCmd::*;
loop {
let command = in_thread.recv().unwrap();
@ -166,7 +166,7 @@ impl Controller {
let (out_thread_db, in_thread) = MailMan::double();
let db_monitor = spawn(move || {
use DatabaseCommand::*;
use DatabaseCmd::*;
loop {
let command = in_thread.recv().unwrap();
@ -218,7 +218,7 @@ impl Controller {
}
fn get_db_songs(&self) -> Vec<Song> {
self.db_mail.send(DatabaseCommand::GetSongs);
self.db_mail.send(DatabaseCmd::GetSongs);
match self.db_mail.recv().unwrap() {
DatabaseResponse::Songs(songs) => songs,
_ => Vec::new()
@ -227,9 +227,9 @@ impl Controller {
}
pub fn new_queue(&mut self) {
let (out_thread_queue, in_thread) = MailMan::<QueueCommand, QueueResponse>::double();
let (out_thread_queue, in_thread) = MailMan::<QueueCmd, QueueResponse>::double();
let queues_monitor = spawn(move || {
use QueueCommand::*;
use QueueCmd::*;
let mut queue = Queue::new().unwrap();
loop {
let command = in_thread.recv().unwrap();
@ -263,27 +263,27 @@ impl Controller {
fn play(&self, index: usize) -> Result<(), Box<dyn Error>> {
let mail = &self.queue_mail[index];
mail.send(QueueCommand::Play)?;
mail.send(QueueCmd::Play)?;
dbg!(mail.recv()?);
Ok(())
}
fn set_songs(&self, index: usize, songs: Vec<Song>) -> Result<(), Box<dyn Error>> {
let mail = &self.queue_mail[index];
mail.send(QueueCommand::SetSongs(songs))?;
mail.send(QueueCmd::SetSongs(songs))?;
dbg!(mail.recv()?);
Ok(())
}
fn enqueue(&self, index: usize, uri: URI) -> Result<(), Box<dyn Error>> {
let mail = &self.queue_mail[index];
mail.send(QueueCommand::Enqueue(uri))?;
mail.send(QueueCmd::Enqueue(uri))?;
// dbg!(mail.recv()?);
Ok(())
}
fn scan_folder(&self, folder: String) -> Result<(), Box<dyn Error>> {
let mail = &self.db_mail;
mail.send(DatabaseCommand::ReadFolder(folder))?;
mail.send(DatabaseCmd::ReadFolder(folder))?;
dbg!(mail.recv()?);
Ok(())
}

View file

@ -183,6 +183,7 @@ impl FoobarPlaylistTrack {
plays: 0,
skips: 0,
favorited: false,
// banned: None,
rating: None,
format: None,
duration: self.duration,

View file

@ -15,7 +15,7 @@ use chrono::prelude::*;
use crate::config::config::{Config, ConfigLibrary};
use crate::music_storage::db_reader::extern_library::ExternalLibrary;
use crate::music_storage::library::{AlbumArt, MusicLibrary, Service, Song, Tag, URI};
use crate::music_storage::library::{AlbumArt, MusicLibrary, Service, Song, Tag, URI, BannedType};
use crate::music_storage::utils;
use urlencoding::decode;
@ -149,7 +149,7 @@ impl ExternalLibrary for ITunesLibrary {
continue;
}
let sug: URI = if track.location.contains("file://localhost/") {
let location: URI = if track.location.contains("file://localhost/") {
URI::Local(PathBuf::from(
decode(track.location.strip_prefix("file://localhost/").unwrap())
.unwrap()
@ -169,11 +169,16 @@ impl ExternalLibrary for ITunesLibrary {
let play_time_ = StdDur::from_secs(track.plays as u64 * dur.as_secs());
let ny: Song = Song {
location: sug,
location,
uuid: Uuid::new_v4(),
plays: track.plays,
skips: 0,
favorited: track.favorited,
// banned: if track.banned {
// Some(BannedType::All)
// }else {
// None
// },
rating: track.rating,
format: match FileFormat::from_file(PathBuf::from(&loc)) {
Ok(e) => Some(e),

View file

@ -115,6 +115,26 @@ impl ToString for Field {
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub enum BannedType {
Shuffle,
All,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub enum DoNotTrack {
// TODO: add services to not track
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
enum SongType {
// TODO: add song types
Main,
Instrumental,
Remix,
Custom(String)
}
/// Stores information about a single song
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Song {
@ -123,6 +143,7 @@ pub struct Song {
pub plays: i32,
pub skips: i32,
pub favorited: bool,
// pub banned: Option<BannedType>,
pub rating: Option<u8>,
pub format: Option<FileFormat>,
pub duration: Duration,
@ -261,6 +282,7 @@ impl Song {
plays: 0,
skips: 0,
favorited: false,
// banned: None,
rating: None,
format,
duration,
@ -384,6 +406,7 @@ impl Song {
plays: 0,
skips: 0,
favorited: false,
// banned: None,
rating: None,
format,
duration,
@ -397,7 +420,7 @@ impl Song {
tracks.push((new_song, audio_location.clone()));
}
}
Ok((tracks))
Ok(tracks)
}
}