Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Show size in byte #1261

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ pub struct CliArgs {
/// Enable README.md rendering in directories
#[arg(long, env = "MINISERVE_README")]
pub readme: bool,

/// Show served file size in exact bytes.
#[arg(long, default_value = "false", env = "MINISERVE_SHOW_SIZE_IN_BYTE")]
pub show_size_in_byte: bool,
}

/// Checks whether an interface is valid, i.e. it can be parsed into an IP address
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ pub struct MiniserveConfig {
/// If enabled, render the readme from the current directory
pub readme: bool,

/// If enabled, will show in exact byte size of the file
pub show_size_in_byte: bool,

/// If set, use provided rustls config for TLS
#[cfg(feature = "tls")]
pub tls_rustls_config: Option<rustls::ServerConfig>,
Expand Down Expand Up @@ -300,6 +303,7 @@ impl MiniserveConfig {
show_wget_footer: args.show_wget_footer,
readme: args.readme,
tls_rustls_config: tls_rustls_server_config,
show_size_in_byte: args.show_size_in_byte,
})
}
}
25 changes: 18 additions & 7 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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);
Expand Down Expand Up @@ -151,7 +151,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_size_in_byte))
}
}
}
Expand Down Expand Up @@ -181,7 +181,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 {
Expand All @@ -205,7 +205,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_size_in_byte))
}
}
}
Expand Down Expand Up @@ -489,6 +489,7 @@ fn entry_row(
sort_method: Option<SortingMethod>,
sort_order: Option<SortingOrder>,
raw: bool,
show_size_in_byte: bool,
) -> Markup {
html! {
tr {
Expand Down Expand Up @@ -521,8 +522,14 @@ fn entry_row(

@if !raw {
@if let Some(size) = entry.size {
span.mobile-info.size {
(maud::display(size))
@if show_size_in_byte {
span.mobile-info.size {
(maud::display(format!("{}B", size.as_u64())))
}
}@else {
span.mobile-info.size {
(maud::display(size))
}
}
}
}
Expand All @@ -531,7 +538,11 @@ fn entry_row(
}
td.size-cell {
@if let Some(size) = entry.size {
(maud::display(size))
@if show_size_in_byte {
(maud::display(format!("{}B", size.as_u64())))
}@else {
(maud::display(size))
}
}
}
td.date-cell {
Expand Down