Start differentiating between mime types of files and scanning mpeg files

This commit is contained in:
Savanni D'Gerinel 2023-03-01 23:54:23 -05:00
parent 226329ab6d
commit 81779c9fc2
11 changed files with 178 additions and 237 deletions

View File

@ -1,6 +1,7 @@
export interface TrackInfo { export interface TrackInfo {
id: string; id: string;
track_number?: number; track_number?: number;
duration?: number;
name?: string; name?: string;
album?: string; album?: string;
artist?: string; artist?: string;

View File

@ -35,7 +35,7 @@ export class TrackName extends HTMLElement {
export class TrackCard extends HTMLElement { export class TrackCard extends HTMLElement {
static get observedAttributes() { static get observedAttributes() {
return ["id", "trackNumber", "name", "album", "artist"]; return ["id", "trackNumber", "name", "album", "artist", "duration"];
} }
constructor() { constructor() {
@ -124,10 +124,14 @@ export class TrackCard extends HTMLElement {
trackName.name = this["name"]; trackName.name = this["name"];
container.appendChild(trackName); container.appendChild(trackName);
} }
this["length"] && container.appendChild(document.createTextNode("1:23")); if (this["length"]) {
this["album"] && container.appendChild(document.createTextNode(this["length"]));
container.appendChild(document.createTextNode("Shatter Me")); }
this["artist"] && if (this["album"]) {
container.appendChild(document.createTextNode("Lindsey Stirling")); container.appendChild(document.createTextNode(this["album"]));
}
if (this["artist"]) {
container.appendChild(document.createTextNode(this["artist"]));
}
} }
} }

View File

@ -1,6 +1,6 @@
import * as _ from "lodash"; import * as _ from "lodash";
import { TrackInfo, getTracks } from "./client"; import { TrackInfo, getTracks } from "./client";
import { TrackName, TrackCard } from "./blocks/track"; import { TrackName, TrackCard } from "./components/track";
window.customElements.define("track-name", TrackName); window.customElements.define("track-name", TrackName);
window.customElements.define("track-card", TrackCard); window.customElements.define("track-card", TrackCard);
@ -12,19 +12,15 @@ declare global {
} }
} }
const replaceTitle = () => {
const title = document.querySelector(".js-title");
if (title && title.innerHTML) {
title.innerHTML = "Music Player Paused";
}
};
const updateTrackList = (tracks: TrackInfo[]) => { const updateTrackList = (tracks: TrackInfo[]) => {
const track_list = document.querySelector(".track-list__tracks"); const track_list = document.querySelector(".track-list__tracks");
if (track_list) { if (track_list) {
let track_formats = _.map(tracks, (info) => { let track_formats = _.map(tracks, (info) => {
let card: TrackCard = document.createElement("track-card"); let card: TrackCard = document.createElement("track-card");
card.name = info.name || null; card.name = info.name || null;
card.album = info.album || null;
card.artist = info.artist || null;
card.length = (info.duration && `${info.duration}`) || null;
return card; return card;
}); });
_.map(track_formats, (trackCard) => { _.map(track_formats, (trackCard) => {

View File

@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]] [[package]]
name = "ahash" name = "ahash"
version = "0.7.6" version = "0.7.6"
@ -77,6 +83,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.6" version = "0.1.6"
@ -87,63 +102,6 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "darling"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote 1.0.23",
"strsim",
"syn 1.0.107",
]
[[package]]
name = "darling_macro"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835"
dependencies = [
"darling_core",
"quote 1.0.23",
"syn 1.0.107",
]
[[package]]
name = "dbus"
version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bb21987b9fb1613058ba3843121dd18b163b254d8a6e797e144cbac14d96d1b"
dependencies = [
"libc",
"libdbus-sys",
"winapi",
]
[[package]]
name = "derive_is_enum_variant"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0ac8859845146979953797f03cc5b282fb4396891807cdb3d04929a88418197"
dependencies = [
"heck",
"quote 0.3.15",
"syn 0.11.11",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.6" version = "0.10.6"
@ -154,17 +112,6 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "enum-kinds"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462"
dependencies = [
"proc-macro2",
"quote 1.0.23",
"syn 1.0.107",
]
[[package]] [[package]]
name = "fallible-iterator" name = "fallible-iterator"
version = "0.2.0" version = "0.2.0"
@ -186,6 +133,16 @@ dependencies = [
"instant", "instant",
] ]
[[package]]
name = "flate2"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]] [[package]]
name = "flow" name = "flow"
version = "0.1.0" version = "0.1.0"
@ -205,27 +162,6 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "from_variants"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd6e0c314d32a210f213d2de7fc205a415498c32477022bffdb8927a95f44493"
dependencies = [
"from_variants_impl",
]
[[package]]
name = "from_variants_impl"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "243da8644e4e4ac28a22f7f30187f9d3f17eafbc219884ff8935edb5ebfe621c"
dependencies = [
"darling",
"proc-macro2",
"quote 1.0.23",
"syn 1.0.107",
]
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.25" version = "0.3.25"
@ -351,15 +287,6 @@ dependencies = [
"http", "http",
] ]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -428,10 +355,15 @@ dependencies = [
] ]
[[package]] [[package]]
name = "ident_case" name = "id3"
version = "1.0.1" version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" checksum = "19d7a833474b30425eb64132d1f9b727b4e39537418bcc3288497c8d2f5c8948"
dependencies = [
"bitflags",
"byteorder",
"flate2",
]
[[package]] [[package]]
name = "idna" name = "idna"
@ -474,15 +406,6 @@ version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "libdbus-sys"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2264f9d90a9b4e60a2dc722ad899ea0374f03c2e96e755fe22a8f551d4d5fb3c"
dependencies = [
"pkg-config",
]
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.25.2" version = "0.25.2"
@ -534,6 +457,15 @@ dependencies = [
"unicase", "unicase",
] ]
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.8.5" version = "0.8.5"
@ -546,19 +478,6 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "mpris"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e3d377fd27d9d5c7145341cd3affcb83839c24c73e7460488b3ae0a3f9c5166"
dependencies = [
"dbus",
"derive_is_enum_variant",
"enum-kinds",
"from_variants",
"thiserror",
]
[[package]] [[package]]
name = "multipart" name = "multipart"
version = "0.18.0" version = "0.18.0"
@ -581,10 +500,10 @@ dependencies = [
name = "music-player" name = "music-player"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dbus",
"flow", "flow",
"id3",
"mime",
"mime_guess", "mime_guess",
"mpris",
"rusqlite", "rusqlite",
"serde", "serde",
"thiserror", "thiserror",
@ -655,8 +574,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.23", "quote",
"syn 1.0.107", "syn",
] ]
[[package]] [[package]]
@ -698,12 +617,6 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.23" version = "1.0.23"
@ -824,8 +737,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.23", "quote",
"syn 1.0.107", "syn",
] ]
[[package]] [[package]]
@ -907,23 +820,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
dependencies = [
"quote 0.3.15",
"synom",
"unicode-xid",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.107" version = "1.0.107"
@ -931,19 +827,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.23", "quote",
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "synom"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
dependencies = [
"unicode-xid",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.3.0" version = "3.3.0"
@ -974,8 +861,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.23", "quote",
"syn 1.0.107", "syn",
] ]
[[package]] [[package]]
@ -1020,8 +907,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.23", "quote",
"syn 1.0.107", "syn",
] ]
[[package]] [[package]]
@ -1158,18 +1045,6 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a"
[[package]]
name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"

