Add download qrcode to table

This commit is contained in:
2021-08-12 15:48:02 +02:00
committed by dietrich
parent 6b0daecd31
commit 5886272585
8 changed files with 107 additions and 15 deletions
+1
View File
@@ -357,6 +357,7 @@ pub async fn webservice(
"/create_link/",
web::post().to(views::process_create_link_json),
)
.route("/get_qr_code/", web::post().to(views::get_qr_code_json))
.route(
"/edit_link/",
web::post().to(views::process_update_link_json),
+27 -2
View File
@@ -16,7 +16,7 @@ use qrcode::{render::svg, QrCode};
use queries::{authenticate, Role};
use shared::apirequests::{
general::{Message, Status},
links::{LinkDelta, LinkRequestForm},
links::{LinkDelta, LinkRequestForm, QrCodeRequest, SvgQrCodeResponse},
users::{LoginUser, UserDelta, UserRequestForm},
};
use tera::{Context, Tera};
@@ -130,7 +130,7 @@ pub async fn index_json(
Err(e) => {
error!("Failed to access database: {:?}", e);
warn!("Not logged in - redirecting to login page");
Ok(redirect_builder("/admin/login/"))
Ok(HttpResponse::Unauthorized().body("Failed"))
}
}
}
@@ -188,6 +188,31 @@ pub async fn view_link_empty(
view_link(tera, config, id, web::Path::from("".to_owned())).await
}
pub async fn get_qr_code_json(
config: web::Data<crate::ServerConfig>,
qr_request: web::Json<QrCodeRequest>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
if let Ok(link) = queries::get_link(&id, &qr_request.link_id, &config).await {
let host = config.public_url.to_string();
let qr = QrCode::with_error_correction_level(
&format!("http://{}/{}", &host, &link.item.code),
qrcode::EcLevel::L,
)?;
let svg = qr
.render()
.min_dimensions(100, 100)
.dark_color(svg::Color("#000000"))
.light_color(svg::Color("#ffffff"))
.build();
Ok(HttpResponse::Ok().json2(&SvgQrCodeResponse { svg }))
} else {
Ok(redirect_builder("/admin/login/"))
}
}
#[instrument(skip(id, tera))]
pub async fn view_link(
tera: web::Data<Tera>,
+5
View File
@@ -40,6 +40,7 @@ div.login div {
height: 60px;
display: table;
}
div.login input {
padding: 15px;
margin-bottom: 20px;
@@ -63,6 +64,10 @@ td {
padding: 10px;
}
td.table_qr svg {
max-height: 40px;
}
table tr:nth-child(even) {
background-color: #eee;
}