@@ -11,13 +11,13 @@ use libs::sha2::{digest, Sha256, Sha384, Sha512};
1111use libs:: tera:: { from_value, to_value, Function as TeraFn , Result , Value } ;
1212use utils:: site:: resolve_internal_link;
1313
14- fn compute_hash < D : digest:: Digest > ( literal : String , as_base64 : bool ) -> String
14+ fn compute_hash < D : digest:: Digest > ( data : & [ u8 ] , as_base64 : bool ) -> String
1515where
1616 digest:: Output < D > : core:: fmt:: LowerHex ,
1717 D : std:: io:: Write ,
1818{
1919 let mut hasher = D :: new ( ) ;
20- hasher. update ( literal ) ;
20+ hasher. update ( data ) ;
2121 if as_base64 {
2222 standard_b64. encode ( hasher. finalize ( ) )
2323 } else {
@@ -127,11 +127,9 @@ impl TeraFn for GetUrl {
127127 . map_err ( |e| format ! ( "`get_url`: {}" , e) ) ?
128128 . and_then ( |( p, _) | fs:: File :: open ( p) . ok ( ) )
129129 . and_then ( |mut f| {
130- let mut contents = String :: new ( ) ;
131-
132- f. read_to_string ( & mut contents) . ok ( ) ?;
133-
134- Some ( compute_hash :: < Sha256 > ( contents, false ) )
130+ let mut contents = Vec :: new ( ) ;
131+ f. read_to_end ( & mut contents) . ok ( ) ?;
132+ Some ( compute_hash :: < Sha256 > ( & contents, false ) )
135133 } ) {
136134 Some ( hash) => {
137135 let shorthash = & hash[ ..20 ] ; // 2^-80 chance of false positive
@@ -202,16 +200,16 @@ impl TeraFn for GetHash {
202200 }
203201 } ;
204202
205- let mut f = match std :: fs:: File :: open ( file_path) {
203+ let mut f = match fs:: File :: open ( file_path) {
206204 Ok ( f) => f,
207205 Err ( e) => {
208206 return Err ( format ! ( "File {} could not be open: {}" , path_v, e) . into ( ) ) ;
209207 }
210208 } ;
211209
212- let mut contents = String :: new ( ) ;
210+ let mut contents = Vec :: new ( ) ;
213211
214- match f. read_to_string ( & mut contents) {
212+ match f. read_to_end ( & mut contents) {
215213 Ok ( f) => f,
216214 Err ( e) => {
217215 return Err ( format ! ( "File {} could not be read: {}" , path_v, e) . into ( ) ) ;
@@ -220,7 +218,7 @@ impl TeraFn for GetHash {
220218
221219 contents
222220 }
223- ( None , Some ( literal_v) ) => literal_v,
221+ ( None , Some ( literal_v) ) => literal_v. into_bytes ( ) ,
224222 } ;
225223
226224 let sha_type = optional_arg ! (
@@ -235,9 +233,9 @@ impl TeraFn for GetHash {
235233 . unwrap_or ( true ) ;
236234
237235 let hash = match sha_type {
238- 256 => compute_hash :: < Sha256 > ( contents, base64) ,
239- 384 => compute_hash :: < Sha384 > ( contents, base64) ,
240- 512 => compute_hash :: < Sha512 > ( contents, base64) ,
236+ 256 => compute_hash :: < Sha256 > ( & contents, base64) ,
237+ 384 => compute_hash :: < Sha384 > ( & contents, base64) ,
238+ 512 => compute_hash :: < Sha512 > ( & contents, base64) ,
241239 _ => return Err ( "`get_hash`: Invalid sha value" . into ( ) ) ,
242240 } ;
243241
@@ -250,7 +248,7 @@ mod tests {
250248 use super :: { GetHash , GetUrl } ;
251249
252250 use std:: collections:: HashMap ;
253- use std:: fs:: create_dir;
251+ use std:: fs:: { copy , create_dir} ;
254252 use std:: path:: PathBuf ;
255253
256254 use libs:: tera:: { to_value, Function } ;
@@ -293,6 +291,16 @@ title = "A title"
293291 static_fn. call( & args) . unwrap( ) ,
294292 "http://a-website.com/app.css?h=572e691dc68c3fcd653a"
295293 ) ;
294+
295+ // And binary files as well
296+ copy ( "gutenberg.jpg" , dir. path ( ) . join ( "gutenberg.jpg" ) ) . unwrap ( ) ;
297+ let mut args = HashMap :: new ( ) ;
298+ args. insert ( "path" . to_string ( ) , to_value ( "gutenberg.jpg" ) . unwrap ( ) ) ;
299+ args. insert ( "cachebust" . to_string ( ) , to_value ( true ) . unwrap ( ) ) ;
300+ assert_eq ! (
301+ static_fn. call( & args) . unwrap( ) ,
302+ "http://a-website.com/gutenberg.jpg?h=93fff9d0ecde9b119c0c"
303+ ) ;
296304 }
297305
298306 #[ test]
0 commit comments