Compare commits
No commits in common. "ee348c29cbd87f6480bddb0139316af25216596e" and "c2e34db79c0f3f357c0845ec63202d670ed057e1" have entirely different histories.
ee348c29cb
...
c2e34db79c
|
@ -274,54 +274,3 @@ 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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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, FileInfo, ReadFileError};
|
use file_service::{FileHandle, FileId, 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,7 +49,14 @@ 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.info),
|
Ok(ref handle) => thumbnail(&handle.id).with_html(
|
||||||
|
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)),
|
||||||
|
@ -81,31 +88,15 @@ pub fn upload_form() -> Form {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn thumbnail(info: &FileInfo) -> Container {
|
pub fn thumbnail(id: &FileId) -> 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!("/{}", *info.id),
|
format!("/{}", **id),
|
||||||
Image::new(&format!("{}/tn", *info.id))
|
Image::new(&format!("{}/tn", **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")),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,13 +120,8 @@ 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 name = path
|
let extension = PathBuf::from(filename)
|
||||||
.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)?;
|
||||||
|
@ -143,7 +138,6 @@ 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,
|
||||||
|
@ -239,17 +233,6 @@ 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, Some("rawr".to_owned()));
|
|
||||||
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();
|
||||||
|
|
|
@ -11,12 +11,6 @@ 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,
|
||||||
|
@ -56,7 +50,6 @@ 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(),
|
||||||
|
|
|
@ -77,10 +77,6 @@ body {
|
||||||
border: none;
|
border: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thumbnail__metadata {
|
|
||||||
list-style: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
[type="submit"] {
|
[type="submit"] {
|
||||||
border-radius: 1em;
|
border-radius: 1em;
|
||||||
|
|
Loading…
Reference in New Issue