clippy and fmt
This commit is contained in:
parent
8497b4137d
commit
62e6d64253
|
@ -16,8 +16,8 @@ use inotify::{Inotify, WatchMask};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
use serde::{de, Deserialize, Deserializer};
|
use serde::{de, Deserialize, Deserializer};
|
||||||
use time::OffsetDateTime;
|
|
||||||
use std::{os::unix::fs::MetadataExt, time::SystemTime};
|
use std::{os::unix::fs::MetadataExt, time::SystemTime};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
body::{self, BoxBody, SizedStream},
|
body::{self, BoxBody, SizedStream},
|
||||||
|
@ -64,7 +64,9 @@ impl<'de> de::Visitor<'de> for SelectionVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||||
where E: de::Error {
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
if v == "all" {
|
if v == "all" {
|
||||||
Ok(DownloadSelection::All)
|
Ok(DownloadSelection::All)
|
||||||
} else if let Ok(n) = v.parse::<usize>() {
|
} else if let Ok(n) = v.parse::<usize>() {
|
||||||
|
@ -76,9 +78,7 @@ impl<'de> de::Visitor<'de> for SelectionVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for DownloadSelection {
|
impl<'de> Deserialize<'de> for DownloadSelection {
|
||||||
fn deserialize<D: Deserializer<'de>>(
|
fn deserialize<D: Deserializer<'de>>(de: D) -> Result<DownloadSelection, D::Error> {
|
||||||
de: D
|
|
||||||
) -> Result<DownloadSelection, D::Error> {
|
|
||||||
de.deserialize_any(SelectionVisitor)
|
de.deserialize_any(SelectionVisitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,8 +122,10 @@ impl DownloadingFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn baseline_offset(&self) -> u64 {
|
fn baseline_offset(&self) -> u64 {
|
||||||
if let (DownloadSelection::One(n), Some(files)) = (self.selection, self.info.contents.as_ref()) {
|
if let (DownloadSelection::One(n), Some(files)) =
|
||||||
crate::zip::file_data_offset(&files, n)
|
(self.selection, self.info.contents.as_ref())
|
||||||
|
{
|
||||||
|
crate::zip::file_data_offset(files, n)
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -184,12 +186,7 @@ impl DownloadingFile {
|
||||||
|
|
||||||
res.insert_header((
|
res.insert_header((
|
||||||
header::CONTENT_RANGE,
|
header::CONTENT_RANGE,
|
||||||
format!(
|
format!("bytes {}-{}/{}", offset, offset + length - 1, total_size,),
|
||||||
"bytes {}-{}/{}",
|
|
||||||
offset,
|
|
||||||
offset + length - 1,
|
|
||||||
total_size,
|
|
||||||
),
|
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
res.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length)));
|
res.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length)));
|
||||||
|
@ -209,7 +206,12 @@ impl DownloadingFile {
|
||||||
.map_into_boxed_body();
|
.map_into_boxed_body();
|
||||||
}
|
}
|
||||||
|
|
||||||
let reader = new_live_reader(length, self.baseline_offset() + offset, self.file, self.storage_path);
|
let reader = new_live_reader(
|
||||||
|
length,
|
||||||
|
self.baseline_offset() + offset,
|
||||||
|
self.file,
|
||||||
|
self.storage_path,
|
||||||
|
);
|
||||||
|
|
||||||
if offset != 0 || length != total_size {
|
if offset != 0 || length != total_size {
|
||||||
res.status(StatusCode::PARTIAL_CONTENT);
|
res.status(StatusCode::PARTIAL_CONTENT);
|
||||||
|
|
43
src/main.rs
43
src/main.rs
|
@ -8,8 +8,8 @@ use std::{fmt::Debug, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::InternalError, get, http::StatusCode, middleware::Logger, post, web, App, HttpRequest, HttpResponse,
|
error::InternalError, get, http::StatusCode, middleware::Logger, post, web, App, HttpRequest,
|
||||||
HttpServer, Responder,
|
HttpResponse, HttpServer, Responder,
|
||||||
};
|
};
|
||||||
use actix_web_actors::ws;
|
use actix_web_actors::ws;
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
@ -51,7 +51,6 @@ pub fn log_auth_failure(ip_addr: &str) {
|
||||||
warn!("Incorrect authentication attempt from {}", ip_addr);
|
warn!("Incorrect authentication attempt from {}", ip_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DownloadRequest {
|
struct DownloadRequest {
|
||||||
code: String,
|
code: String,
|
||||||
|
@ -81,7 +80,7 @@ async fn handle_download(
|
||||||
let info = if let Some(i) = info {
|
let info = if let Some(i) = info {
|
||||||
i
|
i
|
||||||
} else {
|
} else {
|
||||||
return not_found(req, data, true)
|
return not_found(req, data, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
let storage_path = data.config.storage_dir.join(code);
|
let storage_path = data.config.storage_dir.join(code);
|
||||||
|
@ -102,15 +101,19 @@ async fn handle_download(
|
||||||
info,
|
info,
|
||||||
selection,
|
selection,
|
||||||
}
|
}
|
||||||
.into_response(&req))
|
.into_response(&req))
|
||||||
} else {
|
} else {
|
||||||
let offsets = info.contents.as_deref().map(zip::file_data_offsets);
|
let offsets = info.contents.as_deref().map(zip::file_data_offsets);
|
||||||
Ok(HttpResponse::Ok().body(DownloadInfo {
|
Ok(HttpResponse::Ok().body(
|
||||||
file: info,
|
DownloadInfo {
|
||||||
code: code.clone(),
|
file: info,
|
||||||
available: file.metadata().await?.len(),
|
code: code.clone(),
|
||||||
offsets,
|
available: file.metadata().await?.len(),
|
||||||
}.render().unwrap()))
|
offsets,
|
||||||
|
}
|
||||||
|
.render()
|
||||||
|
.unwrap(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +136,7 @@ async fn download_info(
|
||||||
let info = if let Some(i) = info {
|
let info = if let Some(i) = info {
|
||||||
i
|
i
|
||||||
} else {
|
} else {
|
||||||
return not_found(req, data, true)
|
return not_found(req, data, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
let storage_path = data.config.storage_dir.join(code);
|
let storage_path = data.config.storage_dir.join(code);
|
||||||
|
@ -146,18 +149,18 @@ async fn download_info(
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn not_found<T>(
|
fn not_found<T>(req: HttpRequest, data: web::Data<AppState>, report: bool) -> actix_web::Result<T> {
|
||||||
req: HttpRequest,
|
|
||||||
data: web::Data<AppState>,
|
|
||||||
report: bool,
|
|
||||||
) -> actix_web::Result<T> {
|
|
||||||
if report {
|
if report {
|
||||||
let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
|
let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
|
||||||
log_auth_failure(&ip_addr);
|
log_auth_failure(&ip_addr);
|
||||||
}
|
}
|
||||||
Err(InternalError::from_response("Download not found", NamedFile::open(data.config.static_dir.join("404.html"))?
|
Err(InternalError::from_response(
|
||||||
.set_status_code(StatusCode::NOT_FOUND)
|
"Download not found",
|
||||||
.into_response(&req)).into())
|
NamedFile::open(data.config.static_dir.join("404.html"))?
|
||||||
|
.set_status_code(StatusCode::NOT_FOUND)
|
||||||
|
.into_response(&req),
|
||||||
|
)
|
||||||
|
.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/upload")]
|
#[get("/upload")]
|
||||||
|
|
|
@ -3,10 +3,7 @@ use core::fmt;
|
||||||
use serde::{de::Visitor, Deserializer, Serializer};
|
use serde::{de::Visitor, Deserializer, Serializer};
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
pub(crate) fn serialize<S: Serializer>(
|
pub(crate) fn serialize<S: Serializer>(time: &OffsetDateTime, ser: S) -> Result<S::Ok, S::Error> {
|
||||||
time: &OffsetDateTime,
|
|
||||||
ser: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
ser.serialize_i64(time.unix_timestamp())
|
ser.serialize_i64(time.unix_timestamp())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +25,7 @@ impl<'de> Visitor<'de> for I64Visitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
|
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<OffsetDateTime, D::Error> {
|
||||||
de: D,
|
|
||||||
) -> Result<OffsetDateTime, D::Error> {
|
|
||||||
Ok(
|
Ok(
|
||||||
OffsetDateTime::from_unix_timestamp(de.deserialize_i64(I64Visitor)?)
|
OffsetDateTime::from_unix_timestamp(de.deserialize_i64(I64Visitor)?)
|
||||||
.unwrap_or_else(|_| OffsetDateTime::now_utc()),
|
.unwrap_or_else(|_| OffsetDateTime::now_utc()),
|
||||||
|
|
Loading…
Reference in a new issue