feat: scanning somewhat cromulent
now to make sure it can play again ;3
This commit is contained in:
parent
71b7eca0b9
commit
56bc015b7a
2 changed files with 58 additions and 65 deletions
2
Justfile
2
Justfile
|
|
@ -9,6 +9,6 @@ mount:
|
|||
run: mount
|
||||
RAVE_STORAGE_DIR=/tmp/media-for-rave cargo r
|
||||
|
||||
update-db:
|
||||
refresh:
|
||||
sea migrate fresh
|
||||
sea generate entity -o ./entities/src --with-serde both --date-time-crate time --lib
|
||||
|
|
@ -83,7 +83,9 @@ async fn scan() {
|
|||
continue;
|
||||
};
|
||||
|
||||
tokio::spawn(do_entry(de, txn, state.clone()));
|
||||
// tokio::spawn(do_entry(de, txn, state.clone())); // multithreading version
|
||||
|
||||
do_entry(de, txn, state.clone()).await; // test without multithreading
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -96,11 +98,13 @@ async fn scan() {
|
|||
const VALID_EXTENSIONS: &[&str] = &["mp3"];
|
||||
|
||||
async fn do_entry(de: walk::DirEntry, txn: DatabaseTransaction, state: Arc<RwLock<ScanState>>) {
|
||||
if let Err(e) = handle_entry(&txn, de, state).await {
|
||||
if let Err(e) = handle_entry(&txn, de.clone(), state).await {
|
||||
let _ = txn.rollback().await;
|
||||
let path = de.path();
|
||||
let path = path.to_string_lossy();
|
||||
error!(
|
||||
error = e.root_cause(),
|
||||
"Failed to handle directory entry: {e}"
|
||||
"Failed to handle directory entry `{path}`: {e}"
|
||||
);
|
||||
|
||||
{
|
||||
|
|
@ -175,20 +179,11 @@ async fn handle_entry(
|
|||
|
||||
let meta = { File::open(&path).await?.metadata().await? };
|
||||
|
||||
let current_album = { state.read().await.album_id };
|
||||
|
||||
let tag = Tag::async_read_from_path(&path).await?;
|
||||
|
||||
let artist = find_artist(tx, &tag).await?;
|
||||
|
||||
let album = find_album(
|
||||
tx,
|
||||
current_album,
|
||||
artist.as_ref().map(|c| c.id),
|
||||
&tag,
|
||||
state.clone(),
|
||||
)
|
||||
.await?;
|
||||
let album = find_album(tx, artist.as_ref().map(|c| c.id), &tag, state.clone()).await?;
|
||||
|
||||
let mut am = track::ActiveModel::new();
|
||||
|
||||
|
|
@ -219,35 +214,33 @@ async fn handle_entry(
|
|||
#[instrument(skip(tx, tag, state))]
|
||||
async fn find_album(
|
||||
tx: &DatabaseTransaction,
|
||||
current_album: Option<i64>,
|
||||
artist_id: Option<i64>,
|
||||
tag: &Tag,
|
||||
state: Arc<RwLock<ScanState>>,
|
||||
) -> Result<album::Model, Report> {
|
||||
if let Some(current_album) = current_album {
|
||||
let album = Album::find_by_id(current_album).one(tx).await?;
|
||||
|
||||
let Some(album) = album else {
|
||||
error!("Couldn't find album with id {current_album}");
|
||||
|
||||
return Err(Report::msg(format!(
|
||||
"Couldn't find album with id {current_album}"
|
||||
)));
|
||||
let Some(album_name) = tag.album() else {
|
||||
return Err(Report::msg("Couldn't get album name from tag"));
|
||||
};
|
||||
|
||||
Ok(album)
|
||||
} else {
|
||||
let mut am = album::ActiveModel::new();
|
||||
// if not, search by name
|
||||
let search = Album::find()
|
||||
.filter(album::Column::Name.like(album_name))
|
||||
.one(tx)
|
||||
.await?;
|
||||
|
||||
if let Some(tag_album) = tag.album() {
|
||||
am.name = Set(tag_album.to_string());
|
||||
} else {
|
||||
am.name = Set("Unknown Album".to_string());
|
||||
// if we found one, return it
|
||||
if let Some(search) = search {
|
||||
return Ok(search);
|
||||
}
|
||||
|
||||
// otherwise, create it
|
||||
let mut am = album::ActiveModel::new();
|
||||
|
||||
am.name = Set(album_name.to_string());
|
||||
am.music_folder_id = Set(state.read().await.music_folder_id);
|
||||
am.artist_id = Set(artist_id);
|
||||
am.year = Set(tag.year());
|
||||
|
||||
let genre = tag.genre_parsed();
|
||||
if let Some(genre) = genre {
|
||||
let genre_id = find_or_create_genre(tx, genre.as_ref()).await?;
|
||||
|
|
@ -262,6 +255,7 @@ async fn find_album(
|
|||
|
||||
let model = Album::insert(am).exec_with_returning(tx).await;
|
||||
|
||||
// if we failed to insert, return the error
|
||||
let Ok(model) = model else {
|
||||
let err = model.expect_err("somehow not err");
|
||||
error!(
|
||||
|
|
@ -273,7 +267,6 @@ async fn find_album(
|
|||
};
|
||||
|
||||
Ok(model)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tx))]
|
||||
|
|
@ -327,8 +320,9 @@ async fn find_artist(tx: &DatabaseTransaction, tag: &Tag) -> Result<Option<artis
|
|||
|
||||
match &artist_to_search {
|
||||
Some(artist_to_search) => {
|
||||
let artist_to_search = artist_to_search.trim();
|
||||
let attempt = Artist::find()
|
||||
.filter(artist::Column::Name.contains(artist_to_search))
|
||||
.filter(artist::Column::Name.like(artist_to_search))
|
||||
.one(tx)
|
||||
.await?;
|
||||
|
||||
|
|
@ -336,7 +330,7 @@ async fn find_artist(tx: &DatabaseTransaction, tag: &Tag) -> Result<Option<artis
|
|||
Ok(Some(attempt))
|
||||
} else {
|
||||
let am = artist::ActiveModel {
|
||||
name: Set(artist_to_search.clone()),
|
||||
name: Set(artist_to_search.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
let model = Artist::insert(am).exec_with_returning(tx).await;
|
||||
|
|
@ -361,7 +355,6 @@ async fn find_artist(tx: &DatabaseTransaction, tag: &Tag) -> Result<Option<artis
|
|||
#[derive(Debug, Default)]
|
||||
struct ScanState {
|
||||
pub music_folder_id: i64,
|
||||
pub album_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[instrument(skip(dbc, state))]
|
||||
|
|
|
|||
Loading…
Reference in a new issue