Fixed a bug where DB wasn't accessed with the correct type

This commit is contained in:
Antoine Gersant 2018-10-28 17:51:46 -07:00
parent af17c821df
commit 7e11b651ed

View file

@ -80,7 +80,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for AdminRights {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> { fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
let db = request.guard::<State<DB>>()?; let db = request.guard::<State<Arc<DB>>>()?;
match user::count::<DB>(&db) { match user::count::<DB>(&db) {
Err(_) => return Outcome::Failure((Status::InternalServerError, ())), Err(_) => return Outcome::Failure((Status::InternalServerError, ())),
@ -139,7 +139,7 @@ struct InitialSetup {
} }
#[get("/initial_setup")] #[get("/initial_setup")]
fn initial_setup(db: State<DB>) -> Result<Json<InitialSetup>, errors::Error> { fn initial_setup(db: State<Arc<DB>>) -> Result<Json<InitialSetup>, errors::Error> {
let initial_setup = InitialSetup { let initial_setup = InitialSetup {
has_any_users: user::count::<DB>(&db)? > 0, has_any_users: user::count::<DB>(&db)? > 0,
}; };
@ -147,14 +147,14 @@ fn initial_setup(db: State<DB>) -> Result<Json<InitialSetup>, errors::Error> {
} }
#[get("/settings")] #[get("/settings")]
fn get_settings(db: State<DB>, _admin_rights: AdminRights) -> Result<Json<Config>, errors::Error> { fn get_settings(db: State<Arc<DB>>, _admin_rights: AdminRights) -> Result<Json<Config>, errors::Error> {
let config = config::read::<DB>(&db)?; let config = config::read::<DB>(&db)?;
Ok(Json(config)) Ok(Json(config))
} }
#[put("/settings", data = "<config>")] #[put("/settings", data = "<config>")]
fn put_settings( fn put_settings(
db: State<DB>, db: State<Arc<DB>>,
_admin_rights: AdminRights, _admin_rights: AdminRights,
config: Json<Config>, config: Json<Config>,
) -> Result<(), errors::Error> { ) -> Result<(), errors::Error> {
@ -163,14 +163,14 @@ fn put_settings(
} }
#[get("/preferences")] #[get("/preferences")]
fn get_preferences(db: State<DB>, auth: Auth) -> Result<Json<Preferences>, errors::Error> { fn get_preferences(db: State<Arc<DB>>, auth: Auth) -> Result<Json<Preferences>, errors::Error> {
let preferences = config::read_preferences::<DB>(&db, &auth.username)?; let preferences = config::read_preferences::<DB>(&db, &auth.username)?;
Ok(Json(preferences)) Ok(Json(preferences))
} }
#[put("/preferences", data = "<preferences>")] #[put("/preferences", data = "<preferences>")]
fn put_preferences( fn put_preferences(
db: State<DB>, db: State<Arc<DB>>,
auth: Auth, auth: Auth,
preferences: Json<Preferences>, preferences: Json<Preferences>,
) -> Result<(), errors::Error> { ) -> Result<(), errors::Error> {
@ -200,7 +200,7 @@ struct AuthOutput {
#[post("/auth", data = "<credentials>")] #[post("/auth", data = "<credentials>")]
fn auth( fn auth(
db: State<DB>, db: State<Arc<DB>>,
credentials: Json<AuthCredentials>, credentials: Json<AuthCredentials>,
mut cookies: Cookies, mut cookies: Cookies,
) -> Result<Json<AuthOutput>, errors::Error> { ) -> Result<Json<AuthOutput>, errors::Error> {
@ -219,77 +219,77 @@ fn auth(
#[get("/browse")] #[get("/browse")]
fn browse_root( fn browse_root(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> { ) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> {
let result = index::browse(db.deref(), &PathBuf::new())?; let result = index::browse(db.deref().deref(), &PathBuf::new())?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/browse/<path>")] #[get("/browse/<path>")]
fn browse( fn browse(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
path: VFSPathBuf, path: VFSPathBuf,
) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> { ) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> {
let result = index::browse(db.deref(), &path.into() as &PathBuf)?; let result = index::browse(db.deref().deref(), &path.into() as &PathBuf)?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/flatten")] #[get("/flatten")]
fn flatten_root(db: State<DB>, _auth: Auth) -> Result<Json<Vec<index::Song>>, errors::Error> { fn flatten_root(db: State<Arc<DB>>, _auth: Auth) -> Result<Json<Vec<index::Song>>, errors::Error> {
let result = index::flatten(db.deref(), &PathBuf::new())?; let result = index::flatten(db.deref().deref(), &PathBuf::new())?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/flatten/<path>")] #[get("/flatten/<path>")]
fn flatten( fn flatten(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
path: VFSPathBuf, path: VFSPathBuf,
) -> Result<Json<Vec<index::Song>>, errors::Error> { ) -> Result<Json<Vec<index::Song>>, errors::Error> {
let result = index::flatten(db.deref(), &path.into() as &PathBuf)?; let result = index::flatten(db.deref().deref(), &path.into() as &PathBuf)?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/random")] #[get("/random")]
fn random(db: State<DB>, _auth: Auth) -> Result<Json<Vec<index::Directory>>, errors::Error> { fn random(db: State<Arc<DB>>, _auth: Auth) -> Result<Json<Vec<index::Directory>>, errors::Error> {
let result = index::get_random_albums(db.deref(), 20)?; let result = index::get_random_albums(db.deref().deref(), 20)?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/recent")] #[get("/recent")]
fn recent(db: State<DB>, _auth: Auth) -> Result<Json<Vec<index::Directory>>, errors::Error> { fn recent(db: State<Arc<DB>>, _auth: Auth) -> Result<Json<Vec<index::Directory>>, errors::Error> {
let result = index::get_recent_albums(db.deref(), 20)?; let result = index::get_recent_albums(db.deref().deref(), 20)?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/search")] #[get("/search")]
fn search_root( fn search_root(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> { ) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> {
let result = index::search(db.deref(), "")?; let result = index::search(db.deref().deref(), "")?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/search/<query>")] #[get("/search/<query>")]
fn search( fn search(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
query: String, query: String,
) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> { ) -> Result<Json<Vec<index::CollectionFile>>, errors::Error> {
let result = index::search(db.deref(), &query)?; let result = index::search(db.deref().deref(), &query)?;
Ok(Json(result)) Ok(Json(result))
} }
#[get("/serve/<path>")] #[get("/serve/<path>")]
fn serve( fn serve(
db: State<DB>, db: State<Arc<DB>>,
_auth: Auth, _auth: Auth,
path: VFSPathBuf, path: VFSPathBuf,
) -> Result<serve::RangeResponder<File>, errors::Error> { ) -> Result<serve::RangeResponder<File>, errors::Error> {
let db: &DB = db.deref(); let db: &DB = db.deref().deref();
let vfs = db.get_vfs()?; let vfs = db.get_vfs()?;
let real_path = vfs.virtual_to_real(&path.into() as &PathBuf)?; let real_path = vfs.virtual_to_real(&path.into() as &PathBuf)?;
@ -310,10 +310,10 @@ struct ListPlaylistsEntry {
#[get("/playlists")] #[get("/playlists")]
fn list_playlists( fn list_playlists(
db: State<DB>, db: State<Arc<DB>>,
auth: Auth, auth: Auth,
) -> Result<Json<Vec<ListPlaylistsEntry>>, errors::Error> { ) -> Result<Json<Vec<ListPlaylistsEntry>>, errors::Error> {
let playlist_names = playlist::list_playlists(&auth.username, db.deref())?; let playlist_names = playlist::list_playlists(&auth.username, db.deref().deref())?;
let playlists: Vec<ListPlaylistsEntry> = playlist_names let playlists: Vec<ListPlaylistsEntry> = playlist_names
.into_iter() .into_iter()
.map(|p| ListPlaylistsEntry { name: p }) .map(|p| ListPlaylistsEntry { name: p })
@ -329,51 +329,51 @@ struct SavePlaylistInput {
#[put("/playlist/<name>", data = "<playlist>")] #[put("/playlist/<name>", data = "<playlist>")]
fn save_playlist( fn save_playlist(
db: State<DB>, db: State<Arc<DB>>,
auth: Auth, auth: Auth,
name: String, name: String,
playlist: Json<SavePlaylistInput>, playlist: Json<SavePlaylistInput>,
) -> Result<(), errors::Error> { ) -> Result<(), errors::Error> {
playlist::save_playlist(&name, &auth.username, &playlist.tracks, db.deref())?; playlist::save_playlist(&name, &auth.username, &playlist.tracks, db.deref().deref())?;
Ok(()) Ok(())
} }
#[get("/playlist/<name>")] #[get("/playlist/<name>")]
fn read_playlist( fn read_playlist(
db: State<DB>, db: State<Arc<DB>>,
auth: Auth, auth: Auth,
name: String, name: String,
) -> Result<Json<Vec<index::Song>>, errors::Error> { ) -> Result<Json<Vec<index::Song>>, errors::Error> {
let songs = playlist::read_playlist(&name, &auth.username, db.deref())?; let songs = playlist::read_playlist(&name, &auth.username, db.deref().deref())?;
Ok(Json(songs)) Ok(Json(songs))
} }
#[delete("/playlist/<name>")] #[delete("/playlist/<name>")]
fn delete_playlist(db: State<DB>, auth: Auth, name: String) -> Result<(), errors::Error> { fn delete_playlist(db: State<Arc<DB>>, auth: Auth, name: String) -> Result<(), errors::Error> {
playlist::delete_playlist(&name, &auth.username, db.deref())?; playlist::delete_playlist(&name, &auth.username, db.deref().deref())?;
Ok(()) Ok(())
} }
#[put("/lastfm/now_playing/<path>")] #[put("/lastfm/now_playing/<path>")]
fn lastfm_now_playing(db: State<DB>, auth: Auth, path: VFSPathBuf) -> Result<(), errors::Error> { fn lastfm_now_playing(db: State<Arc<DB>>, auth: Auth, path: VFSPathBuf) -> Result<(), errors::Error> {
lastfm::now_playing(db.deref(), &auth.username, &path.into() as &PathBuf)?; lastfm::now_playing(db.deref().deref(), &auth.username, &path.into() as &PathBuf)?;
Ok(()) Ok(())
} }
#[post("/lastfm/scrobble/<path>")] #[post("/lastfm/scrobble/<path>")]
fn lastfm_scrobble(db: State<DB>, auth: Auth, path: VFSPathBuf) -> Result<(), errors::Error> { fn lastfm_scrobble(db: State<Arc<DB>>, auth: Auth, path: VFSPathBuf) -> Result<(), errors::Error> {
lastfm::scrobble(db.deref(), &auth.username, &path.into() as &PathBuf)?; lastfm::scrobble(db.deref().deref(), &auth.username, &path.into() as &PathBuf)?;
Ok(()) Ok(())
} }
#[get("/lastfm/link?<token>&<content>")] #[get("/lastfm/link?<token>&<content>")]
fn lastfm_link( fn lastfm_link(
db: State<DB>, db: State<Arc<DB>>,
auth: Auth, auth: Auth,
token: String, token: String,
content: String, content: String,
) -> Result<Html<String>, errors::Error> { ) -> Result<Html<String>, errors::Error> {
lastfm::link(db.deref(), &auth.username, &token)?; lastfm::link(db.deref().deref(), &auth.username, &token)?;
// Percent decode // Percent decode
let base64_content = match RawStr::from_str(&content).percent_decode() { let base64_content = match RawStr::from_str(&content).percent_decode() {
@ -397,7 +397,7 @@ fn lastfm_link(
} }
#[delete("/lastfm/link")] #[delete("/lastfm/link")]
fn lastfm_unlink(db: State<DB>, auth: Auth) -> Result<(), errors::Error> { fn lastfm_unlink(db: State<Arc<DB>>, auth: Auth) -> Result<(), errors::Error> {
lastfm::unlink(db.deref(), &auth.username)?; lastfm::unlink(db.deref().deref(), &auth.username)?;
Ok(()) Ok(())
} }