cargo fmt

This commit is contained in:
Дамјан Георгиевски 2022-03-01 14:40:41 +01:00
parent 24ee4f3f09
commit 84599e5d82
4 changed files with 23 additions and 24 deletions

View file

@ -1,10 +1,10 @@
use actix_files::Directory; use actix_files::Directory;
use actix_web::{HttpRequest, HttpResponse};
use actix_web::dev::ServiceResponse; use actix_web::dev::ServiceResponse;
use std::path::Path; use actix_web::{HttpRequest, HttpResponse};
use percent_encoding::{utf8_percent_encode, CONTROLS}; // NON_ALPHANUMERIC use percent_encoding::{utf8_percent_encode, CONTROLS}; // NON_ALPHANUMERIC
use v_htmlescape::escape as escape_html_entity;
use std::fmt::Write; use std::fmt::Write;
use std::path::Path;
use v_htmlescape::escape as escape_html_entity;
macro_rules! encode_file_url { macro_rules! encode_file_url {
($path:ident) => { ($path:ident) => {
@ -31,9 +31,7 @@ pub fn directory_listing(
if dir.is_visible(&entry) { if dir.is_visible(&entry) {
let entry = entry.unwrap(); let entry = entry.unwrap();
let p = match entry.path().strip_prefix(&dir.path) { let p = match entry.path().strip_prefix(&dir.path) {
Ok(p) if cfg!(windows) => { Ok(p) if cfg!(windows) => base.join(p).to_string_lossy().replace("\\", "/"),
base.join(p).to_string_lossy().replace("\\", "/")
}
Ok(p) => base.join(p).to_string_lossy().into_owned(), Ok(p) => base.join(p).to_string_lossy().into_owned(),
Err(_) => continue, Err(_) => continue,
}; };
@ -66,7 +64,8 @@ pub fn directory_listing(
let header = format!( let header = format!(
"<h1>Index of {}/</h1>\n\ "<h1>Index of {}/</h1>\n\
<small>[<a href='{}.tar'>.tar</a> of whole directory]</small>", <small>[<a href='{}.tar'>.tar</a> of whole directory]</small>",
index_of, if index_of.is_empty() { "_" } else { index_of } index_of,
if index_of.is_empty() { "_" } else { index_of }
); );
let footer = format!( let footer = format!(

View file

@ -1,8 +1,7 @@
mod threaded_archiver;
mod directory_listing; mod directory_listing;
mod threaded_archiver;
mod web; mod web;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let app = clap::Command::new(clap::crate_name!()) let app = clap::Command::new(clap::crate_name!())

View file

@ -13,7 +13,9 @@ type Stream = futures::channel::mpsc::Receiver<bytes::Bytes>;
type Sender = futures::channel::mpsc::Sender<bytes::Bytes>; type Sender = futures::channel::mpsc::Sender<bytes::Bytes>;
pub fn stream_tar_in_thread<P>(path: P) -> Stream pub fn stream_tar_in_thread<P>(path: P) -> Stream
where P: AsRef<Path> + Send + 'static { where
P: AsRef<Path> + Send + 'static,
{
let (writer, stream) = StreamWriter::new(64); let (writer, stream) = StreamWriter::new(64);
thread::spawn(move || { thread::spawn(move || {
@ -49,9 +51,7 @@ impl io::Write for StreamWriter {
} }
fn flush(&mut self) -> io::Result<()> { fn flush(&mut self) -> io::Result<()> {
futures::executor::block_on(async move { futures::executor::block_on(async move { self.tx.flush().await.ok() });
self.tx.flush().await.ok()
});
Ok(()) Ok(())
} }
} }

View file

@ -1,14 +1,14 @@
use actix_web::{get, middleware, web, App, Error, HttpServer, HttpRequest, HttpResponse, Responder};
use actix_files::{Files, NamedFile}; use actix_files::{Files, NamedFile};
use actix_web::{
get, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
};
use futures::StreamExt; use futures::StreamExt;
use std::path::PathBuf; use std::path::PathBuf;
pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> { pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
let root_ = root.clone(); let root_ = root.clone();
let s = HttpServer::new(move || { let s = HttpServer::new(move || {
let static_files = Files::new("/", &root_) let static_files = Files::new("/", &root_)
.show_files_listing() .show_files_listing()
.redirect_to_slash_directory() .redirect_to_slash_directory()
@ -29,18 +29,20 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
} }
#[get("/{tail:.*}.tar")] #[get("/{tail:.*}.tar")]
async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, tail: web::Path<String>) -> impl Responder { async fn handle_tar(
req: HttpRequest,
root: web::Data<PathBuf>,
tail: web::Path<String>,
) -> impl Responder {
let relpath = PathBuf::from(tail.trim_end_matches('/')); let relpath = PathBuf::from(tail.trim_end_matches('/'));
let fullpath = root.join(&relpath) let fullpath = root.join(&relpath).canonicalize().unwrap();
.canonicalize()
.unwrap();
// if a .tar already exists, just return it as-is // if a .tar already exists, just return it as-is
let mut fullpath_tar = fullpath.clone(); let mut fullpath_tar = fullpath.clone();
fullpath_tar.set_extension("tar"); fullpath_tar.set_extension("tar");
if fullpath_tar.is_file() { if fullpath_tar.is_file() {
return NamedFile::open_async(fullpath_tar).await return NamedFile::open_async(fullpath_tar)
.await
.unwrap() .unwrap()
.into_response(&req); .into_response(&req);
} }
@ -49,8 +51,7 @@ async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, tail: web::Path<
return HttpResponse::NotFound().body("Directory not found\n"); return HttpResponse::NotFound().body("Directory not found\n");
} }
let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath) let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath).map(Ok::<_, Error>);
.map(Ok::<_, Error>);
let response = HttpResponse::Ok() let response = HttpResponse::Ok()
.content_type("application/x-tar") .content_type("application/x-tar")
.streaming(stream); .streaming(stream);