Scan the filesystem
This commit is contained in:
parent
f0130d735e
commit
58b9d10441
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
server-dev:
|
||||||
|
cd server && cargo watch -x run
|
||||||
|
|
||||||
|
server-test:
|
||||||
|
cd server && cargo watch -x test
|
||||||
|
|
||||||
|
client-dev:
|
||||||
|
cd client && npm run watch
|
|
@ -24,6 +24,7 @@
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
<th> id </th>
|
||||||
<th> Track # </th>
|
<th> Track # </th>
|
||||||
<th> Title </th>
|
<th> Title </th>
|
||||||
<th> Artist </th>
|
<th> Artist </th>
|
||||||
|
|
|
@ -19,25 +19,40 @@ const getTracks = () => fetch("/api/v1/tracks").then((r) => r.json());
|
||||||
|
|
||||||
const formatTrack = (track: TrackInfo) => {
|
const formatTrack = (track: TrackInfo) => {
|
||||||
let row = document.createElement("tr");
|
let row = document.createElement("tr");
|
||||||
|
row.classList.add("track-list__row");
|
||||||
|
|
||||||
|
let track_id = document.createElement("td");
|
||||||
|
track_id.appendChild(document.createTextNode(track.id));
|
||||||
|
track_id.classList.add("track-list__cell");
|
||||||
|
|
||||||
let track_number = document.createElement("td");
|
let track_number = document.createElement("td");
|
||||||
track_number.appendChild(
|
track_number.appendChild(
|
||||||
document.createTextNode(track.track_number.toString())
|
document.createTextNode(track.track_number?.toString() || "")
|
||||||
);
|
);
|
||||||
|
track_number.classList.add("track-list__cell");
|
||||||
|
|
||||||
let name = document.createElement("td");
|
let name = document.createElement("td");
|
||||||
name.appendChild(document.createTextNode(track.name));
|
name.appendChild(document.createTextNode(track.name || ""));
|
||||||
|
name.classList.add("track-list__cell");
|
||||||
|
|
||||||
let album = document.createElement("td");
|
let album = document.createElement("td");
|
||||||
album.appendChild(document.createTextNode(track.album));
|
album.appendChild(document.createTextNode(track.album || ""));
|
||||||
|
album.classList.add("track-list__cell");
|
||||||
|
|
||||||
let artist = document.createElement("td");
|
let artist = document.createElement("td");
|
||||||
artist.appendChild(document.createTextNode(track.artist));
|
artist.appendChild(document.createTextNode(track.artist || ""));
|
||||||
|
artist.classList.add("track-list__cell");
|
||||||
|
|
||||||
|
let length = document.createElement("td");
|
||||||
|
artist.appendChild(document.createTextNode(""));
|
||||||
|
length.classList.add("track-list__cell");
|
||||||
|
|
||||||
|
row.appendChild(track_id);
|
||||||
row.appendChild(track_number);
|
row.appendChild(track_number);
|
||||||
row.appendChild(name);
|
row.appendChild(name);
|
||||||
row.appendChild(artist);
|
row.appendChild(artist);
|
||||||
row.appendChild(album);
|
row.appendChild(album);
|
||||||
|
row.appendChild(length);
|
||||||
return row;
|
return row;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -25,15 +25,18 @@ body {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row {
|
.track-list__row {
|
||||||
background-color: rgb(10, 10, 10);
|
background-color: rgb(10, 10, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row:nth-child(even) {
|
.track-list__row:nth-child(even) {
|
||||||
background-color: rgb(255, 255, 255);
|
background-color: rgb(255, 255, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row:nth-child(odd) {
|
.track-list__row:nth-child(odd) {
|
||||||
background-color: rgb(200, 200, 200);
|
background-color: rgb(200, 200, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.track-list__cell {
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
|
|
@ -12,55 +12,16 @@ use warp::{Filter, Reply};
|
||||||
use music_player::{
|
use music_player::{
|
||||||
audio::{TrackId, TrackInfo},
|
audio::{TrackId, TrackInfo},
|
||||||
core::Core,
|
core::Core,
|
||||||
database::MemoryIndex,
|
database::{MemoryIndex, MusicIndex},
|
||||||
|
music_scanner::FileScanner,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn tracks() -> Vec<TrackInfo> {
|
fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> {
|
||||||
vec![
|
match index.list_tracks() {
|
||||||
TrackInfo {
|
Flow::Ok(tracks) => tracks,
|
||||||
track_number: Some(1),
|
Flow::Err(err) => panic!("error: {}", err),
|
||||||
name: Some("Underground".to_owned()),
|
Flow::Fatal(err) => panic!("fatal: {}", err),
|
||||||
album: Some("Artemis".to_owned()),
|
}
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
id: TrackId::from(
|
|
||||||
"/mnt/music/Lindsey Stirling/Artemis/01 - Underground.ogg".to_owned(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
TrackInfo {
|
|
||||||
track_number: Some(2),
|
|
||||||
name: Some("Artemis".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
id: TrackId::from("/mnt/music/Lindsey Stirling/Artemis/02 - Artemis.ogg".to_owned()),
|
|
||||||
},
|
|
||||||
TrackInfo {
|
|
||||||
track_number: Some(3),
|
|
||||||
name: Some("Til the Light Goes Out".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
id: TrackId::from(
|
|
||||||
"/mnt/music/Lindsey Stirling/Artemis/03 - Til the Light Goes Out.ogg".to_owned(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
TrackInfo {
|
|
||||||
track_number: Some(4),
|
|
||||||
name: Some("Between Twilight".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
id: TrackId::from(
|
|
||||||
"/mnt/music/Lindsey Stirling/Artemis/04 - Between Twilight.ogg".to_owned(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
TrackInfo {
|
|
||||||
track_number: Some(5),
|
|
||||||
name: Some("Foreverglow".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
id: TrackId::from(
|
|
||||||
"/mnt/music/Lindsey Stirling/Artemis/05 - Foreverglow.ogg".to_owned(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Bundle {
|
enum Bundle {
|
||||||
|
@ -91,10 +52,21 @@ pub async fn main() {
|
||||||
let bundle_root = std::env::var("BUNDLE_ROOT")
|
let bundle_root = std::env::var("BUNDLE_ROOT")
|
||||||
.map(|b| PathBuf::from(b))
|
.map(|b| PathBuf::from(b))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let music_root = std::env::var("MUSIC_ROOT")
|
||||||
|
.map(|b| PathBuf::from(b))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
println!("config: {:?} {:?}", dev, bundle_root);
|
let index = Arc::new(MemoryIndex::new());
|
||||||
|
let scanner = FileScanner::new(vec![music_root.clone()]);
|
||||||
|
let core = match Core::new(index.clone(), scanner) {
|
||||||
|
Flow::Ok(core) => core,
|
||||||
|
Flow::Err(error) => panic!("error: {}", error),
|
||||||
|
Flow::Fatal(error) => panic!("fatal: {}", error),
|
||||||
|
};
|
||||||
|
|
||||||
let index = warp::path!().and(warp::get()).map({
|
println!("config: {:?} {:?} {:?}", dev, bundle_root, music_root);
|
||||||
|
|
||||||
|
let root = warp::path!().and(warp::get()).map({
|
||||||
let bundle_root = bundle_root.clone();
|
let bundle_root = bundle_root.clone();
|
||||||
move || {
|
move || {
|
||||||
warp::http::Response::builder()
|
warp::http::Response::builder()
|
||||||
|
@ -128,9 +100,11 @@ pub async fn main() {
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let track_list = warp::path!("api" / "v1" / "tracks")
|
let track_list = warp::path!("api" / "v1" / "tracks").and(warp::get()).map({
|
||||||
.and(warp::get())
|
let index = index.clone();
|
||||||
.map(|| warp::reply::json(&tracks()));
|
move || warp::reply::json(&tracks(&index))
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
|
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
|
@ -157,7 +131,7 @@ pub async fn main() {
|
||||||
.or(queue)
|
.or(queue)
|
||||||
.or(playing_status);
|
.or(playing_status);
|
||||||
*/
|
*/
|
||||||
let routes = index.or(app).or(styles).or(track_list);
|
let routes = root.or(app).or(styles).or(track_list);
|
||||||
let server = warp::serve(routes);
|
let server = warp::serve(routes);
|
||||||
server
|
server
|
||||||
.run(SocketAddr::new(
|
.run(SocketAddr::new(
|
||||||
|
|
|
@ -47,19 +47,21 @@ impl Core {
|
||||||
scanner: impl MusicScanner + 'static,
|
scanner: impl MusicScanner + 'static,
|
||||||
) -> Flow<Core, FatalError, Error> {
|
) -> Flow<Core, FatalError, Error> {
|
||||||
let (control_tx, control_rx) = channel::<ControlMsg>();
|
let (control_tx, control_rx) = channel::<ControlMsg>();
|
||||||
|
let db = db;
|
||||||
|
|
||||||
let (_track_handle, _track_rx) = {
|
let (_track_handle, _track_rx) = {
|
||||||
let (track_tx, track_rx) = channel();
|
let (track_tx, track_rx) = channel();
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
let track_handle = thread::spawn(move || {
|
let track_handle = thread::spawn(move || {
|
||||||
println!("tracker thread started");
|
|
||||||
let mut next_scan = Instant::now();
|
let mut next_scan = Instant::now();
|
||||||
loop {
|
loop {
|
||||||
if Instant::now() >= next_scan {
|
if Instant::now() >= next_scan {
|
||||||
let _ = track_tx.send(TrackMsg::UpdateInProgress);
|
let _ = track_tx.send(TrackMsg::UpdateInProgress);
|
||||||
for track in scanner.scan() {
|
for track in scanner.scan() {
|
||||||
println!("scanning {:?}", track);
|
match track {
|
||||||
db.add_track(track);
|
Ok(track) => db.add_track(track),
|
||||||
|
Err(_) => ok(()),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
let _ = track_tx.send(TrackMsg::UpdateComplete);
|
let _ = track_tx.send(TrackMsg::UpdateComplete);
|
||||||
next_scan = Instant::now() + scan_frequency();
|
next_scan = Instant::now() + scan_frequency();
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
audio::TrackInfo,
|
audio::{TrackId, TrackInfo},
|
||||||
core::{ControlMsg, TrackMsg},
|
core::{ControlMsg, TrackMsg},
|
||||||
database::MusicIndex,
|
database::MusicIndex,
|
||||||
FatalError,
|
FatalError,
|
||||||
};
|
};
|
||||||
use flow::{ok, return_error, return_fatal, Flow};
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fs::{DirEntry, ReadDir},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{
|
sync::{
|
||||||
mpsc::{Receiver, RecvTimeoutError, Sender},
|
mpsc::{Receiver, RecvTimeoutError, Sender},
|
||||||
|
@ -19,6 +19,8 @@ use thiserror::Error;
|
||||||
pub enum ScannerError {
|
pub enum ScannerError {
|
||||||
#[error("Cannot scan {0}")]
|
#[error("Cannot scan {0}")]
|
||||||
CannotScan(PathBuf),
|
CannotScan(PathBuf),
|
||||||
|
#[error("Not found {0}")]
|
||||||
|
NotFound(PathBuf),
|
||||||
#[error("IO error {0}")]
|
#[error("IO error {0}")]
|
||||||
IO(std::io::Error),
|
IO(std::io::Error),
|
||||||
}
|
}
|
||||||
|
@ -30,83 +32,101 @@ impl From<std::io::Error> for ScannerError {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait MusicScanner: Send {
|
pub trait MusicScanner: Send {
|
||||||
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = TrackInfo> + 'a>;
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
pub struct FileScanner {
|
pub struct FileScanner {
|
||||||
db: Arc<dyn MusicIndex>,
|
roots: Vec<PathBuf>,
|
||||||
tracker_tx: Sender<TrackMsg>,
|
|
||||||
music_directories: Vec<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileScanner {
|
impl FileScanner {
|
||||||
fn new(db: Arc<dyn MusicIndex>, roots: Vec<PathBuf>, tracker_tx: Sender<TrackMsg>) -> Self {
|
pub fn new(roots: Vec<PathBuf>) -> Self {
|
||||||
Self {
|
Self { roots }
|
||||||
db,
|
|
||||||
tracker_tx,
|
|
||||||
music_directories: roots,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
|
pub struct FileIterator {
|
||||||
while let Some(dir) = paths.pop() {
|
dirs: Vec<PathBuf>,
|
||||||
println!("scanning {:?}", dir);
|
file_iter: Option<ReadDir>,
|
||||||
return_error!(self.scan_dir_(&mut paths, dir));
|
}
|
||||||
}
|
|
||||||
ok(())
|
impl FileIterator {
|
||||||
|
fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> {
|
||||||
|
Ok(TrackInfo {
|
||||||
|
id: TrackId::from(path.to_str().unwrap().to_owned()),
|
||||||
|
album: None,
|
||||||
|
artist: None,
|
||||||
|
name: None,
|
||||||
|
track_number: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn scan_dir_(
|
enum EntryInfo {
|
||||||
&self,
|
Dir(PathBuf),
|
||||||
paths: &mut Vec<PathBuf>,
|
File(PathBuf),
|
||||||
dir: PathBuf,
|
}
|
||||||
) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
|
impl Iterator for FileIterator {
|
||||||
for entry in dir_iter {
|
type Item = Result<TrackInfo, ScannerError>;
|
||||||
match entry {
|
|
||||||
Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
Ok(entry) => {
|
fn process_entry(entry: DirEntry) -> Result<EntryInfo, ScannerError> {
|
||||||
let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
|
entry
|
||||||
println!("scan_file failed: {:?}", err);
|
.metadata()
|
||||||
ok::<(), FatalError, ScannerError>(())
|
.map_err(ScannerError::from)
|
||||||
}));
|
.map(|metadata| {
|
||||||
()
|
if metadata.is_dir() {
|
||||||
|
EntryInfo::Dir(entry.path())
|
||||||
|
} else {
|
||||||
|
EntryInfo::File(entry.path())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let next_file = match &mut self.file_iter {
|
||||||
|
Some(iter) => iter.next(),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
match next_file {
|
||||||
|
Some(Ok(entry)) => match process_entry(entry) {
|
||||||
|
Ok(EntryInfo::Dir(path)) => {
|
||||||
|
self.dirs.push(path);
|
||||||
|
self.next()
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Ok(EntryInfo::File(path)) => Some(self.scan_file(path)),
|
||||||
println!("scan_dir could not read path: ({:?})", err);
|
Err(err) => Some(Err(err)),
|
||||||
}
|
},
|
||||||
}
|
Some(Err(err)) => Some(Err(ScannerError::from(err))),
|
||||||
|
None => match self.dirs.pop() {
|
||||||
|
Some(dir) => match dir.read_dir() {
|
||||||
|
Ok(entry) => {
|
||||||
|
self.file_iter = Some(entry);
|
||||||
|
self.next()
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
if err.kind() == std::io::ErrorKind::NotFound {
|
||||||
|
Some(Err(ScannerError::NotFound(dir)))
|
||||||
|
} else {
|
||||||
|
Some(Err(ScannerError::from(err)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_file(&self, path: PathBuf) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MusicScanner for FileScanner {
|
impl MusicScanner for FileScanner {
|
||||||
fn scan<'a>(&'a self) -> Box<&'a dyn Iterator<Item = TrackInfo>> {
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
|
||||||
unimplemented!()
|
Box::new(FileIterator {
|
||||||
/*
|
dirs: self.roots.clone(),
|
||||||
loop {
|
file_iter: None,
|
||||||
match self.control_rx.recv_timeout(Duration::from_millis(100)) {
|
})
|
||||||
Ok(ControlMsg::Exit) => return,
|
|
||||||
Err(RecvTimeoutError::Timeout) => (),
|
|
||||||
Err(RecvTimeoutError::Disconnected) => return,
|
|
||||||
}
|
|
||||||
if Instant::now() >= self.next_scan {
|
|
||||||
for root in self.music_directories.iter() {
|
|
||||||
self.scan_dir(vec![root.clone()]);
|
|
||||||
}
|
|
||||||
self.next_scan = Instant::now() + scan_frequency();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod factories {
|
pub mod factories {
|
||||||
|
@ -162,8 +182,8 @@ pub mod factories {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MusicScanner for MockScanner {
|
impl MusicScanner for MockScanner {
|
||||||
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = TrackInfo> + 'a> {
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
|
||||||
Box::new(self.data.iter().map(|t| t.clone()))
|
Box::new(self.data.iter().map(|t| Ok(t.clone())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue