From 23df30be5491b76d3caf07b4015225a18bfbbaea Mon Sep 17 00:00:00 2001 From: Sven-Hendrik Haase Date: Wed, 25 Aug 2021 04:50:12 +0200 Subject: Upgrade qrcodegen (fixes #568) --- src/listing.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 43cfb0e..ef2ed8b 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -244,9 +244,9 @@ pub fn directory_listing( let res = match QrCode::encode_text(&url, QrCodeEcc::Medium) { Ok(qr) => HttpResponse::Ok() .header("Content-Type", "image/svg+xml") - .body(qr.to_svg_string(2)), + .body(qr_to_svg_string(&qr, 2)), Err(err) => { - log::error!("URL is too long: {:?}", err); + log::error!("URL is invalid (too long?): {:?}", err); HttpResponse::UriTooLong().body(Body::Empty) } }; @@ -452,3 +452,34 @@ pub fn extract_query_parameters(req: &HttpRequest) -> QueryParameters { } } } + +// Returns a string of SVG code for an image depicting +// the given QR Code, with the given number of border modules. +// The string always uses Unix newlines (\n), regardless of the platform. +fn qr_to_svg_string(qr: &QrCode, border: i32) -> String { + assert!(border >= 0, "Border must be non-negative"); + let mut result = String::new(); + result += "\n"; + result += "\n"; + let dimension = qr + .size() + .checked_add(border.checked_mul(2).unwrap()) + .unwrap(); + result += &format!( + "\n", dimension); + result += "\t\n"; + result += "\t\n"; + result += "\n"; + result +} -- cgit v1.2.3