From 94d5aaec40ee71e55139d919bef00f00212f5bf2 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Thu, 10 Jul 2025 23:33:28 +0300 Subject: [PATCH] fix empty googledrive parent dir value --- crates/dropbox/src/repo/dropbox_directory.rs | 16 ++++----- crates/dropbox/src/scan.rs | 8 +++-- .../src/repo/google_drive_directory.rs | 23 ++++++------ .../googledrive/src/repo/google_drive_path.rs | 25 ++++++++----- crates/googledrive/src/scan.rs | 35 +++++++++++++++---- crates/raichu/src/lib.rs | 2 +- crates/scrobbler/src/scrobbler.rs | 6 ++-- crates/spotify/src/main.rs | 13 +++++-- 8 files changed, 84 insertions(+), 44 deletions(-) diff --git a/crates/dropbox/src/repo/dropbox_directory.rs b/crates/dropbox/src/repo/dropbox_directory.rs index c9e201ce..6ddc4b59 100644 --- a/crates/dropbox/src/repo/dropbox_directory.rs +++ b/crates/dropbox/src/repo/dropbox_directory.rs @@ -1,5 +1,5 @@ -use sqlx::{Pool, Postgres}; use crate::{types::file::Entry, xata::dropbox_diretory::DropboxDirectory}; +use sqlx::{Pool, Postgres}; pub async fn create_dropbox_directory( pool: &Pool, @@ -7,8 +7,8 @@ pub async fn create_dropbox_directory( dropbox_id: &str, parent_dir: &str, ) -> Result<(), sqlx::Error> { - let results: Vec = sqlx::query_as( - r#" + let results: Vec = sqlx::query_as( + r#" SELECT * FROM dropbox_directories WHERE dropbox_id = $1 @@ -21,10 +21,10 @@ pub async fn create_dropbox_directory( .fetch_all(pool) .await?; - let parent_id = results.first().map(|d| d.xata_id.clone()); + let parent_id = results.first().map(|d| d.xata_id.clone()); - sqlx::query( - r#" + sqlx::query( + r#" INSERT INTO dropbox_directories ( dropbox_id, name, @@ -44,5 +44,5 @@ pub async fn create_dropbox_directory( .execute(pool) .await?; - Ok(()) -} \ No newline at end of file + Ok(()) +} diff --git a/crates/dropbox/src/scan.rs b/crates/dropbox/src/scan.rs index a546f796..9395c278 100644 --- a/crates/dropbox/src/scan.rs +++ b/crates/dropbox/src/scan.rs @@ -22,7 +22,10 @@ use crate::{ consts::AUDIO_EXTENSIONS, crypto::decrypt_aes_256_ctr, repo::{ - dropbox_directory::create_dropbox_directory, dropbox_path::create_dropbox_path, dropbox_token::{find_dropbox_refresh_token, find_dropbox_refresh_tokens}, track::get_track_by_hash + dropbox_directory::create_dropbox_directory, + dropbox_path::create_dropbox_path, + dropbox_token::{find_dropbox_refresh_token, find_dropbox_refresh_tokens}, + track::get_track_by_hash, }, token::generate_token, types::file::{Entry, EntryList}, @@ -291,7 +294,8 @@ pub fn scan_audio_files( let parent_path = Path::new(&path) .parent() .map(|p| p.to_string_lossy().to_string()); - let status = create_dropbox_path(&pool, &entry, &track, &dropbox_id, parent_path).await; + let status = + create_dropbox_path(&pool, &entry, &track, &dropbox_id, parent_path).await; println!("status: {:?}", status); // TODO: publish file metadata to nats diff --git a/crates/googledrive/src/repo/google_drive_directory.rs b/crates/googledrive/src/repo/google_drive_directory.rs index b1cd3017..f91ea48f 100644 --- a/crates/googledrive/src/repo/google_drive_directory.rs +++ b/crates/googledrive/src/repo/google_drive_directory.rs @@ -1,5 +1,5 @@ -use sqlx::{Pool, Postgres}; use crate::{types::file::File, xata::google_drive_directory::GoogleDriveDirectory}; +use sqlx::{Pool, Postgres}; pub async fn create_google_drive_directory( pool: &Pool, @@ -15,17 +15,17 @@ pub async fn create_google_drive_directory( WHERE google_drive_id = $1 AND file_id = $2 LIMIT 1 - "# + "#, ) .bind(google_drive_id) .bind(parent_id) .fetch_all(pool) .await?; - if results.is_empty() { - None - } else { - Some(results[0].clone()) - } + if results.is_empty() { + None + } else { + Some(results[0].clone()) + } } else { None }; @@ -35,10 +35,7 @@ pub async fn create_google_drive_directory( format!("{}/{}", p.path.trim_end_matches('/'), file.name), Some(p.xata_id), ), - None => ( - format!("/{}", file.name), - None, - ), + None => (format!("/{}", file.name), None), }; sqlx::query( @@ -52,7 +49,7 @@ pub async fn create_google_drive_directory( ) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING - "# + "#, ) .bind(google_drive_id) .bind(&file.name) @@ -63,4 +60,4 @@ pub async fn create_google_drive_directory( .await?; Ok(()) -} \ No newline at end of file +} diff --git a/crates/googledrive/src/repo/google_drive_path.rs b/crates/googledrive/src/repo/google_drive_path.rs index 6035c3d1..2088b14c 100644 --- a/crates/googledrive/src/repo/google_drive_path.rs +++ b/crates/googledrive/src/repo/google_drive_path.rs @@ -1,6 +1,9 @@ use sqlx::{Pool, Postgres}; -use crate::{types::file::File, xata::{google_drive_directory::GoogleDriveDirectory, track::Track}}; +use crate::{ + types::file::File, + xata::{google_drive_directory::GoogleDriveDirectory, track::Track}, +}; pub async fn create_google_drive_path( pool: &Pool, @@ -9,21 +12,25 @@ pub async fn create_google_drive_path( google_drive_id: &str, parent_dir: &str, ) -> Result<(), sqlx::Error> { - let parent_dir: Vec = sqlx::query_as( - r#" + let parent_dir = if parent_dir.is_empty() { + None + } else { + let parent_dirs: Vec = sqlx::query_as( + r#" SELECT * FROM google_drive_directories WHERE google_drive_id = $1 AND file_id = $2 LIMIT 1 "#, - ) - .bind(google_drive_id) - .bind(parent_dir) - .fetch_all(pool) - .await?; + ) + .bind(google_drive_id) + .bind(parent_dir) + .fetch_all(pool) + .await?; - let parent_dir = parent_dir.first().map(|d| d.clone().xata_id); + parent_dirs.first().map(|d| d.xata_id.clone()) + }; let result = sqlx::query( r#" diff --git a/crates/googledrive/src/scan.rs b/crates/googledrive/src/scan.rs index bc71a48d..0dd81a36 100644 --- a/crates/googledrive/src/scan.rs +++ b/crates/googledrive/src/scan.rs @@ -21,7 +21,10 @@ use crate::{ consts::AUDIO_EXTENSIONS, crypto::decrypt_aes_256_ctr, repo::{ - google_drive_directory::create_google_drive_directory, google_drive_path::create_google_drive_path, google_drive_token::{find_google_drive_refresh_token, find_google_drive_refresh_tokens}, track::get_track_by_hash + google_drive_directory::create_google_drive_directory, + google_drive_path::create_google_drive_path, + google_drive_token::{find_google_drive_refresh_token, find_google_drive_refresh_tokens}, + track::get_track_by_hash, }, token::generate_token, types::file::{File, FileList}, @@ -103,7 +106,13 @@ pub fn scan_audio_files( if file.mime_type == "application/vnd.google-apps.folder" { println!("Scanning folder: {}", file.name.bright_green()); - create_google_drive_directory(&pool, &file, &google_drive_id, parent_drive_file_id.as_deref()).await?; + create_google_drive_directory( + &pool, + &file, + &google_drive_id, + parent_drive_file_id.as_deref(), + ) + .await?; // TODO: publish folder metadata to nats @@ -296,8 +305,15 @@ pub fn scan_audio_files( match track { Some(track) => { println!("Track exists: {}", title.bright_green()); - let status = - create_google_drive_path(&pool, &file, &track, &google_drive_id, &file_id).await?; + let parent_drive_id = parent_drive_file_id.as_deref(); + let status = create_google_drive_path( + &pool, + &file, + &track, + &google_drive_id, + parent_drive_id.unwrap_or(""), + ) + .await?; println!("status: {:?}", status); // TODO: publish file metadata to nats @@ -347,8 +363,15 @@ pub fn scan_audio_files( let track = get_track_by_hash(&pool, &hash).await?; if let Some(track) = track { - let status = - create_google_drive_path(&pool, &file, &track, &google_drive_id, &file_id).await; + let parent_drive_id = parent_drive_file_id.as_deref(); + let status = create_google_drive_path( + &pool, + &file, + &track, + &google_drive_id, + parent_drive_id.unwrap_or(""), + ) + .await; println!("status: {:?}", status); diff --git a/crates/raichu/src/lib.rs b/crates/raichu/src/lib.rs index c9c573dd..d53ea4bf 100644 --- a/crates/raichu/src/lib.rs +++ b/crates/raichu/src/lib.rs @@ -5,7 +5,7 @@ use serde_json::json; use std::f32::consts::PI; use std::io::Cursor; use symphonia::core::audio::SampleBuffer; -use symphonia::core::codecs::{DecoderOptions, CODEC_TYPE_NULL}; +use symphonia::core::codecs::{CODEC_TYPE_NULL, DecoderOptions}; use symphonia::core::formats::FormatOptions; use symphonia::core::io::{MediaSource, MediaSourceStream}; use symphonia::core::meta::MetadataOptions; diff --git a/crates/scrobbler/src/scrobbler.rs b/crates/scrobbler/src/scrobbler.rs index b0b28d52..31d058c2 100644 --- a/crates/scrobbler/src/scrobbler.rs +++ b/crates/scrobbler/src/scrobbler.rs @@ -492,7 +492,10 @@ pub async fn scrobble_listenbrainz( let spotify_user = repo::spotify_account::get_spotify_account(pool, &did).await?; if let Some(spotify_user) = spotify_user { - if cache.get(&format!("{}:current", spotify_user.email))?.is_some() { + if cache + .get(&format!("{}:current", spotify_user.email))? + .is_some() + { println!( "{} {} - {}, currently scrobbling, skipping", "Currently scrobbling: ".yellow(), @@ -513,7 +516,6 @@ pub async fn scrobble_listenbrainz( return Ok(()); } - // set cache for 5 seconds to avoid duplicate scrobbles cache.setex( &format!("listenbrainz:cache:{}:{}:{}", artist, track, did), diff --git a/crates/spotify/src/main.rs b/crates/spotify/src/main.rs index fa521e79..f5679442 100644 --- a/crates/spotify/src/main.rs +++ b/crates/spotify/src/main.rs @@ -205,8 +205,10 @@ async fn main() -> Result<(), Box> { email.bright_green(), e.to_string().bright_red() ); - match rt.block_on(nc.publish("rocksky.spotify.user", email.clone().into())) { - Ok(_) => {}, + match rt + .block_on(nc.publish("rocksky.spotify.user", email.clone().into())) + { + Ok(_) => {} Err(e) => { println!( "{} Error publishing message to restart thread: {}", @@ -697,7 +699,12 @@ pub async fn find_spotify_users( &result.refresh_token, &hex::decode(env::var("SPOTIFY_ENCRYPTION_KEY")?)?, )?; - user_tokens.push((result.email.clone(), token, result.did.clone(), result.user_id.clone())); + user_tokens.push(( + result.email.clone(), + token, + result.did.clone(), + result.user_id.clone(), + )); } Ok(user_tokens) -- 2.51.2