Compare commits

..

No commits in common. "58b9d10441ae7d39d4155f3ce4f52de79666001d" and "95853809b543040d5ab24be2dce447bc50d12e60" have entirely different histories.

10 changed files with 4084 additions and 261 deletions

View File

@ -1,9 +0,0 @@
server-dev:
cd server && cargo watch -x run
server-test:
cd server && cargo watch -x test
client-dev:
cd client && npm run watch

View File

@ -24,7 +24,6 @@
<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>
@ -34,7 +33,6 @@
</thead> </thead>
<tbody> <tbody>
<!--
<tr class="track-list__track-row"> <tr class="track-list__track-row">
<td> 1 </td> <td> 1 </td>
<td> Underground </td> <td> Underground </td>
@ -70,13 +68,12 @@
<td> Artemis </td> <td> Artemis </td>
<td> 3:58 </td> <td> 3:58 </td>
</tr> </tr>
-->
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>
<script src="./bundle.js" type="module"></script> <script src="./dist/main.js" type="module"></script>
</body> </body>
</html> </html>

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"build": "browserify src/main.ts -p [ tsify ] > dist/bundle.js && cp index.html styles.css dist", "build": "browserify src/main.ts -p [ tsify ] > dist/bundle.js",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"watch": "exa index.html styles.css src/* | entr -s 'npm run build'" "watch": "exa index.html styles.css src/* | entr -s 'npm run build'"
}, },
@ -18,6 +18,7 @@
"@types/lodash": "^4.14.191", "@types/lodash": "^4.14.191",
"babelify": "^10.0.0", "babelify": "^10.0.0",
"browserify": "^17.0.0", "browserify": "^17.0.0",
"live-server": "^1.2.2",
"tsify": "^5.0.4", "tsify": "^5.0.4",
"typescript": "^4.9.4", "typescript": "^4.9.4",
"watchify": "^4.0.0" "watchify": "^4.0.0"

View File

@ -1,13 +1,5 @@
import * as _ from "lodash"; import * as _ from "lodash";
interface TrackInfo {
id: string;
track_number?: number;
name?: string;
album?: string;
artist?: string;
}
const replaceTitle = () => { const replaceTitle = () => {
const title = document.querySelector(".js-title"); const title = document.querySelector(".js-title");
if (title && title.innerHTML) { if (title && title.innerHTML) {
@ -15,57 +7,21 @@ const replaceTitle = () => {
} }
}; };
const getTracks = () => fetch("/api/v1/tracks").then((r) => r.json()); /*
const checkWeatherService = () => {
const formatTrack = (track: TrackInfo) => { fetch("https://api.weather.gov/")
let row = document.createElement("tr"); .then((r) => r.json())
row.classList.add("track-list__row"); .then((js) => {
const weather = document.querySelector('.js-weather');
let track_id = document.createElement("td"); weather.innerHTML = js.status;
track_id.appendChild(document.createTextNode(track.id)); });
track_id.classList.add("track-list__cell");
let track_number = document.createElement("td");
track_number.appendChild(
document.createTextNode(track.track_number?.toString() || "")
);
track_number.classList.add("track-list__cell");
let name = document.createElement("td");
name.appendChild(document.createTextNode(track.name || ""));
name.classList.add("track-list__cell");
let album = document.createElement("td");
album.appendChild(document.createTextNode(track.album || ""));
album.classList.add("track-list__cell");
let artist = document.createElement("td");
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(name);
row.appendChild(artist);
row.appendChild(album);
row.appendChild(length);
return row;
};
const updateTrackList = (tracks: TrackInfo[]) => {
const track_list = document.querySelector(".track-list__tracks tbody");
if (track_list) {
let track_formats = _.map(tracks, formatTrack);
_.map(track_formats, (trackinfo) => track_list.appendChild(trackinfo));
} }
}; */
const run = () => { const run = () => {
getTracks().then((tracks) => updateTrackList(tracks)); replaceTitle();
console.log(_.map([4, 8], (x) => x * x));
// checkWeatherService();
}; };
run(); run();

View File

@ -25,18 +25,15 @@ body {
list-style: none; list-style: none;
} }
.track-list__row { .track-list__track-row {
background-color: rgb(10, 10, 10); background-color: rgb(10, 10, 10);
} }
.track-list__row:nth-child(even) { .track-list__track-row:nth-child(even) {
background-color: rgb(255, 255, 255); background-color: rgb(255, 255, 255);
} }
.track-list__row:nth-child(odd) { .track-list__track-row:nth-child(odd) {
background-color: rgb(200, 200, 200); background-color: rgb(200, 200, 200);
} }
.track-list__cell {
padding: 8px;
}

