From 205a5a4cc49e33b86366969a76b3303d71287761 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Sun, 5 Jul 2020 22:34:43 +0800 Subject: Add generate QR code (#330) * Add generate QR code * Add --qrcode option --- src/listing.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 9ba596a..6181c3a 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -9,6 +9,7 @@ use std::io; use std::path::{Path, PathBuf}; use std::time::SystemTime; use strum_macros::{Display, EnumString}; +use qrcodegen::{QrCode, QrCodeEcc}; use crate::archive::CompressionMethod; use crate::errors::{self, ContextualError}; @@ -24,6 +25,7 @@ pub struct QueryParameters { pub sort: Option, pub order: Option, pub theme: Option, + qrcode: Option, download: Option, } @@ -135,6 +137,7 @@ pub fn directory_listing( file_upload: bool, random_route: Option, default_color_scheme: ColorScheme, + show_qrcode: bool, upload_route: String, tar_enabled: bool, zip_enabled: bool, @@ -163,6 +166,23 @@ pub fn directory_listing( let query_params = extract_query_parameters(req); + // If the `qrcode` parameter is included in the url, then should respond to the QR code + if let Some(url) = query_params.qrcode { + 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)) + }, + Err(err) => { + log::error!("URL is too long: {:?}", err); + HttpResponse::UriTooLong() + .body(Body::Empty) + } + }; + return Ok(res) + } + let mut entries: Vec = Vec::new(); for entry in dir.path.read_dir()? { @@ -330,6 +350,7 @@ pub fn directory_listing( query_params.order, default_color_scheme, color_scheme, + show_qrcode, file_upload, &upload_route, ¤t_dir.display().to_string(), @@ -348,6 +369,7 @@ pub fn extract_query_parameters(req: &HttpRequest) -> QueryParameters { order: query.order, download: query.download, theme: query.theme, + qrcode: query.qrcode.to_owned(), path: query.path.clone(), }, Err(e) => { @@ -358,6 +380,7 @@ pub fn extract_query_parameters(req: &HttpRequest) -> QueryParameters { order: None, download: None, theme: None, + qrcode: None, path: None, } } -- cgit v1.2.3