This commit is contained in:
JMARyA 2024-10-07 15:50:46 +02:00
parent 70d45e42a8
commit d309c5d8c1
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 59 additions and 2 deletions

View file

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::prelude::FromRow;
use std::str::FromStr;
use std::{io::Read, str::FromStr};
use crate::{
get_pg,
@ -325,6 +325,47 @@ impl Track {
.unwrap()
}
pub async fn get_lyrics(&self) -> Option<serde_json::Value> {
let base_path = std::path::Path::new(&self.path).with_extension("");
let extensions = vec!["lrc", "txt"];
for ext in &extensions {
let candidate = base_path.with_extension(ext);
if candidate.exists() {
if let Ok(mut file) = std::fs::File::open(&candidate) {
let mut contents = String::new();
if file.read_to_string(&mut contents).is_ok() {
// Determine if it's timed lyrics or plain text
let lyrics_type = if ext == &"lrc" { "timed" } else { "plain" };
return Some(json!({
"lyrics": contents,
"source": "file",
"type": lyrics_type
}));
}
}
}
}
if let Some(lyrics) = self.meta.as_ref()?.get("lyrics").and_then(|l| l.as_str()) {
// Check if lyrics seem to be timed or plain text
let lyrics_type = if lyrics.contains('[') && lyrics.contains(']') {
"timed" // A rough check for LRC-style brackets indicating timing
} else {
"plain"
};
return Some(json!({
"lyrics": lyrics,
"type": lyrics_type
}));
}
// No lyrics found
None
}
/// Finds the first track of a given album.
pub async fn find_first_of_album(album: &uuid::Uuid) -> Option<Self> {
sqlx::query_as("SELECT * FROM track WHERE album = $1 LIMIT 1")