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

feat/issue-306-pr #965

Open
wants to merge 5 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
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ clap_complete = "4"
clap_mangen = "0.2"
comrak = { version = "0.15", default-features = false }
fast_qr = { version = "0.6", features = ["svg"] }
fs_extra = "1.2.0"
futures = "0.3"
get_if_addrs = "0.5"
hex = "0.4"
Expand All @@ -43,6 +44,7 @@ log = "0.4"
maud = "0.24"
mime = "0.3"
nanoid = "0.4"
once_cell = "1.16.0"
percent-encoding = "2"
port_check = "0.1"
regex = "1"
Expand Down
75 changes: 67 additions & 8 deletions src/listing.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#![allow(clippy::format_push_string)]
use std::collections::HashMap;
use std::fmt::Display;
use std::io;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use actix_web::{dev::ServiceResponse, web::Query, HttpMessage, HttpRequest, HttpResponse};
use bytesize::ByteSize;
use comrak::{markdown_to_html, ComrakOptions};
use log::warn;
use once_cell::sync::OnceCell;
use percent_encoding::{percent_decode_str, utf8_percent_encode};
use regex::Regex;
use serde::Deserialize;
Expand All @@ -18,6 +23,8 @@ use crate::renderer;

use self::percent_encode_sets::PATH_SEGMENT;

static FILE_SIZE_CACHE: OnceCell<Arc<Mutex<HashMap<PathBuf, u64>>>> = OnceCell::new();

/// "percent-encode sets" as defined by WHATWG specs:
/// https://url.spec.whatwg.org/#percent-encoded-bytes
mod percent_encode_sets {
Expand Down Expand Up @@ -78,6 +85,24 @@ pub enum EntryType {
File,
}

#[derive(Clone, Copy, PartialEq, Eq)]
/// Possible entry size types
pub enum EntrySize {
/// EntryCount is number of entries in a directory
EntryCount(usize),
/// Bytes is number of bytes in a file
Bytes(bytesize::ByteSize),
}

impl Display for EntrySize {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EntrySize::EntryCount(count) => write!(f, "{}", count),
EntrySize::Bytes(bytes) => write!(f, "{}", bytes),
}
}
}

/// Entry
pub struct Entry {
/// Name of the entry
Expand All @@ -89,8 +114,8 @@ pub struct Entry {
/// URL of the entry
pub link: String,

/// Size in byte of the entry. Only available for EntryType::File
pub size: Option<bytesize::ByteSize>,
/// Size of the entry
pub size: EntrySize,

/// Last modification date
pub last_modification_date: Option<SystemTime>,
Expand All @@ -104,7 +129,7 @@ impl Entry {
name: String,
entry_type: EntryType,
link: String,
size: Option<bytesize::ByteSize>,
size: EntrySize,
last_modification_date: Option<SystemTime>,
symlink_info: Option<String>,
) -> Self {
Expand Down Expand Up @@ -256,12 +281,36 @@ pub fn directory_listing(
Err(_) => None,
};

let cache_map = FILE_SIZE_CACHE
.get_or_init(|| {
println!("init cache_map");
Arc::default()
})
.clone();
let size = {
match fs_extra::dir::get_size(&entry.path()) {
Err(_) => EntrySize::EntryCount({
std::fs::read_dir(entry.path())
.into_iter()
.take(500_000)
.count()
}),
Ok(result) => {
if let Ok(mut lock) = cache_map.lock() {
lock.insert(entry.path().to_path_buf(), result);
} else {
warn!("Failed to write to cache");
};
EntrySize::Bytes(ByteSize::b(result))
}
}
};
if metadata.is_dir() {
entries.push(Entry::new(
file_name,
EntryType::Directory,
file_url,
None,
size,
last_modification_date,
symlink_dest,
));
Expand All @@ -270,7 +319,7 @@ pub fn directory_listing(
file_name.clone(),
EntryType::File,
file_url,
Some(ByteSize::b(metadata.len())),
EntrySize::Bytes(ByteSize::b(metadata.len())),
last_modification_date,
symlink_dest,
));
Expand Down Expand Up @@ -302,9 +351,19 @@ pub fn directory_listing(
SortingMethod::Size => entries.sort_by(|e1, e2| {
// If we can't get the size of the entry (directory for instance)
// let's consider it's 0b
e2.size
.unwrap_or_else(|| ByteSize::b(0))
.cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
match (e1.size, e2.size) {
(EntrySize::EntryCount(ref e1_count), EntrySize::EntryCount(ref e2_count)) => {
e2_count.cmp(e1_count)
}
(EntrySize::Bytes(ref e1_bytes), EntrySize::Bytes(ref e2_bytes)) => {
e2_bytes.cmp(e1_bytes)
}
(EntrySize::EntryCount(_), EntrySize::Bytes(_)) => std::cmp::Ordering::Greater,
(EntrySize::Bytes(_), EntrySize::EntryCount(_)) => std::cmp::Ordering::Less,
}
// e2.size
// .unwrap_or_else(|| ByteSize::b(0))
// .cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
Comment on lines +364 to +366
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this.

}),
SortingMethod::Date => entries.sort_by(|e1, e2| {
// If, for some reason, we can't get the last modification date of an entry
Expand Down
18 changes: 11 additions & 7 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,19 +515,23 @@ fn entry_row(
}

@if !raw {
@if let Some(size) = entry.size {
span.mobile-info.size {
(maud::display(size))
}
span.mobile-info.size {
(maud::display(&entry.size))
}
// @if let size = entry.size {
// span.mobile-info.size {
// (maud::display(size))
// }
// }
}
}
}
}
td.size-cell {
@if let Some(size) = entry.size {
(maud::display(size))
}
(maud::display(&entry.size))
// @if let size = entry.size {
// (maud::display(size))
// }
Comment on lines +521 to +534
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the commented code here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't remove that since it was the code that would need to be switched on if we were going to throw everything behind a feature flag.

}
td.date-cell {
@if let Some(modification_date) = convert_to_local(entry.last_modification_date) {
Expand Down