cleanup all the <Uploader as Actor>::Contexts

This commit is contained in:
xenofem 2022-04-30 16:10:12 -04:00
parent e95590ef07
commit b33bb7aa9d

View file

@ -82,6 +82,8 @@ impl Actor for Uploader {
type Context = ws::WebsocketContext<Self>; type Context = ws::WebsocketContext<Self>;
} }
type Context = <Uploader as Actor>::Context;
pub struct UploadedFile { pub struct UploadedFile {
pub name: String, pub name: String,
pub size: u64, pub size: u64,
@ -141,7 +143,7 @@ impl From<&Error> for ServerMessage {
} }
} }
fn stop_and_flush<T>(_: T, u: &mut Uploader, ctx: &mut <Uploader as Actor>::Context) { fn stop_and_flush<T>(_: T, u: &mut Uploader, ctx: &mut Context) {
ctx.stop(); ctx.stop();
if let Some(w) = u.writer.as_mut() { if let Some(w) = u.writer.as_mut() {
if let Err(e) = w.flush() { if let Err(e) = w.flush() {
@ -178,12 +180,12 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
} }
} }
fn ack(ctx: &mut <Uploader as Actor>::Context) { fn ack(ctx: &mut Context) {
ctx.text("ack"); ctx.text("ack");
} }
impl Uploader { impl Uploader {
fn notify_error_and_cleanup(&mut self, e: Error, ctx: &mut <Self as Actor>::Context) { fn notify_error_and_cleanup(&mut self, e: Error, ctx: &mut Context) {
error!("{}", e); error!("{}", e);
ctx.text(serde_json::to_string(&ServerMessage::from(&e)).unwrap()); ctx.text(serde_json::to_string(&ServerMessage::from(&e)).unwrap());
ctx.close(Some(ws::CloseReason { ctx.close(Some(ws::CloseReason {
@ -196,7 +198,7 @@ impl Uploader {
fn handle_message( fn handle_message(
&mut self, &mut self,
msg: ws::Message, msg: ws::Message,
ctx: &mut <Self as Actor>::Context, ctx: &mut Context,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
trace!("Websocket message: {:?}", msg); trace!("Websocket message: {:?}", msg);
match msg { match msg {
@ -273,7 +275,7 @@ impl Uploader {
.await .await
.add_file(storage_filename, stored_file) .add_file(storage_filename, stored_file)
.await .await
}).map(|res, u: &mut Self, ctx: &mut <Self as Actor>::Context| { }).map(|res, u: &mut Self, ctx: &mut Context| {
match res { match res {
Ok(Ok(())) => ctx.text(serde_json::to_string(&ServerMessage::Ready { code: u.storage_filename.clone() }).unwrap()), Ok(Ok(())) => ctx.text(serde_json::to_string(&ServerMessage::Ready { code: u.storage_filename.clone() }).unwrap()),
Ok(Err(size)) => u.notify_error_and_cleanup(Error::TooBig(size), ctx), Ok(Err(size)) => u.notify_error_and_cleanup(Error::TooBig(size), ctx),
@ -322,7 +324,7 @@ impl Uploader {
} }
} }
fn cleanup_after_error(&mut self, ctx: &mut <Self as Actor>::Context) { fn cleanup_after_error(&mut self, ctx: &mut Context) {
info!( info!(
"Cleaning up after failed upload of {}", "Cleaning up after failed upload of {}",
self.storage_filename self.storage_filename