From 8543d2d61fbafde6222e9b95f9604b41570db477 Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Wed, 20 Jul 2022 16:26:08 +0800
Subject: Switch to `qrcode` lib
---
src/listing.rs | 41 +++++------------------------------------
1 file changed, 5 insertions(+), 36 deletions(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index 82b4cdb..f90c40c 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -9,14 +9,14 @@ use actix_web::{HttpMessage, HttpRequest, HttpResponse};
use bytesize::ByteSize;
use comrak::{markdown_to_html, ComrakOptions};
use percent_encoding::{percent_decode_str, utf8_percent_encode};
-use qrcodegen::{QrCode, QrCodeEcc};
+use qrcode::QrCode;
use serde::Deserialize;
use strum_macros::{Display, EnumString};
use crate::archive::ArchiveMethod;
use crate::auth::CurrentUser;
use crate::errors::{self, ContextualError};
-use crate::renderer;
+use crate::{consts, renderer};
use self::percent_encode_sets::PATH_SEGMENT;
@@ -220,10 +220,10 @@ pub fn directory_listing(
// 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) {
+ let res = match QrCode::with_error_correction_level(url, consts::QR_EC_LEVEL) {
Ok(qr) => HttpResponse::Ok()
- .append_header(("Content-Type", "image/svg+xml"))
- .body(qr_to_svg_string(&qr, 2)),
+ .content_type("text/html; charset=utf-8")
+ .body(renderer::qr_code_page(&qr).into_string()),
Err(err) => {
log::error!("URL is invalid (too long?): {:?}", err);
HttpResponse::UriTooLong().finish()
@@ -407,34 +407,3 @@ 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";
- result
-}
--
cgit v1.2.3
From 3d86dcdb7143dc5675eab82edcec69fd3cf69805 Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Thu, 21 Jul 2022 14:33:37 +0800
Subject: Use `mime` for content type
---
src/listing.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index f90c40c..b201ebb 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -222,7 +222,7 @@ pub fn directory_listing(
if let Some(url) = query_params.qrcode {
let res = match QrCode::with_error_correction_level(url, consts::QR_EC_LEVEL) {
Ok(qr) => HttpResponse::Ok()
- .content_type("text/html; charset=utf-8")
+ .content_type(mime::TEXT_HTML_UTF_8)
.body(renderer::qr_code_page(&qr).into_string()),
Err(err) => {
log::error!("URL is invalid (too long?): {:?}", err);
--
cgit v1.2.3
From a99d8bdcadf4f822bb86a2710e5d73783ac7097a Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Tue, 26 Jul 2022 12:27:14 +0800
Subject: Move QR code page style to `style.scss`
---
src/listing.rs | 23 ++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index b201ebb..66e541c 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -154,10 +154,7 @@ pub async fn file_handler(req: HttpRequest) -> actix_web::Result io::Result {
+pub fn directory_listing(dir: &actix_files::Directory, req: &HttpRequest) -> io::Result {
let extensions = req.extensions();
let current_user: Option<&CurrentUser> = extensions.get::();
@@ -198,8 +195,7 @@ pub fn directory_listing(
}
Component::Normal(s) => {
name = s.to_string_lossy().to_string();
- link_accumulator
- .push_str(&(utf8_percent_encode(&name, PATH_SEGMENT).to_string() + "/"));
+ link_accumulator.push_str(&(utf8_percent_encode(&name, PATH_SEGMENT).to_string() + "/"));
}
_ => name = "".to_string(),
};
@@ -223,7 +219,7 @@ pub fn directory_listing(
let res = match QrCode::with_error_correction_level(url, consts::QR_EC_LEVEL) {
Ok(qr) => HttpResponse::Ok()
.content_type(mime::TEXT_HTML_UTF_8)
- .body(renderer::qr_code_page(&qr).into_string()),
+ .body(renderer::qr_code_page(&breadcrumbs, &qr, conf).into_string()),
Err(err) => {
log::error!("URL is invalid (too long?): {:?}", err);
HttpResponse::UriTooLong().finish()
@@ -301,9 +297,9 @@ pub fn directory_listing(
}
match query_params.sort.unwrap_or(SortingMethod::Name) {
- SortingMethod::Name => entries.sort_by(|e1, e2| {
- alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase())
- }),
+ SortingMethod::Name => {
+ entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase()))
+ }
SortingMethod::Size => entries.sort_by(|e1, e2| {
// If we can't get the size of the entry (directory for instance)
// let's consider it's 0b
@@ -371,10 +367,7 @@ pub fn directory_listing(
.content_type(archive_method.content_type())
.append_header(archive_method.content_encoding())
.append_header(("Content-Transfer-Encoding", "binary"))
- .append_header((
- "Content-Disposition",
- format!("attachment; filename={:?}", file_name),
- ))
+ .append_header(("Content-Disposition", format!("attachment; filename={:?}", file_name)))
.body(actix_web::body::BodyStream::new(rx)),
))
} else {
@@ -386,7 +379,7 @@ pub fn directory_listing(
readme,
is_root,
query_params,
- breadcrumbs,
+ &breadcrumbs,
&encoded_dir,
conf,
current_user,
--
cgit v1.2.3
From 4fca60cc5b06fff1aa7f9e52b774d868dc7bfe1a Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Mon, 22 Aug 2022 19:07:24 +0800
Subject: Fix rustfmt complaints
---
src/listing.rs | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index 66e541c..bc8dcfb 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -154,7 +154,10 @@ pub async fn file_handler(req: HttpRequest) -> actix_web::Result io::Result {
+pub fn directory_listing(
+ dir: &actix_files::Directory,
+ req: &HttpRequest,
+) -> io::Result {
let extensions = req.extensions();
let current_user: Option<&CurrentUser> = extensions.get::();
@@ -195,7 +198,8 @@ pub fn directory_listing(dir: &actix_files::Directory, req: &HttpRequest) -> io:
}
Component::Normal(s) => {
name = s.to_string_lossy().to_string();
- link_accumulator.push_str(&(utf8_percent_encode(&name, PATH_SEGMENT).to_string() + "/"));
+ link_accumulator
+ .push_str(&(utf8_percent_encode(&name, PATH_SEGMENT).to_string() + "/"));
}
_ => name = "".to_string(),
};
@@ -297,9 +301,9 @@ pub fn directory_listing(dir: &actix_files::Directory, req: &HttpRequest) -> io:
}
match query_params.sort.unwrap_or(SortingMethod::Name) {
- SortingMethod::Name => {
- entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase()))
- }
+ SortingMethod::Name => entries.sort_by(|e1, e2| {
+ alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase())
+ }),
SortingMethod::Size => entries.sort_by(|e1, e2| {
// If we can't get the size of the entry (directory for instance)
// let's consider it's 0b
@@ -367,7 +371,10 @@ pub fn directory_listing(dir: &actix_files::Directory, req: &HttpRequest) -> io:
.content_type(archive_method.content_type())
.append_header(archive_method.content_encoding())
.append_header(("Content-Transfer-Encoding", "binary"))
- .append_header(("Content-Disposition", format!("attachment; filename={:?}", file_name)))
+ .append_header((
+ "Content-Disposition",
+ format!("attachment; filename={:?}", file_name),
+ ))
.body(actix_web::body::BodyStream::new(rx)),
))
} else {
--
cgit v1.2.3
From 9f7d5a8aeaf252fb51d88b965b3a324e8e815f37 Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Thu, 1 Sep 2022 19:37:39 +0800
Subject: Fix drop-down QR code
---
src/listing.rs | 26 +++++++++-----------------
1 file changed, 9 insertions(+), 17 deletions(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index bc8dcfb..b000649 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -9,14 +9,13 @@ use actix_web::{HttpMessage, HttpRequest, HttpResponse};
use bytesize::ByteSize;
use comrak::{markdown_to_html, ComrakOptions};
use percent_encoding::{percent_decode_str, utf8_percent_encode};
-use qrcode::QrCode;
use serde::Deserialize;
use strum_macros::{Display, EnumString};
use crate::archive::ArchiveMethod;
use crate::auth::CurrentUser;
use crate::errors::{self, ContextualError};
-use crate::{consts, renderer};
+use crate::renderer;
use self::percent_encode_sets::PATH_SEGMENT;
@@ -38,7 +37,6 @@ pub struct QueryParameters {
pub order: Option,
pub raw: Option,
pub mkdir_name: Option,
- qrcode: Option,
download: Option,
}
@@ -166,6 +164,13 @@ pub fn directory_listing(
let base = Path::new(serve_path);
let random_route_abs = format!("/{}", conf.route_prefix);
+ let abs_url = format!(
+ "{}://{}{}",
+ req.connection_info().scheme(),
+ req.connection_info().host(),
+ req.uri()
+ );
+ dbg!(&abs_url);
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs);
let encoded_dir = match base.strip_prefix(random_route_abs) {
@@ -218,20 +223,6 @@ 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::with_error_correction_level(url, consts::QR_EC_LEVEL) {
- Ok(qr) => HttpResponse::Ok()
- .content_type(mime::TEXT_HTML_UTF_8)
- .body(renderer::qr_code_page(&breadcrumbs, &qr, conf).into_string()),
- Err(err) => {
- log::error!("URL is invalid (too long?): {:?}", err);
- HttpResponse::UriTooLong().finish()
- }
- };
- return Ok(ServiceResponse::new(req.clone(), res));
- }
-
let mut entries: Vec = Vec::new();
let mut readme: Option<(String, String)> = None;
@@ -384,6 +375,7 @@ pub fn directory_listing(
renderer::page(
entries,
readme,
+ abs_url,
is_root,
query_params,
&breadcrumbs,
--
cgit v1.2.3
From f762672b098ec7963748530bccd78f16f42ab9d3 Mon Sep 17 00:00:00 2001
From: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
Date: Thu, 1 Sep 2022 20:13:37 +0800
Subject: Remove leftover `dbg!`
---
src/listing.rs | 1 -
1 file changed, 1 deletion(-)
(limited to 'src/listing.rs')
diff --git a/src/listing.rs b/src/listing.rs
index b000649..c0104b1 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -170,7 +170,6 @@ pub fn directory_listing(
req.connection_info().host(),
req.uri()
);
- dbg!(&abs_url);
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs);
let encoded_dir = match base.strip_prefix(random_route_abs) {
--
cgit v1.2.3