actually support cancels
This commit is contained in:
parent
e4e6e391a3
commit
43ec364fe8
2 changed files with 18 additions and 5 deletions
|
@ -15,7 +15,7 @@ use tower_http::limit::RequestBodyLimitLayer;
|
|||
use crate::{
|
||||
JocalService,
|
||||
discovery::register_device,
|
||||
transfer::{handle_prepare_upload, handle_receive_upload},
|
||||
transfer::{handle_cancel, handle_prepare_upload, handle_receive_upload},
|
||||
};
|
||||
|
||||
impl JocalService {
|
||||
|
@ -61,6 +61,7 @@ impl JocalService {
|
|||
post(handle_prepare_upload),
|
||||
)
|
||||
.route("/api/localsend/v2/upload", post(handle_receive_upload))
|
||||
.route("/api/localsend/v2/cancel", post(handle_cancel))
|
||||
.layer(DefaultBodyLimit::disable())
|
||||
.layer(RequestBodyLimitLayer::new(1024 * 1024 * 1024))
|
||||
.with_state(self.clone())
|
||||
|
|
|
@ -329,21 +329,33 @@ pub async fn handle_cancel(
|
|||
Query(params): Query<CancelParams>,
|
||||
State(service): State<JocalService>,
|
||||
) -> impl IntoResponse {
|
||||
// it's OK to hold this lock for the whole body here, we hardly ever have to
|
||||
// handle cancels and none of these ops are slow.
|
||||
let mut sessions_lock = service.sessions.write().await;
|
||||
let session = match sessions_lock.get_mut(¶ms.session_id) {
|
||||
Some(session) => session,
|
||||
None => return StatusCode::BAD_REQUEST.into_response(),
|
||||
};
|
||||
|
||||
debug!("got cancel request for {}", params.session_id);
|
||||
info!(
|
||||
"{} cancelled the transfer of {}",
|
||||
&session.sender.alias,
|
||||
session
|
||||
.files
|
||||
.values()
|
||||
.map(|f| f.file_name.clone())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
);
|
||||
|
||||
session.status = SessionStatus::Cancelled;
|
||||
|
||||
if let Ok(id) = Julid::from_str(¶ms.session_id) {
|
||||
service.send_event(JocalEvent::Cancelled { session_id: id });
|
||||
};
|
||||
|
||||
StatusCode::OK.into_response()
|
||||
} else {
|
||||
StatusCode::BAD_REQUEST.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
// Cancel parameters struct
|
||||
|
|
Loading…
Reference in a new issue