aboutsummaryrefslogtreecommitdiffstats
path: root/src/listing.rs
diff options
context:
space:
mode:
authorwyhaya <wyhaya@gmail.com>2020-07-05 14:34:43 +0000
committerGitHub <noreply@github.com>2020-07-05 14:34:43 +0000
commit205a5a4cc49e33b86366969a76b3303d71287761 (patch)
tree5c8d73e8380356476cab85b770ab7d983011d639 /src/listing.rs
parentMerge pull request #292 from j2ghz/patch-1 (diff)
downloadminiserve-205a5a4cc49e33b86366969a76b3303d71287761.tar.gz
miniserve-205a5a4cc49e33b86366969a76b3303d71287761.zip
Add generate QR code (#330)
* Add generate QR code * Add --qrcode option
Diffstat (limited to 'src/listing.rs')
-rw-r--r--src/listing.rs23
1 files changed, 23 insertions, 0 deletions
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<SortingMethod>,
pub order: Option<SortingOrder>,
pub theme: Option<ColorScheme>,
+ qrcode: Option<String>,
download: Option<CompressionMethod>,
}
@@ -135,6 +137,7 @@ pub fn directory_listing<S>(
file_upload: bool,
random_route: Option<String>,
default_color_scheme: ColorScheme,
+ show_qrcode: bool,
upload_route: String,
tar_enabled: bool,
zip_enabled: bool,
@@ -163,6 +166,23 @@ pub fn directory_listing<S>(
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<Entry> = Vec::new();
for entry in dir.path.read_dir()? {
@@ -330,6 +350,7 @@ pub fn directory_listing<S>(
query_params.order,
default_color_scheme,
color_scheme,
+ show_qrcode,
file_upload,
&upload_route,
&current_dir.display().to_string(),
@@ -348,6 +369,7 @@ pub fn extract_query_parameters<S>(req: &HttpRequest<S>) -> 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<S>(req: &HttpRequest<S>) -> QueryParameters {
order: None,
download: None,
theme: None,
+ qrcode: None,
path: None,
}
}