Skip to content

Commit 023e232

Browse files
committed
Fix #126: use title for download if available
... and also set headers instead of appending them. Appending makes no sense here.
1 parent 89b215d commit 023e232

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/handlers/download.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::cache::Key;
2-
use crate::db::read::Entry;
2+
use crate::db::read::{Data, Entry};
33
use crate::handlers::extract::{Password, Theme};
44
use crate::handlers::html::{ErrorResponse, PasswordInput, make_error};
55
use crate::{Database, Error, Page};
66
use axum::extract::{Path, State};
77
use axum::http::header;
8-
use axum::response::{AppendHeaders, IntoResponse, Response};
8+
use axum::response::{IntoResponse, Response};
99
use axum_extra::headers::HeaderValue;
1010

1111
/// GET handler for raw content of a paste.
@@ -22,7 +22,7 @@ pub async fn get(
2222

2323
match db.get(key.id, password).await {
2424
Ok(Entry::Regular(data) | Entry::Burned(data)) => {
25-
Ok(get_download(data.text, &key.id(), &key.ext).into_response())
25+
Ok(get_download(&key, data).into_response())
2626
}
2727
Ok(Entry::Expired) => Err(Error::NotFound),
2828
Err(Error::NoPassword) => Ok(PasswordInput {
@@ -38,18 +38,22 @@ pub async fn get(
3838
.map_err(|err| make_error(err, page, theme))
3939
}
4040

41-
fn get_download(text: String, id: &str, extension: &str) -> impl IntoResponse {
41+
fn get_download(key: &Key, data: Data) -> impl IntoResponse {
42+
let filename = data
43+
.title
44+
.unwrap_or_else(|| format!("{}.{}", key.id(), key.ext));
45+
4246
let content_type = "text; charset=utf-8";
4347
let content_disposition =
44-
HeaderValue::from_str(&format!(r#"attachment; filename="{id}.{extension}"#))
48+
HeaderValue::from_str(&format!(r#"attachment; filename="{filename}"#))
4549
.expect("constructing valid header value");
4650

4751
(
48-
AppendHeaders([
52+
[
4953
(header::CONTENT_TYPE, HeaderValue::from_static(content_type)),
5054
(header::CONTENT_DISPOSITION, content_disposition),
51-
]),
52-
text,
55+
],
56+
data.text,
5357
)
5458
}
5559

0 commit comments

Comments
 (0)