diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2025-03-06 23:17:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-06 23:17:51 +0000 |
commit | 1472a05b1c0c6b59af974235d8462f757cf1b0d8 (patch) | |
tree | 2aa07ddcba76d083583c75ff0be86147425630e3 /src | |
parent | Add CHANGELOG entry for #1431 (diff) | |
parent | change cli args (diff) | |
download | miniserve-1472a05b1c0c6b59af974235d8462f757cf1b0d8.tar.gz miniserve-1472a05b1c0c6b59af974235d8462f757cf1b0d8.zip |
Merge pull request #1261 from Lzzzzzt/show-size-in-byte
Feature: Show size in byte
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 20 | ||||
-rw-r--r-- | src/config.rs | 9 | ||||
-rw-r--r-- | src/renderer.rs | 25 |
3 files changed, 47 insertions, 7 deletions
diff --git a/src/args.rs b/src/args.rs index 72ade7b..ee9cb7e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,3 +1,4 @@ +use std::fmt::Display; use std::net::IpAddr; use std::path::PathBuf; @@ -15,6 +16,21 @@ pub enum MediaType { Video, } +#[derive(ValueEnum, Clone)] +pub enum SizeDisplay { + Human, + Exact, +} + +impl Display for SizeDisplay { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SizeDisplay::Human => write!(f, "human"), + SizeDisplay::Exact => write!(f, "exact"), + } + } +} + #[derive(Parser)] #[command(name = "miniserve", author, about, version)] pub struct CliArgs { @@ -350,6 +366,10 @@ pub struct CliArgs { /// Currently incompatible with -P|--no-symlinks (see https://github.com/messense/dav-server-rs/issues/37) #[arg(long, env = "MINISERVE_ENABLE_WEBDAV", conflicts_with = "no_symlinks")] pub enable_webdav: bool, + + /// Show served file size in exact bytes. + #[arg(long, default_value_t = SizeDisplay::Human, env = "MINISERVE_SIZE_DISPLAY")] + pub size_display: SizeDisplay, } /// Checks whether an interface is valid, i.e. it can be parsed into an IP address diff --git a/src/config.rs b/src/config.rs index 4404959..379f7a7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -158,6 +158,9 @@ pub struct MiniserveConfig { /// If enabled, respond to WebDAV requests (read-only). pub webdav_enabled: bool, + /// If enabled, will show in exact byte size of the file + pub show_exact_bytes: bool, + /// If set, use provided rustls config for TLS #[cfg(feature = "tls")] pub tls_rustls_config: Option<rustls::ServerConfig>, @@ -278,6 +281,11 @@ impl MiniserveConfig { .transpose()? .unwrap_or_default(); + let show_exact_bytes = match args.size_display { + crate::args::SizeDisplay::Human => false, + crate::args::SizeDisplay::Exact => true, + }; + Ok(Self { verbose: args.verbose, path: args.path.unwrap_or_else(|| PathBuf::from(".")), @@ -320,6 +328,7 @@ impl MiniserveConfig { webdav_enabled: args.enable_webdav, tls_rustls_config: tls_rustls_server_config, compress_response: args.compress_response, + show_exact_bytes, }) } } diff --git a/src/renderer.rs b/src/renderer.rs index fae3751..af6d3b5 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -32,7 +32,7 @@ pub fn page( ) -> Markup { // If query_params.raw is true, we want render a minimal directory listing if query_params.raw.is_some() && query_params.raw.unwrap() { - return raw(entries, is_root); + return raw(entries, is_root, conf); } let upload_route = format!("{}/upload", &conf.route_prefix); @@ -150,7 +150,7 @@ pub fn page( } } @for entry in entries { - (entry_row(entry, sort_method, sort_order, false)) + (entry_row(entry, sort_method, sort_order, false, conf.show_exact_bytes)) } } } @@ -214,7 +214,7 @@ pub fn page( } /// Renders the file listing -pub fn raw(entries: Vec<Entry>, is_root: bool) -> Markup { +pub fn raw(entries: Vec<Entry>, is_root: bool, conf: &MiniserveConfig) -> Markup { html! { (DOCTYPE) html { @@ -238,7 +238,7 @@ pub fn raw(entries: Vec<Entry>, is_root: bool) -> Markup { } } @for entry in entries { - (entry_row(entry, None, None, true)) + (entry_row(entry, None, None, true, conf.show_exact_bytes)) } } } @@ -522,6 +522,7 @@ fn entry_row( sort_method: Option<SortingMethod>, sort_order: Option<SortingOrder>, raw: bool, + show_exact_bytes: bool, ) -> Markup { html! { tr { @@ -554,13 +555,19 @@ fn entry_row( @if !raw { @if let Some(size) = entry.size { - span.mobile-info.size { - (build_link("size", &format!("{}", size), sort_method, sort_order)) + @if show_exact_bytes { + span.mobile-info.size { + (maud::display(format!("{}B", size.as_u64()))) + } + }@else { + span.mobile-info.size { + (build_link("size", &format!("{}", size), sort_method, sort_order)) } } @if let Some(modification_timer) = humanize_systemtime(entry.last_modification_date) { span.mobile-info.history { (build_link("date", &modification_timer, sort_method, sort_order)) + } } } } @@ -569,7 +576,11 @@ fn entry_row( } td.size-cell { @if let Some(size) = entry.size { - (maud::display(size)) + @if show_exact_bytes { + (maud::display(format!("{}B", size.as_u64()))) + }@else { + (maud::display(size)) + } } } td.date-cell { |