append original file name to urls
This commit is contained in:
parent
b0b03f32a4
commit
64c2567918
|
@ -1,6 +1,6 @@
|
||||||
use std::{fmt::Display, str::FromStr};
|
use std::{fmt::Display, str::FromStr};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub(crate) enum FileKind {
|
pub(crate) enum FileKind {
|
||||||
TEXT,
|
TEXT,
|
||||||
BINARY,
|
BINARY,
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -58,7 +58,7 @@ async fn upload(
|
||||||
|
|
||||||
sqlx::query("INSERT INTO Files (file_id, file_name, valid_till, kind) VALUES ($1, $2, $3, $4)")
|
sqlx::query("INSERT INTO Files (file_id, file_name, valid_till, kind) VALUES ($1, $2, $3, $4)")
|
||||||
.bind(&file_id)
|
.bind(&file_id)
|
||||||
.bind(original_name.unwrap_or_else(|| file_id.clone()))
|
.bind(original_name.as_ref().unwrap_or_else(|| &file_id))
|
||||||
.bind(valid_till.naive_local())
|
.bind(valid_till.naive_local())
|
||||||
.bind(kind.to_string())
|
.bind(kind.to_string())
|
||||||
.execute(db.as_ref())
|
.execute(db.as_ref())
|
||||||
|
@ -74,13 +74,25 @@ async fn upload(
|
||||||
|
|
||||||
sender.send(()).await;
|
sender.send(()).await;
|
||||||
|
|
||||||
|
let redirect = if kind == FileKind::BINARY && original_name.is_some() {
|
||||||
|
format!("/upload/{}/{}", file_id, original_name.unwrap())
|
||||||
|
} else {
|
||||||
|
format!("/upload/{}", file_id)
|
||||||
|
};
|
||||||
Ok(HttpResponse::SeeOther()
|
Ok(HttpResponse::SeeOther()
|
||||||
.header("location", format!("/upload/{}", file_id))
|
.header("location", redirect)
|
||||||
.finish())
|
.finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn uploaded(id: web::Path<String>, req: web::HttpRequest) -> Result<HttpResponse, Error> {
|
async fn uploaded(req: web::HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
let url = req.url_for("file", &[id.as_str()])?;
|
let id = req.match_info().query("id");
|
||||||
|
let name = req.match_info().get("name");
|
||||||
|
let conn = req.connection_info();
|
||||||
|
let url = if let Some(name) = name {
|
||||||
|
format!("{}://{}/file/{}/{}", conn.scheme(), conn.host(), id, name)
|
||||||
|
} else {
|
||||||
|
format!("{}://{}/file/{}", conn.scheme(), conn.host(), id)
|
||||||
|
};
|
||||||
let upload_html = UPLOAD_HTML.replace("{url}", url.as_str());
|
let upload_html = UPLOAD_HTML.replace("{url}", url.as_str());
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
|
@ -89,12 +101,12 @@ async fn uploaded(id: web::Path<String>, req: web::HttpRequest) -> Result<HttpRe
|
||||||
|
|
||||||
async fn download(
|
async fn download(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
id: web::Path<String>,
|
|
||||||
db: web::Data<PgPool>,
|
db: web::Data<PgPool>,
|
||||||
config: web::Data<Config>,
|
config: web::Data<Config>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let id = req.match_info().query("id");
|
||||||
let mut cursor = sqlx::query("SELECT file_id, file_name, kind from files WHERE file_id = $1")
|
let mut cursor = sqlx::query("SELECT file_id, file_name, kind from files WHERE file_id = $1")
|
||||||
.bind(id.as_ref())
|
.bind(id)
|
||||||
.fetch(db.as_ref());
|
.fetch(db.as_ref());
|
||||||
let row: PgRow = cursor
|
let row: PgRow = cursor
|
||||||
.next()
|
.next()
|
||||||
|
@ -218,10 +230,12 @@ async fn main() -> std::io::Result<()> {
|
||||||
.data(config.clone())
|
.data(config.clone())
|
||||||
.service(web::resource("/").route(web::get().to(index)))
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
.service(web::resource("/upload").route(web::post().to(upload)))
|
.service(web::resource("/upload").route(web::post().to(upload)))
|
||||||
.service(web::resource("/upload/{id}").route(web::get().to(uploaded)))
|
|
||||||
.service(
|
.service(
|
||||||
web::resource("/file/{id}")
|
web::resource(["/upload/{id}", "/upload/{id}/{name}"])
|
||||||
.name("file")
|
.route(web::get().to(uploaded)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource(["/file/{id}", "/file/{id}/{name}"])
|
||||||
.route(web::get().to(download)),
|
.route(web::get().to(download)),
|
||||||
)
|
)
|
||||||
.service(Files::new("/static", "static").disable_content_disposition())
|
.service(Files::new("/static", "static").disable_content_disposition())
|
||||||
|
|
Loading…
Reference in New Issue