Skip to content

Commit

Permalink
Fix #126: use title for download if available
Browse files Browse the repository at this point in the history
... and also set headers instead of appending them. Appending makes no
sense here.
  • Loading branch information
matze committed Mar 3, 2025
1 parent 89b215d commit d01be12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
- Use 64-bit integers that render as 11-character identifier from now on.
Existing 32-bit/6-character identifiers continue to work. Based on a PR #71 by
@cgzones.
- Use title as downloaded filename instead of identifier if one is available.
- Serve all CSS assets under hashed URL to avoid caching issues and set
cache-control header to one month and the `immutable` attribute.
- Do not offer any interactions for burn-after-reading pastes that will end up
with a 404 anyway.

### Fixes
### Fixed

- Sort syntax list in case insensitive manner.
- Wrong background color for inline code in Markdown.
Expand Down
20 changes: 12 additions & 8 deletions src/handlers/download.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cache::Key;
use crate::db::read::Entry;
use crate::db::read::{Data, Entry};
use crate::handlers::extract::{Password, Theme};
use crate::handlers::html::{ErrorResponse, PasswordInput, make_error};
use crate::{Database, Error, Page};
use axum::extract::{Path, State};
use axum::http::header;
use axum::response::{AppendHeaders, IntoResponse, Response};
use axum::response::{IntoResponse, Response};
use axum_extra::headers::HeaderValue;

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

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

fn get_download(text: String, id: &str, extension: &str) -> impl IntoResponse {
fn get_download(key: &Key, data: Data) -> impl IntoResponse {
let filename = data
.title
.unwrap_or_else(|| format!("{}.{}", key.id(), key.ext));

let content_type = "text; charset=utf-8";
let content_disposition =
HeaderValue::from_str(&format!(r#"attachment; filename="{id}.{extension}"#))
HeaderValue::from_str(&format!(r#"attachment; filename="{filename}"#))
.expect("constructing valid header value");

(
AppendHeaders([
[
(header::CONTENT_TYPE, HeaderValue::from_static(content_type)),
(header::CONTENT_DISPOSITION, content_disposition),
]),
text,
],
data.text,
)
}

Expand Down

0 comments on commit d01be12

Please sign in to comment.