Serve files
This commit is contained in:
parent
145e3c592e
commit
ca52201880
4 changed files with 125 additions and 41 deletions
|
|
@ -14,10 +14,10 @@ pub struct Home {}
|
|||
#[template(path = "repo.html")]
|
||||
pub struct Repo {
|
||||
pub content: String,
|
||||
pub entries: BTreeSet<Entry>,
|
||||
pub root: Directory,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[derive(Clone, Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{% match self %}
|
||||
|
|
@ -44,16 +44,18 @@ pub enum Entry {
|
|||
File(File),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct File {
|
||||
name: String,
|
||||
path: String,
|
||||
pub name: String,
|
||||
pub path: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Directory {
|
||||
name: String,
|
||||
path: String,
|
||||
entries: BTreeMap<String, Entry>,
|
||||
pub name: String,
|
||||
pub path: String,
|
||||
pub entries: BTreeMap<String, Entry>,
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
|
|
@ -78,13 +80,16 @@ impl PartialOrd<Entry> for Entry {
|
|||
}
|
||||
|
||||
impl Directory {
|
||||
/// Insert
|
||||
pub fn insert<'a>(&mut self, entry: Entry, mut path: Peekable<impl Iterator<Item = &'a str>>) {
|
||||
let Some(next_path_level) = path.next() else {
|
||||
warn!("Cannot insert entry: no more path");
|
||||
return;
|
||||
};
|
||||
if path.peek().is_some() {
|
||||
// Directory
|
||||
if let Some(parent) = self.entries.get_mut(next_path_level) {
|
||||
// Directory exists
|
||||
match parent {
|
||||
Entry::Directory(dir) => dir.insert(entry, path),
|
||||
Entry::File(_file) => {
|
||||
|
|
@ -92,10 +97,29 @@ impl Directory {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
warn!("Cannot insert entry: not found");
|
||||
//warn!("Cannot insert entry: not found");
|
||||
// Need to create directory
|
||||
let mut new_dir_path = self.path.clone();
|
||||
new_dir_path.push_str(next_path_level);
|
||||
let mut parent = Directory {
|
||||
name: next_path_level.into(),
|
||||
path: new_dir_path,
|
||||
entries: BTreeMap::new(),
|
||||
};
|
||||
parent.insert(entry, path);
|
||||
self.entries
|
||||
.insert(next_path_level.into(), Entry::Directory(parent));
|
||||
}
|
||||
} else {
|
||||
// File
|
||||
self.entries.insert(entry.name().to_string(), entry);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find<'a>(&self, mut path: impl Iterator<Item = &'a str>) -> Option<&str> {
|
||||
match self.entries.get(path.next()?)? {
|
||||
Entry::Directory(directory) => directory.find(path),
|
||||
Entry::File(file) => Some(&file.hash),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue