11use crate :: cache:: Key ;
22use crate :: crypto:: Password ;
3- use crate :: db:: read:: { Data , Entry } ;
3+ use crate :: db:: read:: Entry ;
44use crate :: handlers:: html:: { make_error, ErrorResponse , PasswordInput } ;
55use crate :: highlight:: Html ;
66use crate :: { Cache , Database , Error , Highlighter , Page } ;
@@ -26,7 +26,6 @@ pub struct Paste {
2626 title : String ,
2727}
2828
29- #[ expect( clippy:: too_many_arguments) ]
3029pub async fn get (
3130 State ( cache) : State < Cache > ,
3231 State ( page) : State < Page > ,
@@ -54,18 +53,32 @@ pub async fn get(
5453 Err ( err) => return Err ( err) ,
5554 } ;
5655
57- Ok ( get_html (
58- page. clone ( ) ,
59- cache,
60- highlighter,
61- key,
62- data,
63- can_be_cached,
64- jar,
65- password. is_some ( ) ,
66- )
67- . await
68- . into_response ( ) )
56+ let can_be_deleted = jar
57+ . get ( "uid" )
58+ . map ( |cookie| cookie. value ( ) . parse :: < i64 > ( ) )
59+ . transpose ( )
60+ . map_err ( |err| Error :: CookieParsing ( err. to_string ( ) ) ) ?
61+ . zip ( data. uid )
62+ . is_some_and ( |( user_uid, owner_uid) | user_uid == owner_uid) ;
63+
64+ let title = data. title . clone ( ) . unwrap_or_default ( ) ;
65+
66+ let html = if let Some ( html) = cache. get ( & key) {
67+ tracing:: trace!( ?key, "found cached item" ) ;
68+
69+ html
70+ } else {
71+ let html = highlighter. highlight ( data, key. ext . clone ( ) ) . await ?;
72+
73+ if can_be_cached && password. is_none ( ) {
74+ tracing:: trace!( ?key, "cache item" ) ;
75+ cache. put ( key. clone ( ) , html. clone ( ) ) ;
76+ }
77+
78+ html
79+ } ;
80+
81+ Ok ( Paste :: new ( key, html, can_be_deleted, title, page. clone ( ) ) . into_response ( ) )
6982 }
7083 . await
7184 . map_err ( |err| make_error ( err, page) )
@@ -86,50 +99,6 @@ impl Paste {
8699 }
87100}
88101
89- #[ expect( clippy:: too_many_arguments) ]
90- async fn get_html (
91- page : Page ,
92- cache : Cache ,
93- highlighter : Highlighter ,
94- key : Key ,
95- data : Data ,
96- can_be_cached : bool ,
97- jar : SignedCookieJar ,
98- is_protected : bool ,
99- ) -> Result < impl IntoResponse , ErrorResponse > {
100- async {
101- let can_delete = jar
102- . get ( "uid" )
103- . map ( |cookie| cookie. value ( ) . parse :: < i64 > ( ) )
104- . transpose ( )
105- . map_err ( |err| Error :: CookieParsing ( err. to_string ( ) ) ) ?
106- . zip ( data. uid )
107- . is_some_and ( |( user_uid, owner_uid) | user_uid == owner_uid) ;
108-
109- if let Some ( html) = cache. get ( & key) {
110- tracing:: trace!( ?key, "found cached item" ) ;
111-
112- let title = data. title . unwrap_or_default ( ) ;
113- return Ok ( Paste :: new ( key, html, can_delete, title, page. clone ( ) ) . into_response ( ) ) ;
114- }
115-
116- // TODO: turn this upside-down, i.e. cache it but only return a cached version if we were able
117- // to decrypt the content. Highlighting is probably still much slower than decryption.
118- let ext = key. ext . clone ( ) ;
119- let title = data. title . clone ( ) . unwrap_or_default ( ) ;
120- let html = highlighter. highlight ( data, ext) . await ?;
121-
122- if can_be_cached && !is_protected {
123- tracing:: trace!( ?key, "cache item" ) ;
124- cache. put ( key. clone ( ) , html. clone ( ) ) ;
125- }
126-
127- Ok ( Paste :: new ( key, html, can_delete, title, page. clone ( ) ) . into_response ( ) )
128- }
129- . await
130- . map_err ( |err| make_error ( err, page) )
131- }
132-
133102#[ cfg( test) ]
134103mod tests {
135104 use crate :: test_helpers:: Client ;
0 commit comments