1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
use directories::ProjectDirs;
use rusqlite::{Connection, Result as SqlResult};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Artist {
pub id: i64,
pub name: String,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Album {
pub id: i64,
pub artist_id: i64,
pub title: String,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Track {
pub id: i64,
pub album_id: i64,
pub title: String,
pub path: PathBuf,
pub duration: Option<i64>,
pub track_number: Option<i32>,
pub artist_name: Option<String>,
pub album_title: Option<String>,
}
pub struct Database {
conn: Connection,
}
#[allow(dead_code)]
impl Database {
pub fn open(_config: &crate::config::Config) -> SqlResult<Self> {
let proj_dirs = ProjectDirs::from("com", "rsplayer", "rsplayer").ok_or_else(|| {
rusqlite::Error::InvalidPath("Failed to get project directories".into())
})?;
let data_dir = if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from(xdg).join("rsplayer")
} else {
proj_dirs.data_dir().to_path_buf()
};
fs::create_dir_all(&data_dir).ok();
let db_path = data_dir.join("library.db");
let conn = Connection::open(&db_path)?;
let db = Database { conn };
db.init_schema()?;
Ok(db)
}
fn init_schema(&self) -> SqlResult<()> {
self.conn.execute(
"CREATE TABLE IF NOT EXISTS artists (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
)",
[],
)?;
self.conn.execute(
"CREATE TABLE IF NOT EXISTS albums (
id INTEGER PRIMARY KEY,
artist_id INTEGER NOT NULL,
title TEXT NOT NULL,
FOREIGN KEY (artist_id) REFERENCES artists(id),
UNIQUE(artist_id, title)
)",
[],
)?;
self.conn.execute(
"CREATE TABLE IF NOT EXISTS tracks (
id INTEGER PRIMARY KEY,
album_id INTEGER NOT NULL,
title TEXT NOT NULL,
path TEXT UNIQUE NOT NULL,
duration INTEGER,
track_number INTEGER,
last_scanned INTEGER,
FOREIGN KEY (album_id) REFERENCES albums(id)
)",
[],
)?;
Ok(())
}
pub fn scan_paths(&self, paths: &[PathBuf], force: bool) -> SqlResult<()> {
for path in paths {
self.scan_directory(path, force)?;
}
Ok(())
}
fn scan_directory(&self, dir: &Path, force: bool) -> SqlResult<()> {
if !dir.exists() {
return Ok(());
}
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
self.scan_recursive(dir, now, force)?;
self.cleanup_old_tracks(now)?;
Ok(())
}
fn scan_recursive(&self, dir: &Path, scan_time: i64, force: bool) -> SqlResult<()> {
let entries = fs::read_dir(dir).ok();
if let Some(entries) = entries {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
self.scan_recursive(&path, scan_time, force)?;
} else if is_audio_file(&path) {
self.process_audio_file(&path, scan_time, force)?;
}
}
}
Ok(())
}
fn process_audio_file(&self, path: &Path, scan_time: i64, force: bool) -> SqlResult<()> {
let path_str = path.to_string_lossy().to_string();
if !force {
let count: i64 = self
.conn
.query_row(
"SELECT COUNT(*) FROM tracks WHERE path = ? AND last_scanned = ?",
rusqlite::params![path_str, scan_time],
|row| row.get(0),
)
.unwrap_or(0);
if count > 0 {
return Ok(());
}
}
let metadata = extract_metadata(path);
let (artist_name, album_title, track_title, track_number, duration) = metadata;
let duration_i64 = duration.map(|d| d as i64);
let artist_id = self.get_or_create_artist(&artist_name)?;
let album_id = self.get_or_create_album(artist_id, &album_title)?;
self.conn.execute(
"INSERT OR REPLACE INTO tracks (album_id, title, path, duration, track_number, last_scanned)
VALUES (?, ?, ?, ?, ?, ?)",
rusqlite::params![album_id, track_title, path_str, duration_i64, track_number, scan_time],
)?;
Ok(())
}
fn get_or_create_artist(&self, name: &str) -> SqlResult<i64> {
self.conn
.execute("INSERT OR IGNORE INTO artists (name) VALUES (?)", [name])?;
self.conn
.query_row("SELECT id FROM artists WHERE name = ?", [name], |row| {
row.get(0)
})
}
fn get_or_create_album(&self, artist_id: i64, title: &str) -> SqlResult<i64> {
self.conn.execute(
"INSERT OR IGNORE INTO albums (artist_id, title) VALUES (?, ?)",
rusqlite::params![artist_id, title],
)?;
self.conn.query_row(
"SELECT id FROM albums WHERE artist_id = ? AND title = ?",
rusqlite::params![artist_id, title],
|row| row.get(0),
)
}
fn cleanup_old_tracks(&self, scan_time: i64) -> SqlResult<()> {
self.conn
.execute("DELETE FROM tracks WHERE last_scanned < ?", [scan_time])?;
Ok(())
}
pub fn search_tracks(&self, query: &str) -> SqlResult<Vec<Track>> {
let mut stmt = self.conn.prepare(
"SELECT t.id, t.album_id, t.title, t.path, t.duration, t.track_number, ar.name, a.title
FROM tracks t
JOIN albums a ON t.album_id = a.id
JOIN artists ar ON a.artist_id = ar.id
WHERE t.title LIKE ? OR a.title LIKE ? OR ar.name LIKE ?
ORDER BY ar.name, a.title, t.track_number",
)?;
let search = format!("%{}%", query);
let rows = stmt.query_map([&search, &search, &search], |row| {
Ok(Track {
id: row.get(0)?,
album_id: row.get(1)?,
title: row.get(2)?,
path: PathBuf::from(row.get::<_, String>(3)?),
duration: row.get(4)?,
track_number: row.get(5)?,
artist_name: Some(row.get(6)?),
album_title: Some(row.get(7)?),
})
})?;
rows.collect()
}
}
pub fn is_audio_file(path: &Path) -> bool {
path.extension()
.and_then(|ext| ext.to_str())
.map(|ext| {
matches!(
ext.to_lowercase().as_str(),
"mp3" | "flac" | "ogg" | "m4a" | "wav" | "opus"
)
})
.unwrap_or(false)
}
pub fn extract_metadata(path: &Path) -> (String, String, String, Option<i32>, Option<u64>) {
let file_stem = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("Unknown");
let parent = path
.parent()
.and_then(|p| p.file_name())
.and_then(|s| s.to_str())
.unwrap_or("Unknown");
let grandparent = path
.parent()
.and_then(|p| p.parent())
.and_then(|p| p.file_name())
.and_then(|s| s.to_str())
.unwrap_or("Unknown");
(
grandparent.to_string(),
parent.to_string(),
file_stem.to_string(),
None,
None,
)
}
|