Skip to content

Commit

Permalink
Fix #81: include extension in QR code link if available
Browse files Browse the repository at this point in the history
  • Loading branch information
matze committed Jan 14, 2025
1 parent cdb9cf8 commit 2c3eca0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/routes/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub struct PasswordForm {
fn qr_code_from(
state: AppState,
headers: &HeaderMap,
id: &str,
id: String,
ext: Option<String>,
) -> Result<qrcodegen::QrCode, Error> {
let base_url = &state.base_url.map_or_else(
|| {
Expand All @@ -56,8 +57,14 @@ fn qr_code_from(
Ok,
)?;

let name = if let Some(ext) = ext {
format!("{id}.{ext}")
} else {
id
};

Ok(qrcodegen::QrCode::encode_text(
base_url.join(id)?.as_str(),
base_url.join(&name)?.as_str(),
qrcodegen::QrCodeEcc::High,
)?)
}
Expand All @@ -69,7 +76,13 @@ async fn get_qr(
title: String,
) -> Result<pages::Qr<'static>, pages::ErrorResponse<'static>> {
let id = key.id();
let qr_code = tokio::task::spawn_blocking(move || qr_code_from(state, &headers, &id))
let ext = if key.ext.is_empty() {
None
} else {
Some(key.ext.clone())
};

let qr_code = tokio::task::spawn_blocking(move || qr_code_from(state, &headers, id, ext))
.await
.map_err(Error::from)??;

Expand Down Expand Up @@ -267,7 +280,7 @@ pub async fn burn_created(
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))
let qr_code = tokio::task::spawn_blocking(move || qr_code_from(state.0, &headers, id, None))
.await
.map_err(Error::from)??;

Expand Down
2 changes: 1 addition & 1 deletion templates/paste.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</a>
</div>
<div class="nav-item">
<a href="{{ base_path.join(id) }}?fmt=qr" class="nav-button" title="qr code" aria-label="qr code">
<a href="{{ base_path.join(id) }}.{{ ext }}?fmt=qr" class="nav-button" title="qr code" aria-label="qr code">
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linejoin="round" stroke-width="2" d="M4 4h6v6H4V4Zm10 10h6v6h-6v-6Zm0-10h6v6h-6V4Zm-4 10h.01v.01H10V14Zm0 4h.01v.01H10V18Zm-3 2h.01v.01H7V20Zm0-4h.01v.01H7V16Zm-3 2h.01v.01H4V18Zm0-4h.01v.01H4V14Z"/>
<path stroke="currentColor" stroke-linejoin="round" stroke-width="2" d="M7 7h.01v.01H7V7Zm10 10h.01v.01H17V17Z"/>
Expand Down

0 comments on commit 2c3eca0

Please sign in to comment.