cargo fmt
This commit is contained in:
parent
24ee4f3f09
commit
84599e5d82
4 changed files with 23 additions and 24 deletions
|
@ -1,10 +1,10 @@
|
|||
use actix_files::Directory;
|
||||
use actix_web::{HttpRequest, HttpResponse};
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use std::path::Path;
|
||||
use actix_web::{HttpRequest, HttpResponse};
|
||||
use percent_encoding::{utf8_percent_encode, CONTROLS}; // NON_ALPHANUMERIC
|
||||
use v_htmlescape::escape as escape_html_entity;
|
||||
use std::fmt::Write;
|
||||
use std::path::Path;
|
||||
use v_htmlescape::escape as escape_html_entity;
|
||||
|
||||
macro_rules! encode_file_url {
|
||||
($path:ident) => {
|
||||
|
@ -31,9 +31,7 @@ pub fn directory_listing(
|
|||
if dir.is_visible(&entry) {
|
||||
let entry = entry.unwrap();
|
||||
let p = match entry.path().strip_prefix(&dir.path) {
|
||||
Ok(p) if cfg!(windows) => {
|
||||
base.join(p).to_string_lossy().replace("\\", "/")
|
||||
}
|
||||
Ok(p) if cfg!(windows) => base.join(p).to_string_lossy().replace("\\", "/"),
|
||||
Ok(p) => base.join(p).to_string_lossy().into_owned(),
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
@ -66,7 +64,8 @@ pub fn directory_listing(
|
|||
let header = format!(
|
||||
"<h1>Index of {}/</h1>\n\
|
||||
<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!(
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
mod threaded_archiver;
|
||||
mod directory_listing;
|
||||
mod threaded_archiver;
|
||||
mod web;
|
||||
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let app = clap::Command::new(clap::crate_name!())
|
||||
|
|
|
@ -13,7 +13,9 @@ type Stream = futures::channel::mpsc::Receiver<bytes::Bytes>;
|
|||
type Sender = futures::channel::mpsc::Sender<bytes::Bytes>;
|
||||
|
||||
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);
|
||||
|
||||
thread::spawn(move || {
|
||||
|
@ -49,9 +51,7 @@ impl io::Write for StreamWriter {
|
|||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
futures::executor::block_on(async move {
|
||||
self.tx.flush().await.ok()
|
||||
});
|
||||
futures::executor::block_on(async move { self.tx.flush().await.ok() });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
23
src/web.rs
23
src/web.rs
|
@ -1,14 +1,14 @@
|
|||
use actix_web::{get, middleware, web, App, Error, HttpServer, HttpRequest, HttpResponse, Responder};
|
||||
use actix_files::{Files, NamedFile};
|
||||
use actix_web::{
|
||||
get, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
||||
pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
|
||||
let root_ = root.clone();
|
||||
let s = HttpServer::new(move || {
|
||||
|
||||
let static_files = Files::new("/", &root_)
|
||||
.show_files_listing()
|
||||
.redirect_to_slash_directory()
|
||||
|
@ -29,18 +29,20 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
|
|||
}
|
||||
|
||||
#[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 fullpath = root.join(&relpath)
|
||||
.canonicalize()
|
||||
.unwrap();
|
||||
|
||||
let fullpath = root.join(&relpath).canonicalize().unwrap();
|
||||
|
||||
// if a .tar already exists, just return it as-is
|
||||
let mut fullpath_tar = fullpath.clone();
|
||||
fullpath_tar.set_extension("tar");
|
||||
if fullpath_tar.is_file() {
|
||||
return NamedFile::open_async(fullpath_tar).await
|
||||
return NamedFile::open_async(fullpath_tar)
|
||||
.await
|
||||
.unwrap()
|
||||
.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");
|
||||
}
|
||||
|
||||
let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath)
|
||||
.map(Ok::<_, Error>);
|
||||
let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath).map(Ok::<_, Error>);
|
||||
let response = HttpResponse::Ok()
|
||||
.content_type("application/x-tar")
|
||||
.streaming(stream);
|
||||
|
|
Loading…
Add table
Reference in a new issue