feat: recursively scan through directory, refactor library and import functions, add more basic information to schema
This commit is contained in:
80
src/main.rs
80
src/main.rs
@@ -1,72 +1,50 @@
|
||||
use std::{fs::metadata, path::Path};
|
||||
use std::{
|
||||
fs::metadata,
|
||||
path::{Path, PathBuf},
|
||||
string,
|
||||
};
|
||||
|
||||
use diesel::prelude::*;
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
|
||||
mod library_scan;
|
||||
mod types;
|
||||
mod library;
|
||||
mod models;
|
||||
mod schema;
|
||||
mod types;
|
||||
|
||||
use library::{Importer, Library};
|
||||
|
||||
fn open_db_file() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL missing");
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL missing");
|
||||
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
fn open_db_in_memory() -> SqliteConnection {
|
||||
SqliteConnection::establish(":memory:")
|
||||
.unwrap_or_else(|_| panic!("Error starting in-memory database"))
|
||||
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
let mut conn = SqliteConnection::establish(":memory:")
|
||||
.unwrap_or_else(|_| panic!("Error starting in-memory database"));
|
||||
|
||||
conn.run_pending_migrations(MIGRATIONS)
|
||||
.unwrap_or_else(|e| panic!("Error applying in-memory migrations: {}", e));
|
||||
|
||||
conn
|
||||
}
|
||||
|
||||
fn import_scan(conn: &mut SqliteConnection) {
|
||||
for track in library_scan::scan(Path::new("./test/fixtures")).expect("oh no") {
|
||||
println!("-- {}", track.path.into_string().unwrap());
|
||||
|
||||
match track.metadata {
|
||||
Ok(metadata) => {
|
||||
println!("artist: {}", metadata.artist.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"collection: {}",
|
||||
metadata.collection.as_deref().unwrap_or("unknown")
|
||||
);
|
||||
println!("title: {}", metadata.title.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"number: {}",
|
||||
metadata
|
||||
.number
|
||||
.map(|value| value.to_string())
|
||||
.unwrap_or_else(|| "unknown".to_owned())
|
||||
);
|
||||
println!("genre: {}", metadata.genre.as_deref().unwrap_or("unknown"));
|
||||
println!(
|
||||
"datetime: {}",
|
||||
metadata
|
||||
.datetime
|
||||
.as_ref()
|
||||
.map(|value| format!("{value:?}"))
|
||||
.unwrap_or_else(|| "unknown".to_owned())
|
||||
);
|
||||
println!("length_sec: {}", metadata.length_sec);
|
||||
}
|
||||
Err(e) => {
|
||||
eprint!("{e}");
|
||||
}
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
dotenv().ok();
|
||||
|
||||
let conn = &mut open_db_in_memory();
|
||||
let mut library_instance = Library::new(open_db_in_memory());
|
||||
|
||||
import_scan(conn);
|
||||
|
||||
let import_path = env::var("IMPORT_PATH").expect("IMPORT_PATH not set!");
|
||||
|
||||
Importer::import_scan(&mut library_instance, Path::new(&import_path))
|
||||
.expect("Error importing files");
|
||||
|
||||
library_instance.print().expect("Error printing library");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user