Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dhat-profiling = ["dhat"]
anyhow = "1.0.75"
async-stream = "0.3.5"
async-trait = "0.1.72"
chrono = "0.4.31"
chrono = { version = "0.4.31", features = ["serde"] }
dhat = { version = "0.2.4", optional = true }
futures = "0.3.28"
flate2 = "1.0.26"
Expand Down
9 changes: 7 additions & 2 deletions src/addon/file_server/directory_entry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Local};
use serde::Serialize;
use std::cmp::{Ord, Ordering};

Expand All @@ -11,8 +12,10 @@ pub struct DirectoryEntry {
pub(crate) size: String,
pub(crate) len: u64,
pub(crate) entry_path: String,
pub(crate) created_at: String,
pub(crate) updated_at: String,
pub(crate) date_created_string: String,
pub(crate) date_modified_string: String,
pub(crate) date_created_exact: DateTime<Local>,
pub(crate) date_modified_exact: DateTime<Local>,
}

impl Ord for DirectoryEntry {
Expand Down Expand Up @@ -62,4 +65,6 @@ pub struct DirectoryIndex {
pub(crate) breadcrumbs: Vec<BreadcrumbItem>,
pub(crate) sort_by_name: bool,
pub(crate) sort_by_size: bool,
pub(crate) sort_by_date_created: bool,
pub(crate) sort_by_date_modified: bool,
}
53 changes: 44 additions & 9 deletions src/addon/file_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod http_utils;
mod query_params;
mod scoped_file_system;

use chrono::Local;
pub use file::{File, FILE_BUFFER_SIZE};
pub use scoped_file_system::{Directory, Entry, ScopedFileSystem};

Expand All @@ -18,7 +19,7 @@ use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

use crate::utils::fmt::{format_bytes, format_system_date};
use crate::utils::fmt::format_bytes;
use crate::utils::url_encode::{decode_uri, encode_uri, PERCENT_ENCODE_SET};

use self::directory_entry::{BreadcrumbItem, DirectoryEntry, DirectoryIndex};
Expand Down Expand Up @@ -224,16 +225,18 @@ impl<'a> FileServer {
for entry in entries {
let entry = entry.context("Unable to read entry")?;
let metadata = entry.metadata()?;
let created_at = if let Ok(time) = metadata.created() {
format_system_date(time)
let date_created_exact = if let Ok(time) = metadata.created() {
time.into()
} else {
String::default()
Local::now()
};
let updated_at = if let Ok(time) = metadata.modified() {
format_system_date(time)
let date_created_string = date_created_exact.format("%Y/%m/%d %H:%M:%S").to_string();
let date_modified_exact = if let Ok(time) = metadata.modified() {
time.into()
} else {
String::default()
Local::now()
};
let date_modified_string = date_modified_exact.format("%Y/%m/%d %H:%M:%S").to_string();

directory_entries.push(DirectoryEntry {
display_name: entry
Expand All @@ -245,8 +248,10 @@ impl<'a> FileServer {
size: format_bytes(metadata.len() as f64),
len: metadata.len(),
entry_path: FileServer::make_dir_entry_link(&root_dir, &entry.path()),
created_at,
updated_at,
date_created_exact,
date_created_string,
date_modified_exact,
date_modified_string,
});
}

Expand All @@ -261,6 +266,8 @@ impl<'a> FileServer {
breadcrumbs,
sort_by_name: true,
sort_by_size: false,
sort_by_date_created: false,
sort_by_date_modified: false,
});
}
SortBy::Size => {
Expand All @@ -271,6 +278,32 @@ impl<'a> FileServer {
breadcrumbs,
sort_by_name: false,
sort_by_size: true,
sort_by_date_created: false,
sort_by_date_modified: false,
});
}
SortBy::DateCreated => {
directory_entries.sort_by_key(|entry| entry.date_created_exact);

return Ok(DirectoryIndex {
entries: directory_entries,
breadcrumbs,
sort_by_name: false,
sort_by_size: false,
sort_by_date_created: true,
sort_by_date_modified: false,
});
}
SortBy::DateModified => {
directory_entries.sort_by_key(|entry| entry.date_modified_exact);

return Ok(DirectoryIndex {
entries: directory_entries,
breadcrumbs,
sort_by_name: false,
sort_by_size: false,
sort_by_date_created: false,
sort_by_date_modified: true,
});
}
}
Expand All @@ -284,6 +317,8 @@ impl<'a> FileServer {
breadcrumbs,
sort_by_name: false,
sort_by_size: false,
sort_by_date_created: false,
sort_by_date_modified: false,
})
}

Expand Down
8 changes: 6 additions & 2 deletions src/addon/file_server/query_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::str::FromStr;
pub enum SortBy {
Name,
Size,
DateCreated,
DateModified,
}

impl FromStr for SortBy {
Expand All @@ -16,8 +18,10 @@ impl FromStr for SortBy {
let lower = lower.as_str();

match lower {
"name" => Ok(SortBy::Name),
"size" => Ok(SortBy::Size),
"name" => Ok(Self::Name),
"size" => Ok(Self::Size),
"date_created" => Ok(Self::DateCreated),
"date_modified" => Ok(Self::DateModified),
_ => Err(Error::msg("Value doesnt correspond")),
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/addon/file_server/template/explorer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,17 @@
{{else}}
<a class="hcol" href="?sort_by=size">Size</a>
{{/if}}
<a class="hcol">Date created</a>
<a class="hcol">Date modified</a>
{{#if sort_by_date_created}}
<a class="hcol" href="?sort_by=">Date created&nbsp;↓</a>
{{else}}
<a class="hcol" href="?sort_by=date_created">Date created</a>
{{/if}}
{{#if sort_by_date_modified}}
<a class="hcol" href="?sort_by=">Date modified&nbsp;↓</a>
{{else}}
<a class="hcol" href="?sort_by=date_modified">Date modified</a>
{{/if}}

</li>
{{#each entries}}
<li class="brow">
Expand All @@ -215,8 +224,8 @@
</span>
<span class="bcol">{{display_name}}</span>
<span class="bcol">{{size}}</span>
<span class="bcol">{{created_at}}</span>
<span class="bcol">{{updated_at}}</span>
<span class="bcol">{{date_created_string}}</span>
<span class="bcol">{{date_modified_string}}</span>
</a>
</li>
{{/each}}
Expand Down
19 changes: 0 additions & 19 deletions src/utils/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use chrono::prelude::*;
use chrono::{DateTime, Local};
use std::time::SystemTime;

/// Byte size units
const BYTE_SIZE_UNIT: [&str; 9] = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

Expand All @@ -18,21 +14,6 @@ pub fn format_bytes(bytes: f64) -> String {
format!("{:.2} {}", value, BYTE_SIZE_UNIT[i as usize])
}

/// Formats a `SystemTime` into a YYYY/MM/DD HH:MM:SS time `String`
pub fn format_system_date(system_time: SystemTime) -> String {
let datetime: DateTime<Local> = DateTime::from(system_time);

format!(
"{}/{:0>2}/{:0>2} {:0>2}:{:0>2}:{:0>2}",
datetime.year(),
datetime.month(),
datetime.day(),
datetime.hour(),
datetime.minute(),
datetime.second()
)
}

#[cfg(test)]
mod tests {
use std::vec;
Expand Down