This commit is contained in:
maurice committed 2026-11-14 17:54:29 +01:00
1 parent 139404b70f
commit 35e7259775
21 files changed
+590 -70

No files matched your search

+7 -40
View File
@@ -2,7 +2,7 @@ use askama::Template;
use askama_web::WebTemplate;
use axum::{
extract::{Path, State},
http::{HeaderMap, StatusCode, header::ACCEPT},
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use irosashi::{CodeToHtmlOptions, Highlighter};
@@ -10,6 +10,8 @@ use sqlx::SqlitePool;
use crate::paste::{Paste, PasteType};
use super::{db_error, return_html};
#[derive(Template, WebTemplate)]
#[template(path = "note.html")]
pub struct NotePage {
@@ -21,39 +23,12 @@ pub async fn get_note(
Path(id): Path<String>,
headers: HeaderMap,
) -> Result<Response, StatusCode> {
let mut transaction = db
.begin()
let paste = Paste::read(&db, &id, PasteType::TextV1)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let paste = sqlx::query_as::<_, Paste>(
"UPDATE notes SET views = views + 1
WHERE type = ? AND id = ?
AND (expiration IS NULL OR expiration > unixepoch())
AND (max_views IS NULL OR views < max_views)
RETURNING *",
)
.bind(PasteType::TextV1)
.bind(id)
.fetch_optional(&mut *transaction)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::NOT_FOUND)?;
.map_err(db_error)?
.ok_or(StatusCode::NOT_FOUND)?;
if paste.max_views.is_some_and(|max| paste.views >= max) {
sqlx::query("DELETE FROM notes WHERE id = ?")
.bind(&paste.id)
.execute(&mut *transaction)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
}
transaction
.commit()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if return_html(headers) {
if return_html(&headers) {
let highlighter = Highlighter::new().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let code = highlighter
.code_to_html(
@@ -69,11 +44,3 @@ pub async fn get_note(
Ok(paste.content.into_response())
}
}
fn return_html(headers: HeaderMap) -> bool {
headers
.get(ACCEPT)
.and_then(|v| v.to_str().ok())
.map(|h| h.contains("text/html"))
.unwrap_or_default()
}