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>
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>
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>
Absolute path to the `Not found` template file
* this template file can be in binary format (e.g. image)
--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
--template-index <TEMPLATE_INDEX>
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
-r, --read-chunk <READ_CHUNK>

View file

@ -28,19 +28,27 @@ pub struct Config {
pub public: String,
/// Absolute path to the `Access denied` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)]
pub template_access_denied: Option<String>,
/// Absolute path to the `Internal server error` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)]
pub template_internal_server_error: Option<String>,
/// Absolute path to the `Not found` template file
///
/// * this template file can be in binary format (e.g. image)
#[arg(long)]
pub template_not_found: Option<String>,
/// 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**
/// * `{list}` - entries list for the `public` directory
@ -49,6 +57,8 @@ pub struct Config {
/// 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
#[arg(long)]

View file

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