cargo fmt

This commit is contained in:
xenofem 2022-04-27 00:55:36 -04:00
parent 3d5010806b
commit ba3326ef24
4 changed files with 116 additions and 74 deletions

View file

@ -1,14 +1,14 @@
use std::{collections::HashSet, fmt::Display, io::Write};
use actix::{Actor, StreamHandler, ActorContext, AsyncContext};
use actix_http::ws::{Item, CloseReason};
use actix::{Actor, ActorContext, AsyncContext, StreamHandler};
use actix_http::ws::{CloseReason, Item};
use actix_web_actors::ws::{self, CloseCode};
use log::{error, debug, info, trace};
use log::{debug, error, info, trace};
use rand::distributions::{Alphanumeric, DistString};
use serde::Deserialize;
use time::OffsetDateTime;
use crate::{UploadedFile, DownloadableFile, file::LiveWriter};
use crate::{file::LiveWriter, DownloadableFile, UploadedFile};
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
time::macros::format_description!("[year]-[month]-[day]-[hour][minute][second]");
@ -97,7 +97,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
error!("Websocket error: {:?}", e);
ctx.stop();
return;
},
}
};
match self.handle_message(msg, ctx) {
@ -116,10 +116,10 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
code: CloseCode::Normal,
description: None,
}));
// self.app_data.write().unwrap().entry(
// self.app_data.write().unwrap().entry(
ctx.stop();
}
_ => ()
_ => (),
}
}
}
@ -129,12 +129,16 @@ fn ack(ctx: &mut <Uploader as Actor>::Context) {
}
impl Uploader {
fn handle_message(&mut self, msg: ws::Message, ctx: &mut <Self as Actor>::Context) -> Result<bool, Error>{
fn handle_message(
&mut self,
msg: ws::Message,
ctx: &mut <Self as Actor>::Context,
) -> Result<bool, Error> {
trace!("Websocket message: {:?}", msg);
match msg {
ws::Message::Text(text) => {
if self.writer.is_some() {
return Err(Error::UnexpectedMessageType)
return Err(Error::UnexpectedMessageType);
}
let raw_files: Vec<RawUploadedFile> = serde_json::from_slice(text.as_bytes())?;
info!("Received file list: {} files", raw_files.len());
@ -167,32 +171,42 @@ impl Uploader {
let writer = super::zip::ZipGenerator::new(files, Box::new(writer));
let size = writer.total_size();
self.writer = Some(Box::new(writer));
let download_filename = super::APP_NAME.to_owned()
+ &now.format(FILENAME_DATE_FORMAT)?
+ ".zip";
let download_filename =
super::APP_NAME.to_owned() + &now.format(FILENAME_DATE_FORMAT)? + ".zip";
let modtime = now;
self.app_data.write().map_err(|_| Error::LockPoisoned)?.insert(storage_filename, DownloadableFile {
name: download_filename,
size,
modtime,
uploader: Some(ctx.address()),
});
self.app_data
.write()
.map_err(|_| Error::LockPoisoned)?
.insert(
storage_filename,
DownloadableFile {
name: download_filename,
size,
modtime,
uploader: Some(ctx.address()),
},
);
} else {
self.writer = Some(Box::new(writer));
self.app_data.write().map_err(|_| Error::LockPoisoned)?.insert(storage_filename, DownloadableFile {
name: files[0].name.clone(),
size: files[0].size,
modtime: files[0].modtime,
uploader: Some(ctx.address()),
});
self.app_data
.write()
.map_err(|_| Error::LockPoisoned)?
.insert(
storage_filename,
DownloadableFile {
name: files[0].name.clone(),
size: files[0].size,
modtime: files[0].modtime,
uploader: Some(ctx.address()),
},
);
}
ack(ctx);
}
ws::Message::Binary(data)
| ws::Message::Continuation(Item::FirstBinary(data))
| ws::Message::Continuation(Item::Continue(data))
| ws::Message::Continuation(Item::Last(data)) =>
{
| ws::Message::Continuation(Item::FirstBinary(data))
| ws::Message::Continuation(Item::Continue(data))
| ws::Message::Continuation(Item::Last(data)) => {
if let Some(ref mut writer) = self.writer {
if data.len() > self.bytes_remaining {
return Err(Error::TooMuchData);