Import and update the file service application and orizentic #72
|
@ -681,6 +681,7 @@ 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" }
|
||||
|
|
|
@ -211,8 +211,8 @@ fn load_content(path: &Path) -> Result<Vec<u8>, ReadFileError> {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::store::utils::DirCleanup;
|
||||
use std::{convert::TryFrom, path::PathBuf};
|
||||
use tempdir::TempDir;
|
||||
|
||||
#[test]
|
||||
fn paths() {
|
||||
|
@ -235,21 +235,23 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn it_opens_a_file() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_deletes_a_file() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
let f = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let f =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
f.delete();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_can_return_a_thumbnail() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let _ =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
/*
|
||||
assert_eq!(
|
||||
f.thumbnail(),
|
||||
|
@ -263,15 +265,16 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn it_can_return_a_file_stream() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
let _ = FileHandle::new("rawr.png".to_owned(), PathBuf::from("var/")).expect("to succeed");
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let _ =
|
||||
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
|
||||
// f.stream().expect("to succeed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_raises_an_error_when_file_not_found() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
match FileHandle::load(&FileId::from("rawr"), &PathBuf::from("var/")) {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
match FileHandle::load(&FileId::from("rawr"), tmp.path()) {
|
||||
Err(ReadFileError::FileNotFound(_)) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
|
|
|
@ -41,11 +41,12 @@ impl FileInfo {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::store::{utils::DirCleanup, FileId};
|
||||
use crate::store::FileId;
|
||||
use tempdir::TempDir;
|
||||
|
||||
#[test]
|
||||
fn it_saves_and_loads_metadata() {
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
let created = Utc::now();
|
||||
|
||||
let info = FileInfo {
|
||||
|
@ -56,10 +57,11 @@ mod test {
|
|||
hash: "abcdefg".to_owned(),
|
||||
extension: "png".to_owned(),
|
||||
};
|
||||
info.save(PathBuf::from(format!("var/{}", *info.id)))
|
||||
.unwrap();
|
||||
let mut path = tmp.path().to_owned();
|
||||
path.push(&PathBuf::from(info.id.clone()));
|
||||
info.save(path.clone()).unwrap();
|
||||
|
||||
let info_ = FileInfo::load(PathBuf::from(format!("var/{}", *info.id))).unwrap();
|
||||
let info_ = FileInfo::load(path).unwrap();
|
||||
assert_eq!(info_.size, 23777);
|
||||
assert_eq!(info_.created, info.created);
|
||||
assert_eq!(info_.file_type, "image/png");
|
||||
|
|
|
@ -5,7 +5,6 @@ use thiserror::Error;
|
|||
|
||||
mod filehandle;
|
||||
mod fileinfo;
|
||||
pub mod utils;
|
||||
|
||||
pub use filehandle::FileHandle;
|
||||
pub use fileinfo::FileInfo;
|
||||
|
@ -161,49 +160,50 @@ impl Store {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{utils::DirCleanup, *};
|
||||
use super::*;
|
||||
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),
|
||||
F: FnOnce(Store, FileId, TempDir),
|
||||
{
|
||||
let _cleanup = DirCleanup(PathBuf::from("var/"));
|
||||
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("var/"));
|
||||
let mut store = Store::new(PathBuf::from(tmp.path()));
|
||||
let file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
||||
|
||||
test_fn(store, file_record.id);
|
||||
test_fn(store, file_record.id, tmp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adds_files() {
|
||||
with_file(|store, id| {
|
||||
with_file(|store, id, tmp| {
|
||||
let file = store.get_file(&id).expect("to retrieve the file");
|
||||
|
||||
assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777);
|
||||
|
||||
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());
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sets_up_metadata_for_file() {
|
||||
with_file(|store, id| {
|
||||
assert!(PathBuf::from(format!("var/{}.png", *id)).exists());
|
||||
with_file(|store, id, tmp| {
|
||||
assert!(tmp.path().join(&(*id)).with_extension("png").exists());
|
||||
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, "".to_owned());
|
||||
assert_eq!(hash, "b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee".to_owned());
|
||||
assert_eq!(extension, "png".to_owned());
|
||||
});
|
||||
});
|
||||
|
@ -221,22 +221,21 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn deletes_associated_files() {
|
||||
with_file(|mut store, id| {
|
||||
with_file(|mut store, id, tmp| {
|
||||
store.delete_file(&id).expect("file to be deleted");
|
||||
|
||||
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());
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
#[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>>();
|
||||
|
||||
println!("ids: {:?}", ids);
|
||||
assert_eq!(ids.len(), 1);
|
||||
assert!(ids.contains(&id));
|
||||
});
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
use std::{ffi::OsStr, path::PathBuf};
|
||||
|
||||
pub struct DirCleanup(pub PathBuf);
|
||||
|
||||
impl Drop for DirCleanup {
|
||||
fn drop(&mut self) {
|
||||
let files = std::fs::read_dir(&self.0).unwrap();
|
||||
for file in files {
|
||||
let filename = file.unwrap().path();
|
||||
if filename.file_name() != Some(&OsStr::new(".placeholder")) {
|
||||
let _ = std::fs::remove_file(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue