Skip to content

Commit

Permalink
SQLite WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Oct 31, 2024
1 parent dc02864 commit c5a0600
Show file tree
Hide file tree
Showing 8 changed files with 982 additions and 228 deletions.
715 changes: 709 additions & 6 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ lofty = "0.21.0"
m3u = "1.0.0"
memoize = "0.4.2"
nosleep = "0.2.1"
ormlite = { version = "0.22.2", features = ["sqlite"] }
pathdiff = "0.2.2"
rayon = "1.10.0"
serde = { version = "1.0.211", features = ["derive"] }
serde_json = "1.0.132"
strum = { version = "0.26.2", features = ["derive"] }
tokio = { version = "1.41.0", features = ["macros"] }
thiserror = "1.0.64"
ts-rs = "10.0.0"
ts-rs = {version = "10.0.0", features = ["serde-compat", "serde-json-impl"] }
uuid = { version = "1.11.0", features = ["v3", "v4", "fast-rng"] }
walkdir = "2.5.0"

[target.'cfg(target_os = "linux")'.dependencies]
dbus = "0.9.7"
26 changes: 26 additions & 0 deletions src-tauri/migrations/01-initial-setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Track table
CREATE TABLE track (
_id TEXT PRIMARY KEY NOT NULL,
path TEXT NOT NULL UNIQUE, -- Path as a string and unique
title TEXT NOT NULL,
album TEXT NOT NULL,
artists JSON NOT NULL, -- Array of strings
genres JSON NOT NULL, -- Array of strings
year INTEGER,
duration INTEGER NOT NULL,
track_no INTEGER,
track_of INTEGER,
disk_no INTEGER,
disk_of INTEGER
);

-- Index for the path column in Track
CREATE INDEX index_track_path ON Track (path);

-- Playlist table
CREATE TABLE playlist (
_id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
tracks JSON NOT NULL DEFAULT '[]', -- Array of track IDs
import_path TEXT UNIQUE -- Path of the playlist file, unique if it exists
);
6 changes: 6 additions & 0 deletions src-tauri/src/libs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub enum MuseeksError {
#[error(transparent)]
LocalDatabase(#[from] bonsaidb::local::Error),

#[error(transparent)]
ORMLite(#[from] ormlite::Error),

#[error(transparent)]
ORMLiteSqlx(#[from] ormlite::SqlxError),

#[error(transparent)]
NoSleep(#[from] nosleep::Error),

Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/libs/file_associations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use tauri::{AppHandle, Emitter, Manager};
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};

use crate::libs::track::get_track_from_file;
use crate::libs::track::{get_track_from_file, get_track_from_insert_track};
use crate::libs::utils::is_file_valid;
use crate::plugins::database::SUPPORTED_TRACKS_EXTENSIONS;

Expand Down Expand Up @@ -83,6 +83,7 @@ fn handle_file_associations(app_handle: AppHandle, mut files: Vec<PathBuf>) {
.par_iter()
.map(|path| get_track_from_file(&path))
.flatten()
.map(get_track_from_insert_track)
.collect::<Vec<_>>();

let window = app_handle.get_webview_window("main");
Expand Down
17 changes: 8 additions & 9 deletions src-tauri/src/libs/playlist.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::path::PathBuf;

use bonsaidb::core::schema::Collection;
use ormlite::model::Model;
use serde::{Deserialize, Serialize};
use ts_rs::TS;

Expand All @@ -9,13 +7,14 @@ use ts_rs::TS;
* represent a playlist, that has a name and a list of tracks
* -------------------------------------------------------------------------- */

#[derive(Debug, Clone, Serialize, Deserialize, Collection, TS)]
#[collection(name = "playlists", primary_key = String)]
#[derive(Debug, Clone, Serialize, Deserialize, Model, TS)]
#[ormlite(insert = "InsertPlaylist")]
#[ts(export, export_to = "../../src/generated/typings/index.ts")]
pub struct Playlist {
#[natural_id]
pub _id: String,
#[ormlite(primary_key)]
pub id: String,
pub name: String,
pub tracks: Vec<String>, // vector of IDs
pub import_path: Option<PathBuf>, // the path of the file on disk (not set for playlists created in app)
#[ormlite(json)]
pub tracks: Vec<String>, // vector of IDs
pub import_path: Option<String>, // the path of the file on disk (not set for playlists created in app), ideally, a PathBuf
}
45 changes: 31 additions & 14 deletions src-tauri/src/libs/track.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::PathBuf;

use bonsaidb::core::schema::Collection;
use lofty::file::{AudioFile, TaggedFileExt};
use lofty::tag::{Accessor, ItemKey};
use log::{error, warn};
use ormlite::model::Model;
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use uuid::Uuid;
Expand All @@ -12,16 +12,17 @@ use uuid::Uuid;
* Track
* represent a single track, id and path should be unique
*/
#[derive(Debug, Clone, Serialize, Deserialize, Collection, TS)]
#[collection(name="tracks", primary_key = String)]
#[derive(Debug, Clone, Serialize, Deserialize, Model, TS)]
#[ormlite(insert = "InsertTrack")]
#[ts(export, export_to = "../../src/generated/typings/index.ts")]
pub struct Track {
#[natural_id]
pub _id: String,
pub path: PathBuf, // must be unique
pub id: String,
pub path: String, // must be unique, ideally, a PathBuf
pub title: String,
pub album: String,
#[ormlite(json)]
pub artists: Vec<String>,
#[ormlite(json)]
pub genres: Vec<String>,
pub year: Option<u32>,
pub duration: u32,
Expand All @@ -35,7 +36,7 @@ pub struct Track {
* Generate a Track struct from a Path, or nothing if it is not a valid audio
* file
*/
pub fn get_track_from_file(path: &PathBuf) -> Option<Track> {
pub fn get_track_from_file(path: &PathBuf) -> Option<InsertTrack> {
match lofty::read_from_path(&path) {
Ok(tagged_file) => {
let tag = tagged_file.primary_tag()?;
Expand All @@ -57,13 +58,8 @@ pub fn get_track_from_file(path: &PathBuf) -> Option<Track> {
artists = vec!["Unknown Artist".into()];
}

let Some(id) = get_track_id_for_path(path) else {
return None;
};

Some(Track {
_id: id,
path: path.to_owned(),
Some(InsertTrack {
path: path.to_string_lossy().into_owned(),
title: tag
.get_string(&ItemKey::TrackTitle)
.unwrap_or("Unknown")
Expand Down Expand Up @@ -92,6 +88,27 @@ pub fn get_track_from_file(path: &PathBuf) -> Option<Track> {
}
}

/**
* Generate a fake ID for inserted track, typically used when manipulating tracks
* that may never be stored in the database.
*/
pub fn get_track_from_insert_track(insert_track: InsertTrack) -> Track {
return Track {
id: get_track_id_for_path(&PathBuf::from(&insert_track.path)).unwrap(),
path: insert_track.path,
title: insert_track.title,
album: insert_track.album,
artists: insert_track.artists,
genres: insert_track.genres,
year: insert_track.year,
duration: insert_track.duration,
track_no: insert_track.track_no,
track_of: insert_track.track_of,
disk_no: insert_track.disk_no,
disk_of: insert_track.disk_of,
};
}

/**
* Generate an ID for a track based on its location.
*
Expand Down
Loading

0 comments on commit c5a0600

Please sign in to comment.