11use crate :: cache:: Key ;
2- use crate :: db:: read:: Entry ;
2+ use crate :: db:: read:: { Data , Entry } ;
33use crate :: handlers:: extract:: { Password , Theme } ;
44use crate :: handlers:: html:: { ErrorResponse , PasswordInput , make_error} ;
55use crate :: { Database , Error , Page } ;
66use axum:: extract:: { Path , State } ;
77use axum:: http:: header;
8- use axum:: response:: { AppendHeaders , IntoResponse , Response } ;
8+ use axum:: response:: { IntoResponse , Response } ;
99use 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