cargo clippy and fmt
This commit is contained in:
parent
8275b940ac
commit
bfe7fcde99
3 changed files with 71 additions and 50 deletions
|
@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
|||
use time::OffsetDateTime;
|
||||
use unicode_normalization::UnicodeNormalization;
|
||||
|
||||
use crate::store::{storage_dir, StoredFile, self};
|
||||
use crate::store::{self, storage_dir, StoredFile};
|
||||
|
||||
const MAX_FILES: usize = 256;
|
||||
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
|
||||
|
@ -44,14 +44,14 @@ impl Error {
|
|||
match self {
|
||||
Self::Storage(_) => CloseCode::Error,
|
||||
Self::Parse(_)
|
||||
| Self::UnexpectedMessageType
|
||||
| Self::ClosedEarly(_)
|
||||
| Self::UnexpectedExtraData => CloseCode::Invalid,
|
||||
| Self::UnexpectedMessageType
|
||||
| Self::ClosedEarly(_)
|
||||
| Self::UnexpectedExtraData => CloseCode::Invalid,
|
||||
Self::DuplicateFilename
|
||||
| Self::NoFiles
|
||||
| Self::TooManyFiles
|
||||
| Self::TooLong
|
||||
| Self::TooBig(_) => CloseCode::Policy,
|
||||
| Self::NoFiles
|
||||
| Self::TooManyFiles
|
||||
| Self::TooLong
|
||||
| Self::TooBig(_) => CloseCode::Policy,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -132,9 +132,15 @@ enum ServerMessage {
|
|||
impl From<&Error> for ServerMessage {
|
||||
fn from(e: &Error) -> Self {
|
||||
match e {
|
||||
Error::TooBig(max_size) => ServerMessage::TooBig { max_size: *max_size },
|
||||
Error::TooLong => ServerMessage::TooLong { max_days: store::max_lifetime() },
|
||||
_ => ServerMessage::Error { details: e.to_string() },
|
||||
Error::TooBig(max_size) => ServerMessage::TooBig {
|
||||
max_size: *max_size,
|
||||
},
|
||||
Error::TooLong => ServerMessage::TooLong {
|
||||
max_days: store::max_lifetime(),
|
||||
},
|
||||
_ => ServerMessage::Error {
|
||||
details: e.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -191,18 +197,17 @@ impl Uploader {
|
|||
self.cleanup_after_error(ctx);
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
&mut self,
|
||||
msg: ws::Message,
|
||||
ctx: &mut Context,
|
||||
) -> Result<bool, Error> {
|
||||
fn handle_message(&mut self, msg: ws::Message, ctx: &mut Context) -> Result<bool, Error> {
|
||||
trace!("Websocket message: {:?}", msg);
|
||||
match msg {
|
||||
ws::Message::Text(text) => {
|
||||
if self.writer.is_some() {
|
||||
return Err(Error::UnexpectedMessageType);
|
||||
}
|
||||
let UploadManifest { files: raw_files, lifetime, } = serde_json::from_slice(text.as_bytes())?;
|
||||
let UploadManifest {
|
||||
files: raw_files,
|
||||
lifetime,
|
||||
} = serde_json::from_slice(text.as_bytes())?;
|
||||
if lifetime > store::max_lifetime() {
|
||||
return Err(Error::TooLong);
|
||||
}
|
||||
|
@ -235,19 +240,15 @@ impl Uploader {
|
|||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&storage_path)?;
|
||||
let (writer, name, size, modtime): (Box<dyn Write>,_,_,_) = if files.len() > 1 {
|
||||
let (writer, name, size, modtime): (Box<dyn Write>, _, _, _) = if files.len() > 1 {
|
||||
info!("Wrapping in zipfile generator");
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let zip_writer = super::zip::ZipGenerator::new(files, writer);
|
||||
let size = zip_writer.total_size();
|
||||
let download_filename =
|
||||
super::APP_NAME.to_owned() + &now.format(FILENAME_DATE_FORMAT).unwrap() + ".zip";
|
||||
(
|
||||
Box::new(zip_writer),
|
||||
download_filename,
|
||||
size,
|
||||
now,
|
||||
)
|
||||
let download_filename = super::APP_NAME.to_owned()
|
||||
+ &now.format(FILENAME_DATE_FORMAT).unwrap()
|
||||
+ ".zip";
|
||||
(Box::new(zip_writer), download_filename, size, now)
|
||||
} else {
|
||||
(
|
||||
Box::new(writer),
|
||||
|
@ -261,23 +262,29 @@ impl Uploader {
|
|||
name,
|
||||
size,
|
||||
modtime,
|
||||
expiry: OffsetDateTime::now_utc() + lifetime*time::Duration::DAY,
|
||||
expiry: OffsetDateTime::now_utc() + lifetime * time::Duration::DAY,
|
||||
};
|
||||
let data = self.app_data.clone();
|
||||
let storage_filename = self.storage_filename.clone();
|
||||
ctx.spawn(actix::fut::wrap_future(async move {
|
||||
debug!("Spawned future to add entry {} to state", storage_filename);
|
||||
data.write()
|
||||
.await
|
||||
.add_file(storage_filename, stored_file)
|
||||
.await
|
||||
}).map(|res, u: &mut Self, ctx: &mut Context| {
|
||||
match res {
|
||||
Ok(Ok(())) => ctx.text(serde_json::to_string(&ServerMessage::Ready { code: u.storage_filename.clone() }).unwrap()),
|
||||
ctx.spawn(
|
||||
actix::fut::wrap_future(async move {
|
||||
debug!("Spawned future to add entry {} to state", storage_filename);
|
||||
data.write()
|
||||
.await
|
||||
.add_file(storage_filename, stored_file)
|
||||
.await
|
||||
})
|
||||
.map(|res, u: &mut Self, ctx: &mut Context| match res {
|
||||
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),
|
||||
Err(e) => u.notify_error_and_cleanup(Error::from(e), ctx)
|
||||
}
|
||||
}));
|
||||
Err(e) => u.notify_error_and_cleanup(Error::from(e), ctx),
|
||||
}),
|
||||
);
|
||||
}
|
||||
ws::Message::Binary(data) | ws::Message::Continuation(Item::Last(data)) => {
|
||||
let result = self.handle_data(data)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue