2022-10-29 11:11:55 +00:00
use crate ::args ::{ Args , ARGS } ;
2022-11-07 18:28:45 +00:00
use crate ::endpoints ::errors ::ErrorTemplate ;
2022-10-29 11:11:55 +00:00
use crate ::pasta ::Pasta ;
2022-11-07 18:28:45 +00:00
use crate ::util ::animalnumbers ::to_u64 ;
use crate ::util ::hashids ::to_u64 as hashid_to_u64 ;
2022-10-29 11:11:55 +00:00
use crate ::util ::misc ::{ self , remove_expired } ;
use crate ::AppState ;
use actix_web ::{ get , web , HttpResponse } ;
use askama ::Template ;
2022-11-08 21:30:16 +00:00
2022-10-29 11:11:55 +00:00
#[ derive(Template) ]
#[ template(path = " qr.html " , escape = " none " ) ]
struct QRTemplate < ' a > {
qr : & ' a String ,
2022-11-07 18:28:45 +00:00
pasta : & ' a Pasta ,
2022-10-29 11:11:55 +00:00
args : & ' a Args ,
}
#[ get( " /qr/{id} " ) ]
pub async fn getqr ( data : web ::Data < AppState > , id : web ::Path < String > ) -> HttpResponse {
2022-11-07 18:28:45 +00:00
// get access to the pasta collection
let mut pastas = data . pastas . lock ( ) . unwrap ( ) ;
let u64_id = if ARGS . hash_ids {
hashid_to_u64 ( & id ) . unwrap_or ( 0 )
} else {
to_u64 ( & id ) . unwrap_or ( 0 )
} ;
2022-10-29 11:11:55 +00:00
2022-11-07 18:28:45 +00:00
// remove expired pastas (including this one if needed)
remove_expired ( & mut pastas ) ;
2022-10-29 11:11:55 +00:00
2022-11-07 18:28:45 +00:00
// find the index of the pasta in the collection based on u64 id
let mut index : usize = 0 ;
let mut found : bool = false ;
for ( i , pasta ) in pastas . iter ( ) . enumerate ( ) {
if pasta . id = = u64_id {
index = i ;
found = true ;
break ;
2022-10-29 11:11:55 +00:00
}
2022-11-07 18:28:45 +00:00
}
if found {
// generate the QR code as an SVG - if its a file or text pastas, this will point to the /pasta endpoint, otherwise to the /url endpoint, essentially directly taking the user to the url stored in the pasta
let svg : String = match pastas [ index ] . pasta_type . as_str ( ) {
" url " = > misc ::string_to_qr_svg ( format! ( " {} /url/ {} " , & ARGS . public_path , & id ) . as_str ( ) ) ,
_ = > misc ::string_to_qr_svg ( format! ( " {} /pasta/ {} " , & ARGS . public_path , & id ) . as_str ( ) ) ,
} ;
// serve qr code in template
return HttpResponse ::Ok ( ) . content_type ( " text/html " ) . body (
QRTemplate {
qr : & svg ,
pasta : & pastas [ index ] ,
args : & ARGS ,
}
. render ( )
. unwrap ( ) ,
) ;
}
// otherwise
// send pasta not found error
HttpResponse ::Ok ( )
. content_type ( " text/html " )
. body ( ErrorTemplate { args : & ARGS } . render ( ) . unwrap ( ) )
2022-10-29 11:11:55 +00:00
}