Use actix url generator for the uploaded url

This commit is contained in:
Fionera 2020-08-03 00:07:16 +02:00
parent 3c05a725c2
commit 64be9b7f0c
2 changed files with 7 additions and 15 deletions

View File

@ -4,13 +4,7 @@ mod multipart;
use actix_files::{Files, NamedFile};
use actix_multipart::Multipart;
use actix_web::{
error,
http::header::{ContentDisposition, DispositionParam, DispositionType},
middleware,
web::{self, Bytes},
App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer,
};
use actix_web::{error, http::header::{ContentDisposition, DispositionParam, DispositionType}, middleware, web::{self, Bytes}, App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer};
use async_std::{
fs,
path::PathBuf,
@ -79,10 +73,10 @@ async fn upload(
.finish())
}
async fn uploaded(id: web::Path<String>, config: web::Data<Config>) -> Result<HttpResponse, Error> {
async fn uploaded(id: web::Path<String>, req: web::HttpRequest) -> Result<HttpResponse, Error> {
let url = req.url_for("file", &[id.as_str()])?;
let upload_html = UPLOAD_HTML
.replace("{id}", id.as_ref())
.replace("{server}", &config.server_url);
.replace("{url}", url.as_str());
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(upload_html))
@ -175,7 +169,6 @@ async fn setup_db() -> PgPool {
#[derive(Clone)]
struct Config {
server_url: String,
files_dir: PathBuf,
}
@ -188,7 +181,6 @@ async fn main() -> std::io::Result<()> {
let pool: PgPool = setup_db().await;
let config = Config {
server_url: env::var("SERVER_URL").unwrap_or_else(|_| "http://localhost:8000".to_owned()),
files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())),
};
fs::create_dir_all(&config.files_dir)
@ -223,7 +215,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/upload").route(web::post().to(upload)))
.service(web::resource("/upload/{id}").route(web::get().to(uploaded)))
.service(web::resource("/file/{id}").route(web::get().to(download)))
.service(web::resource("/file/{id}").name("file").route(web::get().to(download)))
.service(Files::new("/static", "static").disable_content_disposition())
}
})

View File

@ -11,8 +11,8 @@
<h1><a href="/">datatrash</a></h1>
<p>
Datei-Link:
<a href="{server}/file/{id}">
{server}/files/{id}
<a href="{url}">
{url}
</a>
</p>
</main>