View File

@ -6,7 +6,6 @@
"rootDir": "src", "rootDir": "src",
"outDir": "./dist", "outDir": "./dist",
"lib": ["es2016", "DOM"], "lib": ["es2016", "DOM"],
"sourceMap": true, "sourceMap": true
"strict": true
} }
} }

View File

@ -1,95 +1,74 @@
use flow::Flow; use flow::Flow;
use std::{ use std::{io::stdin, path::PathBuf, sync::Arc, thread, time::Duration};
io::stdin, // use warp::Filter;
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::Arc,
thread,
time::Duration,
};
use warp::{Filter, Reply};
use music_player::{ use music_player::{core::Core, database::MemoryIndex};
audio::{TrackId, TrackInfo},
core::Core,
database::{MemoryIndex, MusicIndex},
music_scanner::FileScanner,
};
fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> { /*
match index.list_tracks() { fn tracks() -> Vec<Track> {
Flow::Ok(tracks) => tracks, vec![
Flow::Err(err) => panic!("error: {}", err), Track {
Flow::Fatal(err) => panic!("fatal: {}", err), track_number: Some(1),
} name: Some("Underground".to_owned()),
} album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
enum Bundle { path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/01 - Underground.ogg"),
Index, },
App, Track {
Styles, track_number: Some(2),
} name: Some("Artemis".to_owned()),
album: Some("Artemis".to_owned()),
impl Bundle { artist: Some("Lindsey Stirling".to_owned()),
fn read(self, root: PathBuf) -> String { path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/02 - Artemis.ogg"),
let mut path = root; },
match self { Track {
Bundle::Index => path.push(PathBuf::from("index.html")), track_number: Some(3),
Bundle::App => path.push(PathBuf::from("bundle.js")), name: Some("Til the Light Goes Out".to_owned()),
Bundle::Styles => path.push(PathBuf::from("styles.css")), album: Some("Artemis".to_owned()),
}; artist: Some("Lindsey Stirling".to_owned()),
println!("path: {:?}", path); path: PathBuf::from(
std::fs::read_to_string(path).expect("to find the file") "/mnt/music/Lindsey Stirling/Artemis/03 - Til the Light Goes Out.ogg",
} ),
},
Track {
track_number: Some(4),
name: Some("Between Twilight".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/04 - Between Twilight.ogg"),
},
Track {
track_number: Some(5),
name: Some("Foreverglow".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/05 - Foreverglow.ogg"),
},
]
} }
*/
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
let dev = std::env::var("DEV") /*
.ok() match Core::new(Arc::new(MemoryIndex::new())) {
.and_then(|v| v.parse::<bool>().ok()) Flow::Ok(core) => {
.unwrap_or(false); let mut buf = String::new();
let bundle_root = std::env::var("BUNDLE_ROOT") let _ = stdin().read_line(&mut buf).unwrap();
.map(|b| PathBuf::from(b)) core.exit();
.unwrap();
let music_root = std::env::var("MUSIC_ROOT")
.map(|b| PathBuf::from(b))
.unwrap();
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),
};
println!("config: {:?} {:?} {:?}", dev, bundle_root, music_root);
let root = warp::path!().and(warp::get()).map({
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/html")
.body(Bundle::Index.read(bundle_root.clone()))
} }
}); Flow::Err(err) => println!("non-fatal error: {:?}", err),
let app = warp::path!("bundle.js").and(warp::get()).map({ Flow::Fatal(err) => println!("fatal error: {:?}", err),
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/javascript")
.body(Bundle::App.read(bundle_root.clone()))
} }
}); */
let styles = warp::path!("styles.css").and(warp::get()).map({
let bundle_root = bundle_root.clone(); /*
move || { let connection = Connection::new_session().expect("to connect to dbus");
warp::http::Response::builder()
.header("content-type", "text/css") for player in list_players(connection) {
.body(Bundle::Styles.read(bundle_root.clone())) println!("player found: {}", player.identity());
} }
}); */
/* /*
let devices = warp::path!("api" / "v1" / "devices") let devices = warp::path!("api" / "v1" / "devices")
@ -98,14 +77,10 @@ pub async fn main() {
let conn = Connection::new_session().expect("to connect to dbus"); let conn = Connection::new_session().expect("to connect to dbus");
warp::reply::json(&list_devices(conn)) warp::reply::json(&list_devices(conn))
}); });
*/
let track_list = warp::path!("api" / "v1" / "tracks").and(warp::get()).map({ let track_list = warp::path!("api" / "v1" / "tracks")
let index = index.clone(); .and(warp::get())
move || warp::reply::json(&tracks(&index)) .map(|| warp::reply::json(&tracks()));
});
/*
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())
.map(|_artist: String| warp::reply::json(&tracks())); .map(|_artist: String| warp::reply::json(&tracks()));
@ -130,8 +105,6 @@ pub async fn main() {
.or(tracks_for_artist) .or(tracks_for_artist)
.or(queue) .or(queue)
.or(playing_status); .or(playing_status);
*/
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(
@ -139,4 +112,5 @@ pub async fn main() {
8002, 8002,
)) ))
.await; .await;
*/
} }

