enable the .tar handler

This commit is contained in:
Дамјан Георгиевски 2020-09-14 23:44:00 +02:00
parent 070a878c75
commit 98fbec274c
2 changed files with 29 additions and 22 deletions

View file

@ -1,4 +1,4 @@
// mod threaded_archiver; mod threaded_archiver;
mod listing; mod listing;
mod web; mod web;

View file

@ -1,8 +1,7 @@
use actix_files::Files; use actix_web::{get, error, middleware, web, App, Error, HttpServer, HttpRequest, HttpResponse, Responder};
use actix_web::{get, middleware, web, App, HttpServer, HttpResponse, Responder}; use actix_web::http::StatusCode;
use actix_files::{Files, NamedFile};
use futures::StreamExt;
// use crate::threaded_archiver;
use std::path::PathBuf; use std::path::PathBuf;
@ -12,12 +11,12 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
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()
.files_listing_renderer(crate::listing::directory_listing); .files_listing_renderer(crate::listing::directory_listing);
App::new() App::new()
.app_data(root_.clone()) .data(root_.clone())
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(favicon_ico) .service(favicon_ico)
.service(handle_tar) .service(handle_tar)
@ -31,20 +30,28 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
} }
#[get("/{tail:.*}.tar")] #[get("/{tail:.*}.tar")]
async fn handle_tar(_root: web::Data<PathBuf>, web::Path(_tail): web::Path<String>) -> impl Responder { async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, web::Path(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).canonicalize()?; let fullpath = root.join(&relpath).canonicalize()
.map_err(|err| error::InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR))?;
// if !(fullpath.is_dir()) { if fullpath.is_file() {
// return Err(error::ErrorBadRequest("not a directory")); return NamedFile::open(fullpath)
// } .map_err(|err| error::InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR))?
.into_response(&req);
}
// let stream = threaded_archiver::stream_tar_in_thread(fullpath); if !(fullpath.is_dir()) {
// let resp = HttpResponse::Ok() return Ok(HttpResponse::NotFound().body("Directory not found"));
// .content_type("application/x-tar") }
// .streaming(stream.map_err(|_e| error::ErrorBadRequest("stream error")));
// Ok(resp) let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath)
HttpResponse::Ok() .map(|item| Ok::<_, Error>(item));
let res = HttpResponse::Ok()
.content_type("application/x-tar")
.streaming(stream);
Ok(res)
} }
const FAVICON_ICO: &'static [u8] = include_bytes!("favicon.png"); const FAVICON_ICO: &'static [u8] = include_bytes!("favicon.png");