cargo clippy and fmt

This commit is contained in:
xenofem 2022-04-28 05:18:35 -04:00
parent 127d7e9c67
commit bda6da33e8
6 changed files with 129 additions and 68 deletions

View file

@ -4,8 +4,8 @@ use actix_web::{
body::{self, BoxBody, SizedStream}, body::{self, BoxBody, SizedStream},
http::{ http::{
header::{ header::{
self, ContentDisposition, DispositionParam, self, ContentDisposition, DispositionParam, DispositionType, EntityTag, HeaderValue,
DispositionType, HeaderValue, HttpDate, EntityTag, IfUnmodifiedSince, IfMatch, IfNoneMatch, IfModifiedSince, HttpDate, IfMatch, IfModifiedSince, IfNoneMatch, IfUnmodifiedSince,
}, },
StatusCode, StatusCode,
}, },
@ -52,7 +52,7 @@ impl DownloadingFile {
ContentDisposition { ContentDisposition {
disposition: DispositionType::Attachment, disposition: DispositionType::Attachment,
parameters: vec![DispositionParam::Filename(self.info.name)], parameters: vec![DispositionParam::Filename(self.info.name)],
} },
)); ));
res.insert_header((header::LAST_MODIFIED, last_modified)); res.insert_header((header::LAST_MODIFIED, last_modified));
res.insert_header((header::ETAG, etag)); res.insert_header((header::ETAG, etag));
@ -76,7 +76,12 @@ impl DownloadingFile {
res.insert_header(( res.insert_header((
header::CONTENT_RANGE, header::CONTENT_RANGE,
format!("bytes {}-{}/{}", offset, offset + length - 1, self.info.size), format!(
"bytes {}-{}/{}",
offset,
offset + length - 1,
self.info.size
),
)); ));
} else { } else {
res.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length))); res.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length)));
@ -122,7 +127,9 @@ fn precondition_failed(req: &HttpRequest, etag: &EntityTag, last_modified: &Http
fn not_modified(req: &HttpRequest, etag: &EntityTag, last_modified: &HttpDate) -> bool { fn not_modified(req: &HttpRequest, etag: &EntityTag, last_modified: &HttpDate) -> bool {
match req.get_header::<IfNoneMatch>() { match req.get_header::<IfNoneMatch>() {
Some(IfNoneMatch::Any) => { return true; } Some(IfNoneMatch::Any) => {
return true;
}
Some(IfNoneMatch::Items(ref items)) => { Some(IfNoneMatch::Items(ref items)) => {
return items.iter().any(|item| item.weak_eq(etag)); return items.iter().any(|item| item.weak_eq(etag));
} }

View file

@ -1,4 +1,12 @@
use std::{cmp, fs::File, future::Future, io::{self, Write}, path::PathBuf, pin::Pin, task::{Context, Poll, Waker}}; use std::{
cmp,
fs::File,
future::Future,
io::{self, Write},
path::PathBuf,
pin::Pin,
task::{Context, Poll, Waker},
};
use actix::Addr; use actix::Addr;
use actix_web::error::{Error, ErrorInternalServerError}; use actix_web::error::{Error, ErrorInternalServerError};
@ -53,7 +61,6 @@ impl Write for LiveFileWriter {
} }
} }
// This implementation of a file responder is copied pretty directly // This implementation of a file responder is copied pretty directly
// from actix-files with some tweaks // from actix-files with some tweaks
@ -104,13 +111,19 @@ async fn live_file_reader_callback(
use io::{Read as _, Seek as _}; use io::{Read as _, Seek as _};
let res = actix_web::web::block(move || { let res = actix_web::web::block(move || {
trace!("reading up to {} bytes of file starting at {}", max_bytes, offset); trace!(
"reading up to {} bytes of file starting at {}",
max_bytes,
offset
);
let mut buf = Vec::with_capacity(max_bytes); let mut buf = Vec::with_capacity(max_bytes);
file.seek(io::SeekFrom::Start(offset))?; file.seek(io::SeekFrom::Start(offset))?;
let n_bytes = std::io::Read::by_ref(&mut file).take(max_bytes as u64).read_to_end(&mut buf)?; let n_bytes = std::io::Read::by_ref(&mut file)
.take(max_bytes as u64)
.read_to_end(&mut buf)?;
trace!("got {} bytes from file", n_bytes); trace!("got {} bytes from file", n_bytes);
if n_bytes == 0 { if n_bytes == 0 {
Err(io::Error::from(io::ErrorKind::UnexpectedEof)) Err(io::Error::from(io::ErrorKind::UnexpectedEof))
@ -141,12 +154,14 @@ where
if size == counter { if size == counter {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
let inner_file = file let inner_file = file.take().expect("LiveFileReader polled after completion");
.take()
.expect("LiveFileReader polled after completion");
if offset >= *this.available_file_size { if offset >= *this.available_file_size {
trace!("offset {} has reached available file size {}, updating metadata", offset, this.available_file_size); trace!(
"offset {} has reached available file size {}, updating metadata",
offset,
this.available_file_size
);
// If we've hit the end of what was available // If we've hit the end of what was available
// last time we checked, check again // last time we checked, check again
*this.available_file_size = match inner_file.metadata() { *this.available_file_size = match inner_file.metadata() {
@ -165,15 +180,25 @@ where
if let Ok(()) = addr.try_send(WakerMessage(cx.waker().clone())) { if let Ok(()) = addr.try_send(WakerMessage(cx.waker().clone())) {
return Poll::Pending; return Poll::Pending;
} else { } else {
return Poll::Ready(Some(Err(ErrorInternalServerError("Failed to contact file upload actor")))); return Poll::Ready(Some(Err(ErrorInternalServerError(
"Failed to contact file upload actor",
))));
} }
} else { } else {
return Poll::Ready(Some(Err(ErrorInternalServerError("File upload was not completed")))); return Poll::Ready(Some(Err(ErrorInternalServerError(
"File upload was not completed",
))));
} }
} }
} }
let max_bytes = cmp::min(65_536, cmp::min(size.saturating_sub(counter), this.available_file_size.saturating_sub(offset))) as usize; let max_bytes = cmp::min(
65_536,
cmp::min(
size.saturating_sub(counter),
this.available_file_size.saturating_sub(offset),
),
) as usize;
let fut = (this.callback)(inner_file, offset, max_bytes); let fut = (this.callback)(inner_file, offset, max_bytes);

View file

@ -5,14 +5,11 @@ mod upload;
mod util; mod util;
mod zip; mod zip;
use std::{ use std::{fs::File, path::PathBuf};
path::PathBuf,
fs::File,
};
use actix::Addr; use actix::Addr;
use actix_web::{ use actix_web::{
get, middleware::Logger, web, App, HttpRequest, HttpServer, Responder, HttpResponse, get, middleware::Logger, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
}; };
use actix_web_actors::ws; use actix_web_actors::ws;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -20,7 +17,7 @@ use state::PersistentState;
use time::OffsetDateTime; use time::OffsetDateTime;
use tokio::sync::RwLock; use tokio::sync::RwLock;
const APP_NAME: &'static str = "transbeam"; const APP_NAME: &str = "transbeam";
pub struct UploadedFile { pub struct UploadedFile {
name: String, name: String,
@ -55,7 +52,11 @@ fn storage_dir() -> PathBuf {
} }
#[get("/download/{file_code}")] #[get("/download/{file_code}")]
async fn handle_download(req: HttpRequest, path: web::Path<String>, data: AppData) -> actix_web::Result<HttpResponse> { async fn handle_download(
req: HttpRequest,
path: web::Path<String>,
data: AppData,
) -> actix_web::Result<HttpResponse> {
let file_code = path.into_inner(); let file_code = path.into_inner();
if !util::is_ascii_alphanumeric(&file_code) { if !util::is_ascii_alphanumeric(&file_code) {
return Ok(HttpResponse::NotFound().finish()); return Ok(HttpResponse::NotFound().finish());
@ -65,8 +66,9 @@ async fn handle_download(req: HttpRequest, path: web::Path<String>, data: AppDat
if let Some(info) = info { if let Some(info) = info {
Ok(download::DownloadingFile { Ok(download::DownloadingFile {
file: File::open(storage_dir().join(file_code))?, file: File::open(storage_dir().join(file_code))?,
info: info.clone(), info,
}.into_response(&req)) }
.into_response(&req))
} else { } else {
Ok(HttpResponse::NotFound().finish()) Ok(HttpResponse::NotFound().finish())
} }

View file

@ -1,19 +1,25 @@
use std::{collections::HashMap, io::ErrorKind}; use std::{collections::HashMap, io::ErrorKind};
use log::{info, error}; use log::{error, info};
use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}}; use tokio::{
fs::File,
io::{AsyncReadExt, AsyncWriteExt},
};
use crate::{DownloadableFile, storage_dir}; use crate::{storage_dir, DownloadableFile};
const STATE_FILE_NAME: &str = "files.json"; const STATE_FILE_NAME: &str = "files.json";
pub(crate) mod timestamp { pub(crate) mod timestamp {
use core::fmt; use core::fmt;
use serde::{Serializer, Deserializer, de::Visitor}; use serde::{de::Visitor, Deserializer, Serializer};
use time::OffsetDateTime; use time::OffsetDateTime;
pub(crate) fn serialize<S: Serializer>(time: &OffsetDateTime, ser: S) -> Result<S::Ok, S::Error> { pub(crate) fn serialize<S: Serializer>(
time: &OffsetDateTime,
ser: S,
) -> Result<S::Ok, S::Error> {
ser.serialize_i64(time.unix_timestamp()) ser.serialize_i64(time.unix_timestamp())
} }
@ -31,8 +37,13 @@ pub(crate) mod timestamp {
} }
} }
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<OffsetDateTime, D::Error> { pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
Ok(OffsetDateTime::from_unix_timestamp(de.deserialize_i64(I64Visitor)?).unwrap_or_else(|_| OffsetDateTime::now_utc())) de: D,
) -> Result<OffsetDateTime, D::Error> {
Ok(
OffsetDateTime::from_unix_timestamp(de.deserialize_i64(I64Visitor)?)
.unwrap_or_else(|_| OffsetDateTime::now_utc()),
)
} }
} }
@ -55,18 +66,24 @@ impl PersistentState {
let file = if let Ok(f) = File::open(storage_dir().join(&key)).await { let file = if let Ok(f) = File::open(storage_dir().join(&key)).await {
f f
} else { } else {
error!("Unable to open file {} referenced in persistent storage", key); error!(
"Unable to open file {} referenced in persistent storage",
key
);
continue; continue;
}; };
let metadata = if let Ok(md) = file.metadata().await { let metadata = if let Ok(md) = file.metadata().await {
md md
} else { } else {
error!("Unable to get metadata for file {} referenced in persistent storage", key); error!(
"Unable to get metadata for file {} referenced in persistent storage",
key
);
continue; continue;
}; };
if metadata.len() != info.size { if metadata.len() != info.size {
error!("Mismatched file size for file {} referenced in persistent storage: expected {}, found {}", key, info.size, metadata.len()); error!("Mismatched file size for file {} referenced in persistent storage: expected {}, found {}", key, info.size, metadata.len());
continue continue;
} }
filtered.insert(key, info); filtered.insert(key, info);
} }
@ -83,16 +100,23 @@ impl PersistentState {
} }
async fn save(&mut self) -> std::io::Result<()> { async fn save(&mut self) -> std::io::Result<()> {
File::create(storage_dir().join(STATE_FILE_NAME)).await?.write_all(&serde_json::to_vec_pretty(&self.0)?).await File::create(storage_dir().join(STATE_FILE_NAME))
.await?
.write_all(&serde_json::to_vec_pretty(&self.0)?)
.await
} }
pub(crate) async fn add_file(&mut self, key: String, file: DownloadableFile) -> std::io::Result<()> { pub(crate) async fn add_file(
&mut self,
key: String,
file: DownloadableFile,
) -> std::io::Result<()> {
self.0.insert(key, file); self.0.insert(key, file);
self.save().await self.save().await
} }
pub(crate) fn lookup_file(&self, key: &str) -> Option<DownloadableFile> { pub(crate) fn lookup_file(&self, key: &str) -> Option<DownloadableFile> {
self.0.get(key).map(|f| f.clone()) self.0.get(key).cloned()
} }
pub(crate) async fn remove_file(&mut self, key: &str) -> std::io::Result<()> { pub(crate) async fn remove_file(&mut self, key: &str) -> std::io::Result<()> {

View file

@ -1,6 +1,6 @@
use std::{collections::HashSet, io::Write, task::Waker}; use std::{collections::HashSet, io::Write, task::Waker};
use actix::{Actor, ActorContext, AsyncContext, StreamHandler, Message, Handler}; use actix::{Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
use actix_http::ws::{CloseReason, Item}; use actix_http::ws::{CloseReason, Item};
use actix_web_actors::ws::{self, CloseCode}; use actix_web_actors::ws::{self, CloseCode};
use bytes::Bytes; use bytes::Bytes;
@ -82,7 +82,11 @@ pub(crate) struct WakerMessage(pub Waker);
impl Handler<WakerMessage> for Uploader { impl Handler<WakerMessage> for Uploader {
type Result = (); type Result = ();
fn handle(&mut self, msg: WakerMessage, _: &mut Self::Context) { fn handle(&mut self, msg: WakerMessage, _: &mut Self::Context) {
self.writer.as_mut().map(|w| w.add_waker(msg.0)); if let Some(w) = self.writer.as_mut() {
w.add_waker(msg.0);
} else {
error!("Got a wakeup request before creating a file");
}
} }
} }
@ -198,36 +202,38 @@ impl Uploader {
let size = zip_writer.total_size(); let size = zip_writer.total_size();
let download_filename = let download_filename =
super::APP_NAME.to_owned() + &now.format(FILENAME_DATE_FORMAT)? + ".zip"; super::APP_NAME.to_owned() + &now.format(FILENAME_DATE_FORMAT)? + ".zip";
(Box::new(zip_writer), DownloadableFile { (
Box::new(zip_writer),
DownloadableFile {
name: download_filename, name: download_filename,
size, size,
modtime: now, modtime: now,
uploader: addr, uploader: addr,
}) },
)
} else { } else {
(Box::new(writer), DownloadableFile { (
Box::new(writer),
DownloadableFile {
name: files[0].name.clone(), name: files[0].name.clone(),
size: files[0].size, size: files[0].size,
modtime: files[0].modtime, modtime: files[0].modtime,
uploader: addr, uploader: addr,
}) },
)
}; };
self.writer = Some(writer); self.writer = Some(writer);
let data = self.app_data.clone(); let data = self.app_data.clone();
let storage_filename_copy = storage_filename.clone();
ctx.spawn(actix::fut::wrap_future(async move { ctx.spawn(actix::fut::wrap_future(async move {
data data.write()
.write()
.await .await
.add_file( .add_file(storage_filename, downloadable_file)
storage_filename_copy, .await
downloadable_file, .unwrap();
).await.unwrap();
})); }));
ctx.text(self.storage_filename.as_str()); ctx.text(self.storage_filename.as_str());
} }
ws::Message::Binary(data) ws::Message::Binary(data) | ws::Message::Continuation(Item::Last(data)) => {
| ws::Message::Continuation(Item::Last(data)) => {
let result = self.handle_data(data)?; let result = self.handle_data(data)?;
ack(ctx); ack(ctx);
return Ok(result); return Ok(result);
@ -272,10 +278,7 @@ impl Uploader {
let data = self.app_data.clone(); let data = self.app_data.clone();
let filename = self.storage_filename.clone(); let filename = self.storage_filename.clone();
ctx.spawn(actix::fut::wrap_future(async move { ctx.spawn(actix::fut::wrap_future(async move {
data data.write().await.remove_file(&filename).await.unwrap();
.write()
.await
.remove_file(&filename).await.unwrap();
})); }));
} }
} }