Skip to content

Commit

Permalink
Show QR code after a burn paste has been created
Browse files Browse the repository at this point in the history
Since one can not visit the actual page (without deleting it) for
one-time-pastes, there is no other way to get the QR code for URL of the
created paste.
  • Loading branch information
cgzones authored and matze committed Oct 26, 2024
1 parent ba69063 commit e9c8fff
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
26 changes: 18 additions & 8 deletions src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ impl<'a> Encrypted<'a> {
}
}

/// Return module coordinates that are dark.
fn dark_modules(code: &qrcodegen::QrCode) -> Vec<(i32, i32)> {
let size = code.size();
(0..size)
.flat_map(|x| (0..size).map(move |y| (x, y)))
.filter(|(x, y)| code.get_module(*x, *y))
.collect()
}

/// Paste view showing the formatted paste as well as a bunch of links.
#[derive(Template)]
#[template(path = "qr.html", escape = "none")]
Expand All @@ -202,32 +211,33 @@ impl<'a> Qr<'a> {
}
}

// Return module coordinates that are dark.
fn dark_modules(&self) -> Vec<(i32, i32)> {
let size = self.code.size();
(0..size)
.flat_map(|x| (0..size).map(move |y| (x, y)))
.filter(|(x, y)| self.code.get_module(*x, *y))
.collect()
dark_modules(&self.code)
}
}

/// Burn page shown if "burn-after-reading" was selected during insertion.
#[derive(Template)]
#[template(path = "burn.html")]
#[template(path = "burn.html", escape = "none")]
pub struct Burn<'a> {
meta: &'a env::Metadata<'a>,
base_path: &'static env::BasePath,
id: String,
code: qrcodegen::QrCode,
}

impl<'a> Burn<'a> {
/// Construct new burn page linking to `id`.
pub fn new(id: String) -> Self {
pub fn new(code: qrcodegen::QrCode, id: String) -> Self {
Self {
meta: &env::METADATA,
base_path: &env::BASE_PATH,
id,
code,
}
}

fn dark_modules(&self) -> Vec<(i32, i32)> {
dark_modules(&self.code)
}
}
6 changes: 3 additions & 3 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::pages::{Burn, Index};
use crate::pages::Index;
use crate::AppState;
use axum::extract::{Path, State};
use axum::extract::State;
use axum::routing::{get, Router};

mod assets;
Expand All @@ -19,7 +19,7 @@ pub fn routes() -> Router<AppState> {
"/:id",
get(paste::get).post(paste::get).delete(paste::delete),
)
.route("/burn/:id", get(|Path(id)| async { Burn::new(id) }))
.route("/burn/:id", get(paste::burn_created))
.route("/delete/:id", get(paste::delete))
.merge(assets::routes())
}
Expand Down
14 changes: 14 additions & 0 deletions src/routes/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::crypto::Password;
use crate::db::read::Entry;
use crate::env::BASE_PATH;
use crate::highlight::Html;
use crate::pages::Burn;
use crate::routes::{form, json};
use crate::{pages, AppState, Error};
use axum::body::Body;
Expand Down Expand Up @@ -235,3 +236,16 @@ pub async fn delete(

Ok(Redirect::to(BASE_PATH.path()))
}

pub async fn burn_created(
Path(id): Path<String>,
headers: HeaderMap,
state: State<AppState>,
) -> Result<impl IntoResponse, pages::ErrorResponse<'static>> {
let id_clone = id.clone();
let qr_code = tokio::task::spawn_blocking(move || qr_code_from(state.0, &headers, &id))
.await
.map_err(Error::from)??;

Ok(Burn::new(qr_code, id_clone))
}
6 changes: 6 additions & 0 deletions templates/burn.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
<div class="center">
<p class="text-center">Copy and send <a class="punctuation definition tag" href="{{ base_path.join(id) }}">this link</a>.
After opening it for the first time, it will be deleted.</p>
<p class="text-center">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 {{ code.size() + 4 }} {{ code.size() + 4 }}" stroke="none" width="16rem">
<rect width="100%" height="100%" fill="#fafafa"/>
<path d="{% for (x, y) in self.dark_modules() %}M{{ x + 2 }},{{ y + 2 }}h1v1h-1z {% endfor %}" fill="#000000"/>
</svg>
</p>
</div>
{% endblock %}

0 comments on commit e9c8fff

Please sign in to comment.