Skip to content

Commit

Permalink
Show File Size in Bytes
Browse files Browse the repository at this point in the history
Add a cli option that show file size in bytes rather than KB or MB, etc.
  • Loading branch information
Lzzzzzt committed Sep 22, 2024
1 parent 869cd65 commit cb0dd89
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ pub struct CliArgs {
/// and return an error instead.
#[arg(short = 'I', long, env = "MINISERVE_DISABLE_INDEXING")]
pub disable_indexing: 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 @@ -149,6 +149,9 @@ pub struct MiniserveConfig {
/// If enabled, indexing is disabled.
pub disable_indexing: 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 @@ -308,6 +311,7 @@ impl MiniserveConfig {
disable_indexing: args.disable_indexing,
tls_rustls_config: tls_rustls_server_config,
compress_response: args.compress_response,
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 @@ -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);
Expand Down Expand Up @@ -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_size_in_byte))
}
}
}
Expand Down Expand Up @@ -180,7 +180,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 @@ -204,7 +204,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 @@ -488,6 +488,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 @@ -520,8 +521,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 @@ -530,7 +537,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

0 comments on commit cb0dd89

Please sign in to comment.