47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use poem::web::Data;
|
|
use poem_ext::db::DbTxn;
|
|
use tracing::{error, instrument};
|
|
|
|
use crate::{
|
|
authentication::Authentication,
|
|
subsonic::{Error, SubsonicResponse},
|
|
utils::{self},
|
|
};
|
|
|
|
#[poem::handler]
|
|
#[instrument(skip(txn, auth))]
|
|
pub async fn start_scan(Data(txn): Data<&DbTxn>, auth: Authentication) -> SubsonicResponse {
|
|
let u = utils::verify_user(txn.clone(), auth).await;
|
|
|
|
match u {
|
|
Ok(u) => {
|
|
if !u.is_admin {
|
|
return SubsonicResponse::new_error(Error::UserIsNotAuthorizedForGivenOperation(
|
|
None,
|
|
));
|
|
}
|
|
}
|
|
Err(e) => return e,
|
|
}
|
|
crate::scan::start_scan().await;
|
|
|
|
let res = crate::scan::get_scan_status().await;
|
|
|
|
match res {
|
|
Ok(status) => {
|
|
if status.errors.is_empty() {
|
|
SubsonicResponse::new_scan_status(status.scanning, status.count)
|
|
} else {
|
|
error!("Failed to start scan:");
|
|
for e in status.errors {
|
|
error!(error = e.report.root_cause(), "{e:?}");
|
|
}
|
|
SubsonicResponse::new_error(Error::Generic(None))
|
|
}
|
|
}
|
|
Err(e) => {
|
|
error!(error = e.root_cause(), "Failed to start scan: {e}");
|
|
SubsonicResponse::new_error(Error::Generic(None))
|
|
}
|
|
}
|
|
}
|