Compare commits

...

4 Commits

6 changed files with 105 additions and 22 deletions

View File

@ -274,3 +274,54 @@ impl Html for Image {
) )
} }
} }
#[derive(Debug)]
pub struct UnorderedList {
children: Vec<String>,
attributes: Attributes,
}
impl UnorderedList {
pub fn new() -> Self {
Self {
children: vec![],
attributes: Attributes::default(),
}
}
pub fn with_attributes<'a>(
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
self
}
}
impl Html for UnorderedList {
fn to_html_string(&self) -> String {
let children = self
.children
.iter()
.map(|item| format!("<li>{}</li>", item.to_html_string()))
.collect::<Vec<String>>();
format!(
"<ul {attrs}>
{children}
</ul>",
attrs = self.attributes.to_string(),
children = children.join("\n")
)
}
}
impl HtmlContainer for UnorderedList {
fn add_html<H: Html>(&mut self, html: H) {
self.children.push(html.to_html_string())
}
}

View File

@ -1,6 +1,6 @@
use crate::html::*; use crate::html::*;
use build_html::{self, Container, ContainerType, Html, HtmlContainer}; use build_html::{self, Container, ContainerType, Html, HtmlContainer};
use file_service::{FileHandle, FileId, ReadFileError}; use file_service::{FileHandle, FileInfo, ReadFileError};
pub fn auth(_message: Option<String>) -> build_html::HtmlPage { pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
build_html::HtmlPage::new() build_html::HtmlPage::new()
@ -49,14 +49,7 @@ pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::H
let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]); let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
for handle in handles { for handle in handles {
let container = match handle { let container = match handle {
Ok(ref handle) => thumbnail(&handle.id).with_html( Ok(ref handle) => thumbnail(&handle.info),
Form::new()
.with_path(&format!("/{}", *handle.id))
.with_method("post")
.with_html(Input::new("hidden", "_method").with_value("delete"))
.with_html(Button::new("Delete")),
),
Err(err) => Container::new(ContainerType::Div) Err(err) => Container::new(ContainerType::Div)
.with_attributes(vec![("class", "file")]) .with_attributes(vec![("class", "file")])
.with_paragraph(format!("{:?}", err)), .with_paragraph(format!("{:?}", err)),
@ -88,15 +81,31 @@ pub fn upload_form() -> Form {
) )
} }
pub fn thumbnail(id: &FileId) -> Container { pub fn thumbnail(info: &FileInfo) -> Container {
Container::new(ContainerType::Div) Container::new(ContainerType::Div)
.with_attributes(vec![("class", "card thumbnail")]) .with_attributes(vec![("class", "card thumbnail")])
.with_html( .with_html(
Container::new(ContainerType::Div).with_link( Container::new(ContainerType::Div).with_link(
format!("/{}", **id), format!("/{}", *info.id),
Image::new(&format!("{}/tn", **id)) Image::new(&format!("{}/tn", *info.id))
.with_attributes([("class", "thumbnail__image")]) .with_attributes([("class", "thumbnail__image")])
.to_html_string(), .to_html_string(),
), ),
) )
.with_html(
Container::new(ContainerType::Div)
.with_html(
UnorderedList::new()
.with_attributes(vec![("class", "thumbnail__metadata")])
.with_html(info.name.clone())
.with_html(format!("{}", info.created.format("%Y-%m-%d"))),
)
.with_html(
Form::new()
.with_path(&format!("/{}", *info.id))
.with_method("post")
.with_html(Input::new("hidden", "_method").with_value("delete"))
.with_html(Button::new("Delete")),
),
)
} }

View File

@ -120,8 +120,13 @@ impl FileHandle {
/// Create a new entry in the database /// Create a new entry in the database
pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> { pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> {
let id = FileId::from(Uuid::new_v4().hyphenated().to_string()); let id = FileId::from(Uuid::new_v4().hyphenated().to_string());
let path = PathBuf::from(filename);
let extension = PathBuf::from(filename) let name = path
.file_stem()
.and_then(|s| s.to_str().map(|s| s.to_owned()))
.ok_or(WriteFileError::InvalidPath)?;
let extension = path
.extension() .extension()
.and_then(|s| s.to_str().map(|s| s.to_owned())) .and_then(|s| s.to_str().map(|s| s.to_owned()))
.ok_or(WriteFileError::InvalidPath)?; .ok_or(WriteFileError::InvalidPath)?;
@ -138,6 +143,7 @@ impl FileHandle {
let info = FileInfo { let info = FileInfo {
id: id.clone(), id: id.clone(),
name,
size: 0, size: 0,
created: Utc::now(), created: Utc::now(),
file_type, file_type,
@ -233,6 +239,17 @@ mod test {
); );
} }
#[test]
fn it_creates_file_info() {
let tmp = TempDir::new("var").unwrap();
let handle =
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
assert_eq!(handle.info.name, "rawr");
assert_eq!(handle.info.size, 0);
assert_eq!(handle.info.file_type, "image/png");
assert_eq!(handle.info.extension, "png");
}
#[test] #[test]
fn it_opens_a_file() { fn it_opens_a_file() {
let tmp = TempDir::new("var").unwrap(); let tmp = TempDir::new("var").unwrap();

View File

@ -11,6 +11,12 @@ use std::{
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileInfo { pub struct FileInfo {
pub id: FileId, pub id: FileId,
// Early versions of the application didn't support a name field, so it is possible that
// metadata won't contain the name. We can just default to an empty string when loading the
// metadata, as all future versions will require a filename when the file gets uploaded.
#[serde(default)]
pub name: String,
pub size: usize, pub size: usize,
pub created: DateTime<Utc>, pub created: DateTime<Utc>,
pub file_type: String, pub file_type: String,
@ -50,6 +56,7 @@ mod test {
let info = FileInfo { let info = FileInfo {
id: FileId("temp-id".to_owned()), id: FileId("temp-id".to_owned()),
name: "test-image".to_owned(),
size: 23777, size: 23777,
created, created,
file_type: "image/png".to_owned(), file_type: "image/png".to_owned(),

View File

@ -77,6 +77,10 @@ body {
border: none; border: none;
} }
.thumbnail__metadata {
list-style: none;
}
/* /*
[type="submit"] { [type="submit"] {
border-radius: 1em; border-radius: 1em;

View File

@ -9,18 +9,13 @@ use std::{
rc::Rc, rc::Rc,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub enum Tree<T> { pub enum Tree<T> {
#[default]
Empty, Empty,
Root(Node<T>), Root(Node<T>),
} }
impl<T> Default for Tree<T> {
fn default() -> Self {
Tree::Empty
}
}
impl<T> Tree<T> { impl<T> Tree<T> {
pub fn new(value: T) -> (Tree<T>, Node<T>) { pub fn new(value: T) -> (Tree<T>, Node<T>) {
let node = Node::new(value); let node = Node::new(value);
@ -129,14 +124,14 @@ impl<T> Node<T> {
} }
/// Immutably retrieve the data in this node. /// Immutably retrieve the data in this node.
pub fn value<'a>(&self) -> Ref<T> { pub fn value(&self) -> Ref<T> {
// Ref::map is not actually a member function. I don't know why this was done, other than // Ref::map is not actually a member function. I don't know why this was done, other than
// maybe to avoid conflicting with other `map` declarations. Why that is necessary when // maybe to avoid conflicting with other `map` declarations. Why that is necessary when
// Option::map exists as a member, I don't know. // Option::map exists as a member, I don't know.
Ref::map(self.0.borrow(), |v| &v.value) Ref::map(self.0.borrow(), |v| &v.value)
} }
pub fn children<'a>(&self) -> Ref<Vec<Node<T>>> { pub fn children(&self) -> Ref<Vec<Node<T>>> {
Ref::map(self.0.borrow(), |v| &v.children) Ref::map(self.0.borrow(), |v| &v.children)
} }