Store entry html content outside DB in file storage

Since the HTML content can get quite big and can have embeded images.
This commit is contained in:
2023-07-05 23:45:49 -04:00
parent 7289151318
commit 3f028c3088
10 changed files with 81 additions and 37 deletions

View File

@@ -1,8 +1,11 @@
use std::fs;
use axum::extract::{Path, State};
use axum::response::Response;
use maud::{html, PreEscaped};
use sqlx::PgPool;
use crate::config::Config;
use crate::error::Result;
use crate::models::entry::get_entry;
use crate::partials::layout::Layout;
@@ -11,9 +14,12 @@ use crate::uuid::Base62Uuid;
pub async fn get(
Path(id): Path<Base62Uuid>,
State(pool): State<PgPool>,
State(config): State<Config>,
layout: Layout,
) -> Result<Response> {
let entry = get_entry(&pool, id.as_uuid()).await?;
let content_dir = std::path::Path::new(&config.content_dir);
let content_path = content_dir.join(format!("{}.html", entry.entry_id));
Ok(layout.render(html! {
article {
@let title = entry.title.unwrap_or_else(|| "Untitled".to_string());
@@ -25,7 +31,7 @@ pub async fn get(
(published_at)
}
}
@let content = entry.html_content.unwrap_or_else(|| "No content".to_string());
@let content = fs::read_to_string(content_path).unwrap_or_else(|_| "No content".to_string());
(PreEscaped(content))
}
}))