View File

@ -6,10 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dbus = { version = "0.9.7" }
flow = { path = "../../flow" } flow = { path = "../../flow" }
mime_guess = "2.0.4" id3 = { version = "1.6" }
mpris = { version = "2.0" } mime_guess = { version = "2.0" }
mime = { version = "0.3" }
rusqlite = { version = "0.28" } rusqlite = { version = "0.28" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
thiserror = { version = "1.0" } thiserror = { version = "1.0" }

View File

@ -7,10 +7,10 @@ use std::{
use warp::Filter; use warp::Filter;
use music_player::{ use music_player::{
audio::TrackInfo,
core::Core, core::Core,
database::{MemoryIndex, MusicIndex}, database::{MemoryIndex, MusicIndex},
music_scanner::FileScanner, media::TrackInfo,
scanner::FileScanner,
}; };
fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> { fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> {

View File

@ -1,6 +1,4 @@
use crate::{ use crate::{database::MusicIndex, media::TrackInfo, scanner::MusicScanner, Error, FatalError};
audio::TrackInfo, database::MusicIndex, music_scanner::MusicScanner, Error, FatalError,
};
use flow::{ok, Flow}; use flow::{ok, Flow};
use std::{ use std::{
sync::{ sync::{
@ -108,7 +106,7 @@ impl Core {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::{audio::TrackId, database::MemoryIndex, music_scanner::factories::MockScanner}; use crate::{database::MemoryIndex, media::TrackId, scanner::factories::MockScanner};
use std::collections::HashSet; use std::collections::HashSet;
fn with_example_index<F>(f: F) fn with_example_index<F>(f: F)

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
audio::{TrackId, TrackInfo}, media::{TrackId, TrackInfo},
FatalError, FatalError,
}; };
use flow::{error, ok, Flow}; use flow::{error, ok, Flow};
@ -129,6 +129,8 @@ mod test {
name: None, name: None,
album: None, album: None,
artist: None, artist: None,
duration: None,
filetype: "text/plain".parse::<mime::Mime>().unwrap(),
}; };
index.add_track(info.clone()); index.add_track(info.clone());

View File

@ -1,7 +1,7 @@
pub mod audio;
pub mod core; pub mod core;
pub mod database; pub mod database;
pub mod music_scanner; pub mod media;
pub mod scanner;
use database::DatabaseError; use database::DatabaseError;
use thiserror::Error; use thiserror::Error;

View File

@ -10,7 +10,7 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/ */
use serde::Serialize; use serde::{Serialize, Serializer};
use std::time::Duration; use std::time::Duration;
use thiserror::Error; use thiserror::Error;
@ -46,28 +46,10 @@ pub enum AudioError {
#[error("Play was ordered, but nothing is in the queue")] #[error("Play was ordered, but nothing is in the queue")]
NothingInQueue, NothingInQueue,
#[error("Unknown dbus error")]
DbusError(dbus::Error),
#[error("Unknown problem with mpris")]
MprisError(mpris::DBusError),
#[error("url parse error {0}")] #[error("url parse error {0}")]
UrlError(url::ParseError), UrlError(url::ParseError),
} }
impl From<dbus::Error> for AudioError {
fn from(err: dbus::Error) -> Self {
Self::DbusError(err)
}
}
impl From<mpris::DBusError> for AudioError {
fn from(err: mpris::DBusError) -> Self {
Self::MprisError(err)
}
}
impl From<url::ParseError> for AudioError { impl From<url::ParseError> for AudioError {
fn from(err: url::ParseError) -> Self { fn from(err: url::ParseError) -> Self {
Self::UrlError(err) Self::UrlError(err)
@ -102,6 +84,16 @@ pub struct TrackInfo {
pub name: Option<String>, pub name: Option<String>,
pub album: Option<String>, pub album: Option<String>,
pub artist: Option<String>, pub artist: Option<String>,
pub duration: Option<u32>,
#[serde(serialize_with = "serialize_mime")]
pub filetype: mime::Mime,
}
fn serialize_mime<S>(val: &mime::Mime, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(val.essence_str())
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]

View File

@ -1,16 +1,19 @@
use crate::audio::{TrackId, TrackInfo}; use crate::media::{TrackId, TrackInfo};
use id3::{Tag, TagLike};
use std::{ use std::{
fs::{DirEntry, ReadDir}, fs::{DirEntry, ReadDir},
path::PathBuf, path::{Path, PathBuf},
}; };
use thiserror::Error; use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ScannerError { pub enum ScannerError {
#[error("Cannot scan {0}")] #[error("Cannot scan file")]
CannotScan(PathBuf), CannotScan,
#[error("Not found {0}")] #[error("File not found")]
NotFound(PathBuf), FileNotFound,
#[error("Tag not found")]
TagNotFound,
#[error("IO error {0}")] #[error("IO error {0}")]
IO(std::io::Error), IO(std::io::Error),
} }
@ -21,6 +24,19 @@ impl From<std::io::Error> for ScannerError {
} }
} }
impl From<id3::Error> for ScannerError {
fn from(err: id3::Error) -> ScannerError {
match err.kind {
id3::ErrorKind::Io(err) => ScannerError::IO(err),
id3::ErrorKind::StringDecoding(_) => ScannerError::CannotScan,
id3::ErrorKind::NoTag => ScannerError::TagNotFound,
id3::ErrorKind::Parsing => ScannerError::CannotScan,
id3::ErrorKind::InvalidInput => ScannerError::CannotScan,
id3::ErrorKind::UnsupportedFeature => ScannerError::CannotScan,
}
}
}
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 = Result<TrackInfo, ScannerError>> + 'a>;
} }
@ -42,15 +58,62 @@ pub struct FileIterator {
impl FileIterator { impl FileIterator {
fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> { fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> {
println!(
"[{:?}] {}",
mime_guess::from_path(path.clone()).first(),
path.to_str().unwrap()
);
let mimetype = mime_guess::from_path(path.clone())
.first()
.ok_or(ScannerError::CannotScan)?;
match (mimetype.type_(), mimetype.subtype().as_str()) {
(mime::AUDIO, "mpeg") => TrackInfo::scan_id3(path, mimetype),
/*
(mime::AUDIO, "ogg") => Ok(TrackInfo {
id: TrackId::from(path.to_str().unwrap().to_owned()),
album: None,
artist: None,
name: path
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_owned()),
track_number: None,
filetype: mimetype,
}),
(mime::AUDIO, "flac") => Ok(TrackInfo {
id: TrackId::from(path.to_str().unwrap().to_owned()),
album: None,
artist: None,
name: path
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_owned()),
track_number: None,
filetype: mimetype,
}),
*/
_ => Err(ScannerError::CannotScan),
}
}
}
impl TrackInfo {
fn scan_id3(path: PathBuf, mimetype: mime::Mime) -> Result<TrackInfo, ScannerError> {
let tags = Tag::read_from_path(path.clone()).map_err(ScannerError::from)?;
Ok(TrackInfo { Ok(TrackInfo {
id: TrackId::from(path.to_str().unwrap().to_owned()), id: TrackId::from(path.to_str().unwrap().to_owned()),
album: None, album: tags.album().map(|s| s.to_owned()),
artist: None, artist: tags.artist().map(|s| s.to_owned()),
name: path name: tags.title().map(|s| s.to_owned()).or_else(|| {
.file_stem() path.file_stem()
.and_then(|s| s.to_str()) .and_then(|s| s.to_str())
.map(|s| s.to_owned()), .map(|s| s.to_owned())
}),
duration: tags.duration(),
track_number: None, track_number: None,
filetype: mimetype,
}) })
} }
} }
@ -100,7 +163,7 @@ impl Iterator for FileIterator {
} }
Err(err) => { Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound { if err.kind() == std::io::ErrorKind::NotFound {
Some(Err(ScannerError::NotFound(dir))) Some(Err(ScannerError::FileNotFound))
} else { } else {
Some(Err(ScannerError::from(err))) Some(Err(ScannerError::from(err)))
} }
@ -124,7 +187,7 @@ impl MusicScanner for FileScanner {
#[cfg(test)] #[cfg(test)]
pub mod factories { pub mod factories {
use super::*; use super::*;
use crate::audio::TrackId; use crate::media::TrackId;
pub struct MockScanner { pub struct MockScanner {
data: Vec<TrackInfo>, data: Vec<TrackInfo>,
@ -140,6 +203,8 @@ pub mod factories {
name: Some("Track 1".to_owned()), name: Some("Track 1".to_owned()),
album: Some("Savanni's Demo".to_owned()), album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()), artist: Some("Savanni".to_owned()),
duration: Some(15),
filetype: "audio/mpeg".parse::<mime::Mime>().unwrap(),
}, },
TrackInfo { TrackInfo {
id: TrackId::from("/home/savanni/Track 2.mp3".to_owned()), id: TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
@ -147,6 +212,8 @@ pub mod factories {
name: Some("Track 2".to_owned()), name: Some("Track 2".to_owned()),
album: Some("Savanni's Demo".to_owned()), album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()), artist: Some("Savanni".to_owned()),
duration: Some(15),
filetype: "audio/mpeg".parse::<mime::Mime>().unwrap(),
}, },
TrackInfo { TrackInfo {
id: TrackId::from("/home/savanni/Track 3.mp3".to_owned()), id: TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
@ -154,6 +221,8 @@ pub mod factories {
name: Some("Track 3".to_owned()), name: Some("Track 3".to_owned()),
album: Some("Savanni's Demo".to_owned()), album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()), artist: Some("Savanni".to_owned()),
duration: Some(15),
filetype: "audio/mpeg".parse::<mime::Mime>().unwrap(),
}, },
TrackInfo { TrackInfo {
id: TrackId::from("/home/savanni/Track 4.mp3".to_owned()), id: TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
@ -161,6 +230,8 @@ pub mod factories {
name: Some("Track 4".to_owned()), name: Some("Track 4".to_owned()),
album: Some("Savanni's Demo".to_owned()), album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()), artist: Some("Savanni".to_owned()),
duration: Some(15),
filetype: "audio/mpeg".parse::<mime::Mime>().unwrap(),
}, },
TrackInfo { TrackInfo {
id: TrackId::from("/home/savanni/Track 5.mp3".to_owned()), id: TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
@ -168,6 +239,8 @@ pub mod factories {
name: Some("Track 5".to_owned()), name: Some("Track 5".to_owned()),
album: Some("Savanni's Demo".to_owned()), album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()), artist: Some("Savanni".to_owned()),
duration: Some(15),
filetype: "audio/mpeg".parse::<mime::Mime>().unwrap(),
}, },
], ],
} }