optimize template storage handlers

This commit is contained in:
yggverse 2025-06-25 00:58:30 +03:00
parent 8a11e420ee
commit 95f5f77d18
3 changed files with 33 additions and 17 deletions

View file

@ -42,20 +42,30 @@ nexy -p /path/to/public_dir
--template-access-denied <TEMPLATE_ACCESS_DENIED> --template-access-denied <TEMPLATE_ACCESS_DENIED>
Absolute path to the `Access denied` template file Absolute path to the `Access denied` template file
* this template file can be in binary format (e.g. image)
--template-internal-server-error <TEMPLATE_INTERNAL_SERVER_ERROR> --template-internal-server-error <TEMPLATE_INTERNAL_SERVER_ERROR>
Absolute path to the `Internal server error` template file Absolute path to the `Internal server error` template file
* this template file can be in binary format (e.g. image)
--template-not-found <TEMPLATE_NOT_FOUND> --template-not-found <TEMPLATE_NOT_FOUND>
Absolute path to the `Not found` template file Absolute path to the `Not found` template file
* this template file can be in binary format (e.g. image)
--template-welcome <TEMPLATE_WELCOME> --template-welcome <TEMPLATE_WELCOME>
Absolute path to the `Welcome` template file. Unlike `template_index`, this applies only to the `public` location Absolute path to the `Welcome` template file. Unlike `template-index`, this applies only to the `public` location
* this template file expects pattern and cannot be in binary format
**Patterns** * `{list}` - entries list for the `public` directory **Patterns** * `{list}` - entries list for the `public` directory
--template-index <TEMPLATE_INDEX> --template-index <TEMPLATE_INDEX>
Absolute path to the `Index` template file for each directory Absolute path to the `Index` template file for each directory
* this template file expects pattern and cannot be in binary format
**Patterns** * `{list}` - entries list for the current directory **Patterns** * `{list}` - entries list for the current directory
-r, --read-chunk <READ_CHUNK> -r, --read-chunk <READ_CHUNK>

View file

@ -28,19 +28,27 @@ pub struct Config {
pub public: String, pub public: String,
/// Absolute path to the `Access denied` template file /// Absolute path to the `Access denied` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)] #[arg(long)]
pub template_access_denied: Option<String>, pub template_access_denied: Option<String>,
/// Absolute path to the `Internal server error` template file /// Absolute path to the `Internal server error` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)] #[arg(long)]
pub template_internal_server_error: Option<String>, pub template_internal_server_error: Option<String>,
/// Absolute path to the `Not found` template file /// Absolute path to the `Not found` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)] #[arg(long)]
pub template_not_found: Option<String>, pub template_not_found: Option<String>,
/// Absolute path to the `Welcome` template file. /// Absolute path to the `Welcome` template file.
/// Unlike `template_index`, this applies only to the `public` location /// Unlike `template-index`, this applies only to the `public` location
///
/// * this template file expects pattern and cannot be in binary format
/// ///
/// **Patterns** /// **Patterns**
/// * `{list}` - entries list for the `public` directory /// * `{list}` - entries list for the `public` directory
@ -49,6 +57,8 @@ pub struct Config {
/// Absolute path to the `Index` template file for each directory /// Absolute path to the `Index` template file for each directory
/// ///
/// * this template file expects pattern and cannot be in binary format
///
/// **Patterns** /// **Patterns**
/// * `{list}` - entries list for the current directory /// * `{list}` - entries list for the current directory
#[arg(long)] #[arg(long)]

View file

@ -1,21 +1,21 @@
pub struct Template { pub struct Template {
access_denied: Vec<u8>, access_denied: Vec<u8>,
index: Vec<u8>, index: String,
internal_server_error: Vec<u8>, internal_server_error: Vec<u8>,
not_found: Vec<u8>, not_found: Vec<u8>,
welcome: Vec<u8>, welcome: String,
} }
impl Template { impl Template {
pub fn init(config: &crate::config::Config) -> anyhow::Result<Self> { pub fn init(config: &crate::config::Config) -> anyhow::Result<Self> {
use std::fs::read; use std::fs::{read, read_to_string};
Ok(Self { Ok(Self {
access_denied: match config.template_access_denied { access_denied: match config.template_access_denied {
Some(ref p) => read(p)?, Some(ref p) => read(p)?,
None => "Access denied".into(), None => "Access denied".into(),
}, },
index: match config.template_access_denied { index: match config.template_access_denied {
Some(ref p) => read(p)?, Some(ref p) => read_to_string(p)?,
None => "{list}".into(), None => "{list}".into(),
}, },
internal_server_error: match config.template_access_denied { internal_server_error: match config.template_access_denied {
@ -27,7 +27,7 @@ impl Template {
None => "Not found".into(), None => "Not found".into(),
}, },
welcome: match config.template_access_denied { welcome: match config.template_access_denied {
Some(ref p) => read(p)?, Some(ref p) => read_to_string(p)?,
None => "Welcome to Nexy!\n{list}".into(), None => "Welcome to Nexy!\n{list}".into(),
}, },
}) })
@ -38,11 +38,9 @@ impl Template {
} }
pub fn index(&self, list: Option<&str>) -> Vec<u8> { pub fn index(&self, list: Option<&str>) -> Vec<u8> {
let l = list.unwrap_or_default(); self.index
match std::str::from_utf8(&self.index) { .replace("{list}", list.unwrap_or_default())
Ok(s) => s.replace("{list}", l).into(), .into()
Err(_) => l.into(),
}
} }
pub fn internal_server_error(&self) -> &[u8] { pub fn internal_server_error(&self) -> &[u8] {
@ -54,10 +52,8 @@ impl Template {
} }
pub fn welcome(&self, list: Option<&str>) -> Vec<u8> { pub fn welcome(&self, list: Option<&str>) -> Vec<u8> {
let l = list.unwrap_or_default(); self.welcome
match std::str::from_utf8(&self.welcome) { .replace("{list}", list.unwrap_or_default())
Ok(s) => s.replace("{list}", l).into(), .into()
Err(_) => l.into(),
}
} }
} }