Compare commits
No commits in common. "3294262d8d6f66e3d4b9a83b75a5e10cd56d4c4f" and "6c1fa7cc2fe3ada396aa3963af927700b107b294" have entirely different histories.
3294262d8d
...
6c1fa7cc2f
|
@ -692,7 +692,6 @@ dependencies = [
|
|||
"serde 1.0.188",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"tempdir",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"uuid 0.4.0",
|
||||
|
|
|
@ -32,4 +32,4 @@ log = { version = "0.4" }
|
|||
bytes = { version = "1" }
|
||||
futures-util = { version = "0.3" }
|
||||
cool_asserts = { version = "2" }
|
||||
tempdir = { version = "0.3" }
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{"id":"rawr.png","size":23777,"created":"2020-06-04T16:04:10.085680927Z","file_type":"image/png","hash":"b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee"}
|
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
|
@ -1,3 +1,11 @@
|
|||
/*
|
||||
use iron::headers;
|
||||
use iron::middleware::Handler;
|
||||
use iron::modifiers::{Header, Redirect};
|
||||
use iron::prelude::*;
|
||||
use iron::response::BodyReader;
|
||||
use iron::status;
|
||||
*/
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
|
@ -6,8 +14,9 @@ use http::status::StatusCode;
|
|||
// use orizentic::{Permissions, ResourceName, Secret};
|
||||
use build_html::Html;
|
||||
use bytes::Buf;
|
||||
use futures_util::StreamExt;
|
||||
use futures_util::{Stream, StreamExt};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::Read,
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
|
@ -15,9 +24,9 @@ use std::{
|
|||
};
|
||||
use warp::{filters::multipart::Part, Filter};
|
||||
|
||||
// mod cookies;
|
||||
mod cookies;
|
||||
mod html;
|
||||
// mod middleware;
|
||||
mod middleware;
|
||||
mod pages;
|
||||
mod store;
|
||||
|
||||
|
@ -421,7 +430,7 @@ pub async fn main() {
|
|||
.with(log),
|
||||
);
|
||||
*/
|
||||
let server = warp::serve(root.or(upload).or(thumbnail).or(file).or(delete).with(log));
|
||||
let server = warp::serve(root.or(upload).with(log));
|
||||
server
|
||||
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
|
||||
.await;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
html::*,
|
||||
store::{FileHandle, FileId, ReadFileError},
|
||||
store::{FileHandle, FileId, ReadFileError, Thumbnail},
|
||||
};
|
||||
use build_html::{self, Container, ContainerType, Html, HtmlContainer};
|
||||
|
||||
|
|
|
@ -21,34 +21,27 @@ pub enum PathError {
|
|||
pub struct PathResolver {
|
||||
base: PathBuf,
|
||||
id: FileId,
|
||||
extension: String,
|
||||
}
|
||||
|
||||
impl PathResolver {
|
||||
pub fn new(base: &Path, id: FileId, extension: String) -> Self {
|
||||
pub fn new(base: &Path, id: FileId) -> Self {
|
||||
Self {
|
||||
base: base.to_owned(),
|
||||
id,
|
||||
extension,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn metadata_path_by_id(base: &Path, id: FileId) -> PathBuf {
|
||||
let mut path = base.to_path_buf();
|
||||
path.push(PathBuf::from(id.clone()));
|
||||
path.set_extension("json");
|
||||
path
|
||||
}
|
||||
|
||||
pub fn id(&self) -> FileId {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
pub fn file_path(&self) -> PathBuf {
|
||||
pub fn file_path(&self) -> Result<PathBuf, ReadFileError> {
|
||||
let info = FileInfo::load(self.metadata_path())?;
|
||||
|
||||
let mut path = self.base.clone();
|
||||
path.push(PathBuf::from(self.id.clone()));
|
||||
path.set_extension(self.extension.clone());
|
||||
path
|
||||
path.set_extension(info.extension);
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn metadata_path(&self) -> PathBuf {
|
||||
|
@ -58,11 +51,13 @@ impl PathResolver {
|
|||
path
|
||||
}
|
||||
|
||||
pub fn thumbnail_path(&self) -> PathBuf {
|
||||
pub fn thumbnail_path(&self) -> Result<PathBuf, ReadFileError> {
|
||||
let info = FileInfo::load(self.metadata_path())?;
|
||||
|
||||
let mut path = self.base.clone();
|
||||
path.push(PathBuf::from(self.id.clone()));
|
||||
path.set_extension(format!("tn.{}", self.extension));
|
||||
path
|
||||
path.set_extension(format!("tn.{}", info.extension));
|
||||
Ok(path)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,6 +71,7 @@ impl TryFrom<String> for PathResolver {
|
|||
impl TryFrom<&str> for PathResolver {
|
||||
type Error = PathError;
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
let path = Path::new(s);
|
||||
PathResolver::try_from(Path::new(s))
|
||||
}
|
||||
}
|
||||
|
@ -99,10 +95,6 @@ impl TryFrom<&Path> for PathResolver {
|
|||
.file_stem()
|
||||
.and_then(|s| s.to_str().map(|s| FileId::from(s)))
|
||||
.ok_or(PathError::InvalidPath)?,
|
||||
extension: path
|
||||
.extension()
|
||||
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
||||
.ok_or(PathError::InvalidPath)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +120,6 @@ impl FileHandle {
|
|||
let path = PathResolver {
|
||||
base: root.clone(),
|
||||
id: id.clone(),
|
||||
extension: extension.clone(),
|
||||
};
|
||||
|
||||
let file_type = mime_guess::from_ext(&extension)
|
||||
|
@ -152,8 +143,8 @@ impl FileHandle {
|
|||
}
|
||||
|
||||
pub fn load(id: &FileId, root: &Path) -> Result<Self, ReadFileError> {
|
||||
let info = FileInfo::load(PathResolver::metadata_path_by_id(root, id.clone()))?;
|
||||
let resolver = PathResolver::new(root, id.clone(), info.extension.clone());
|
||||
let resolver = PathResolver::new(root, id.clone());
|
||||
let info = FileInfo::load(resolver.metadata_path())?;
|
||||
Ok(Self {
|
||||
id: info.id.clone(),
|
||||
path: resolver,
|
||||
|
@ -162,42 +153,69 @@ impl FileHandle {
|
|||
}
|
||||
|
||||
pub fn set_content(&mut self, content: Vec<u8>) -> Result<(), WriteFileError> {
|
||||
let mut content_file = std::fs::File::create(self.path.file_path())?;
|
||||
let mut content_file = std::fs::File::create(
|
||||
self.path
|
||||
.file_path()
|
||||
.map_err(|_| WriteFileError::NoMetadata)?,
|
||||
)?;
|
||||
let byte_count = content_file.write(&content)?;
|
||||
self.info.size = byte_count;
|
||||
self.info.hash = self.hash_content(&content).as_string();
|
||||
|
||||
let mut md_file = std::fs::File::create(self.path.metadata_path())?;
|
||||
md_file.write(&serde_json::to_vec(&self.info)?)?;
|
||||
|
||||
self.write_thumbnail()?;
|
||||
self.write_thumbnail();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn content(&self) -> Result<Vec<u8>, ReadFileError> {
|
||||
load_content(&self.path.file_path())
|
||||
load_content(&self.path.file_path()?)
|
||||
}
|
||||
|
||||
pub fn thumbnail(&self) -> Result<Vec<u8>, ReadFileError> {
|
||||
load_content(&self.path.thumbnail_path())
|
||||
load_content(&self.path.thumbnail_path()?)
|
||||
}
|
||||
|
||||
fn hash_content(&self, data: &Vec<u8>) -> HexString {
|
||||
HexString::from_bytes(&Sha256::digest(data).to_vec())
|
||||
fn hash_content(&self) -> Result<HexString, ReadFileError> {
|
||||
let mut buf = Vec::new();
|
||||
let mut file = std::fs::File::open(self.path.file_path()?)?;
|
||||
file.read_to_end(&mut buf).map_err(ReadFileError::from)?;
|
||||
|
||||
Ok(HexString::from_bytes(&Sha256::digest(&buf).to_vec()))
|
||||
}
|
||||
|
||||
fn write_thumbnail(&self) -> Result<(), WriteFileError> {
|
||||
let img = image::open(&self.path.file_path())?;
|
||||
let img = image::open(
|
||||
&self
|
||||
.path
|
||||
.file_path()
|
||||
.map_err(|_| WriteFileError::NoMetadata)?,
|
||||
)?;
|
||||
let tn = img.resize(640, 640, FilterType::Nearest);
|
||||
tn.save(&self.path.thumbnail_path())?;
|
||||
tn.save(
|
||||
&self
|
||||
.path
|
||||
.thumbnail_path()
|
||||
.map_err(|_| WriteFileError::NoMetadata)?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete(self) {
|
||||
let _ = std::fs::remove_file(self.path.thumbnail_path());
|
||||
let _ = std::fs::remove_file(self.path.file_path());
|
||||
let _ = std::fs::remove_file(self.path.metadata_path());
|
||||
pub fn delete(self) -> Result<(), WriteFileError> {
|
||||
std::fs::remove_file(
|
||||
self.path
|
||||
.thumbnail_path()
|
||||
.map_err(|_| WriteFileError::NoMetadata)?,
|
||||
)?;
|
||||
std::fs::remove_file(self.path.metadata_path())?;
|
||||
std::fs::remove_file(
|
||||
self.path
|
||||
.file_path()
|
||||
.map_err(|_| WriteFileError::NoMetadata)?,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,47 +229,43 @@ fn load_content(path: &Path) -> Result<Vec<u8>, ReadFileError> {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::store::utils::FileCleanup;
|
||||
use cool_asserts::assert_matches;
|
||||
use std::{convert::TryFrom, path::PathBuf};
|
||||
use tempdir::TempDir;
|
||||
|
||||
#[test]
|
||||
fn paths() {
|
||||
let resolver = PathResolver::try_from("path/82420255-d3c8-4d90-a582-f94be588c70c.png")
|
||||
.expect("to have a valid path");
|
||||
|
||||
assert_eq!(
|
||||
assert_matches!(
|
||||
resolver.file_path(),
|
||||
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.png")
|
||||
Ok(path) => assert_eq!(path, PathBuf::from(
|
||||
"path/82420255-d3c8-4d90-a582-f94be588c70c.png"
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
resolver.metadata_path(),
|
||||
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.json")
|
||||
);
|
||||
assert_eq!(
|
||||
assert_matches!(
|
||||
resolver.thumbnail_path(),
|
||||
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.tn.png")
|
||||
Ok(path) => assert_eq!(path, PathBuf::from(
|
||||
"path/82420255-d3c8-4d90-a582-f94be588c70c.tn.png"
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_opens_a_file() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
}
|
||||
let _md = FileCleanup(PathBuf::from("fixtures/.metadata/rawr.png.json"));
|
||||
let _tn = FileCleanup(PathBuf::from("fixtures/.thumbnails/rawr.png"));
|
||||
|
||||
#[test]
|
||||
fn it_deletes_a_file() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let f =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
f.delete();
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_return_a_thumbnail() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let _ =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
let f = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
/*
|
||||
assert_eq!(
|
||||
f.thumbnail(),
|
||||
|
@ -265,16 +279,14 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn it_can_return_a_file_stream() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let _ =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
let f = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
// f.stream().expect("to succeed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_raises_an_error_when_file_not_found() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
match FileHandle::load(&FileId::from("rawr"), tmp.path()) {
|
||||
let resolver = PathResolver::try_from("var/rawr.png").expect("a valid path");
|
||||
match FileHandle::load(&FileId::from("rawr"), &PathBuf::from("var/")) {
|
||||
Err(ReadFileError::FileNotFound(_)) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
|
|
|
@ -41,12 +41,15 @@ impl FileInfo {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::store::FileId;
|
||||
use tempdir::TempDir;
|
||||
use crate::store::{filehandle::PathResolver, utils::FileCleanup, FileId};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
#[test]
|
||||
fn it_saves_and_loads_metadata() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let resolver = PathResolver::try_from("var/1617654d-a588-4714-b4fa-e00ed0a8a607.png")
|
||||
.expect("a valid path");
|
||||
let _cleanup = FileCleanup(resolver.metadata_path());
|
||||
|
||||
let created = Utc::now();
|
||||
|
||||
let info = FileInfo {
|
||||
|
@ -57,11 +60,9 @@ mod test {
|
|||
hash: "abcdefg".to_owned(),
|
||||
extension: "png".to_owned(),
|
||||
};
|
||||
let mut path = tmp.path().to_owned();
|
||||
path.push(&PathBuf::from(info.id.clone()));
|
||||
info.save(path.clone()).unwrap();
|
||||
info.save(resolver.metadata_path()).unwrap();
|
||||
|
||||
let info_ = FileInfo::load(path).unwrap();
|
||||
let info_ = FileInfo::load(resolver.metadata_path()).unwrap();
|
||||
assert_eq!(info_.size, 23777);
|
||||
assert_eq!(info_.created, info.created);
|
||||
assert_eq!(info_.file_type, "image/png");
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use std::{ops::Deref, path::PathBuf};
|
||||
use std::{
|
||||
ops::Deref,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
mod filehandle;
|
||||
mod fileinfo;
|
||||
mod thumbnail;
|
||||
pub mod utils;
|
||||
|
||||
pub use filehandle::FileHandle;
|
||||
pub use fileinfo::FileInfo;
|
||||
pub use thumbnail::Thumbnail;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum WriteFileError {
|
||||
|
@ -113,21 +118,8 @@ impl Store {
|
|||
Self { files_root }
|
||||
}
|
||||
|
||||
pub fn list_files(&self) -> Result<HashSet<FileId>, ReadFileError> {
|
||||
let paths = std::fs::read_dir(&self.files_root)?;
|
||||
let info_files = paths
|
||||
.into_iter()
|
||||
.filter_map(|path| {
|
||||
let path_ = path.unwrap().path();
|
||||
if path_.extension().and_then(|s| s.to_str()) == Some("json") {
|
||||
let stem = path_.file_stem().and_then(|s| s.to_str()).unwrap();
|
||||
Some(FileId::from(FileId::from(stem)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<HashSet<FileId>>();
|
||||
Ok(info_files)
|
||||
pub fn list_files(&self) -> Result<Vec<FileId>, ReadFileError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn add_file(
|
||||
|
@ -161,49 +153,50 @@ impl Store {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::store::utils::FileCleanup;
|
||||
use cool_asserts::assert_matches;
|
||||
use std::{collections::HashSet, io::Read};
|
||||
use tempdir::TempDir;
|
||||
|
||||
fn with_file<F>(test_fn: F)
|
||||
where
|
||||
F: FnOnce(Store, FileId, TempDir),
|
||||
F: FnOnce(Store, FileId),
|
||||
{
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut file = std::fs::File::open("fixtures/rawr.png").unwrap();
|
||||
file.read_to_end(&mut buf).unwrap();
|
||||
|
||||
let mut store = Store::new(PathBuf::from(tmp.path()));
|
||||
let mut store = Store::new(PathBuf::from("var/"));
|
||||
let file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
||||
|
||||
test_fn(store, file_record.id, tmp);
|
||||
let _file = FileCleanup(PathBuf::from(format!("var/{}.png", *file_record.id)));
|
||||
let _md = FileCleanup(PathBuf::from(format!("var/{}.json", *file_record.id)));
|
||||
let _tn = FileCleanup(PathBuf::from(format!("var/{}.tn.png", *file_record.id)));
|
||||
|
||||
test_fn(store, file_record.id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adds_files() {
|
||||
with_file(|store, id, tmp| {
|
||||
with_file(|store, id| {
|
||||
let file = store.get_file(&id).expect("to retrieve the file");
|
||||
|
||||
assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777);
|
||||
|
||||
assert!(tmp.path().join(&(*id)).with_extension("png").exists());
|
||||
assert!(tmp.path().join(&(*id)).with_extension("json").exists());
|
||||
assert!(tmp.path().join(&(*id)).with_extension("tn.png").exists());
|
||||
assert!(PathBuf::from(format!("var/{}.png", *id)).exists());
|
||||
assert!(PathBuf::from(format!("var/{}.json", *id)).exists());
|
||||
assert!(PathBuf::from(format!("var/{}.tn.png", *id)).exists());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sets_up_metadata_for_file() {
|
||||
with_file(|store, id, tmp| {
|
||||
assert!(tmp.path().join(&(*id)).with_extension("png").exists());
|
||||
with_file(|store, id| {
|
||||
let info = store.get_metadata(&id).expect("to retrieve the metadata");
|
||||
|
||||
assert_matches!(info, FileInfo { size, file_type, hash, extension, .. } => {
|
||||
assert_eq!(size, 23777);
|
||||
assert_eq!(file_type, "image/png");
|
||||
assert_eq!(hash, "b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee".to_owned());
|
||||
assert_eq!(hash, "".to_owned());
|
||||
assert_eq!(extension, "png".to_owned());
|
||||
});
|
||||
});
|
||||
|
@ -221,23 +214,22 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn deletes_associated_files() {
|
||||
with_file(|mut store, id, tmp| {
|
||||
with_file(|mut store, id| {
|
||||
store.delete_file(&id).expect("file to be deleted");
|
||||
|
||||
assert!(!tmp.path().join(&(*id)).with_extension("png").exists());
|
||||
assert!(!tmp.path().join(&(*id)).with_extension("json").exists());
|
||||
assert!(!tmp.path().join(&(*id)).with_extension("tn.png").exists());
|
||||
assert!(!PathBuf::from(format!("var/{}.png", *id)).exists());
|
||||
assert!(!PathBuf::from(format!("var/{}.json", *id)).exists());
|
||||
assert!(!PathBuf::from(format!("var/{}.tn.png", *id)).exists());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_files_in_the_db() {
|
||||
with_file(|store, id, _| {
|
||||
with_file(|store, id| {
|
||||
let resolvers = store.list_files().expect("file listing to succeed");
|
||||
let ids = resolvers.into_iter().collect::<HashSet<FileId>>();
|
||||
|
||||
assert_eq!(ids.len(), 1);
|
||||
assert!(ids.contains(&id));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub struct FileCleanup(pub PathBuf);
|
||||
|
||||
impl Drop for FileCleanup {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn append_extension(path: &Path, extra_ext: &str) -> PathBuf {
|
||||
let ext_ = match path.extension() {
|
||||
None => String::from(extra_ext),
|
||||
Some(ext) => [ext.to_string_lossy(), std::borrow::Cow::from(extra_ext)].join("."),
|
||||
};
|
||||
path.with_extension(ext_)
|
||||
}
|
Loading…
Reference in New Issue