View File

@ -47,21 +47,19 @@ 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() {
match track { println!("scanning {:?}", track);
Ok(track) => db.add_track(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();

View File

@ -1,11 +1,11 @@
use crate::{ use crate::{
audio::{TrackId, TrackInfo}, audio::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,8 +19,6 @@ 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),
} }
@ -32,101 +30,83 @@ impl From<std::io::Error> for ScannerError {
} }
pub trait MusicScanner: Send { pub trait MusicScanner: Send {
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a>; fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = TrackInfo> + 'a>;
} }
/*
pub struct FileScanner { pub struct FileScanner {
roots: Vec<PathBuf>, db: Arc<dyn MusicIndex>,
tracker_tx: Sender<TrackMsg>,
music_directories: Vec<PathBuf>,
} }
impl FileScanner { impl FileScanner {
pub fn new(roots: Vec<PathBuf>) -> Self { fn new(db: Arc<dyn MusicIndex>, roots: Vec<PathBuf>, tracker_tx: Sender<TrackMsg>) -> Self {
Self { roots } Self {
db,
tracker_tx,
music_directories: roots,
} }
} }
pub struct FileIterator { fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
dirs: Vec<PathBuf>, while let Some(dir) = paths.pop() {
file_iter: Option<ReadDir>, println!("scanning {:?}", dir);
return_error!(self.scan_dir_(&mut paths, dir));
}
ok(())
} }
impl FileIterator { fn scan_dir_(
fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> { &self,
Ok(TrackInfo { paths: &mut Vec<PathBuf>,
id: TrackId::from(path.to_str().unwrap().to_owned()), dir: PathBuf,
album: None, ) -> Flow<(), FatalError, ScannerError> {
artist: None, let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
name: None, for entry in dir_iter {
track_number: None, match entry {
}) Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
}
}
enum EntryInfo {
Dir(PathBuf),
File(PathBuf),
}
impl Iterator for FileIterator {
type Item = Result<TrackInfo, ScannerError>;
fn next(&mut self) -> Option<Self::Item> {
fn process_entry(entry: DirEntry) -> Result<EntryInfo, ScannerError> {
entry
.metadata()
.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()
}
Ok(EntryInfo::File(path)) => Some(self.scan_file(path)),
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) => { Ok(entry) => {
self.file_iter = Some(entry); let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
self.next() println!("scan_file failed: {:?}", err);
ok::<(), FatalError, ScannerError>(())
}));
()
} }
Err(err) => { Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound { println!("scan_dir could not read path: ({:?})", err);
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<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> { fn scan<'a>(&'a self) -> Box<&'a dyn Iterator<Item = TrackInfo>> {
Box::new(FileIterator { unimplemented!()
dirs: self.roots.clone(), /*
file_iter: None, loop {
}) 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 {
@ -182,8 +162,8 @@ pub mod factories {
} }
impl MusicScanner for MockScanner { impl MusicScanner for MockScanner {
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> { fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = TrackInfo> + 'a> {
Box::new(self.data.iter().map(|t| Ok(t.clone()))) Box::new(self.data.iter().map(|t| t.clone()))
} }
} }
} }