2023-09-23 23:15:56 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-09-23 00:03:58 +00:00
|
|
|
use std::{
|
2023-09-23 19:17:49 +00:00
|
|
|
convert::TryFrom,
|
2023-09-23 00:03:58 +00:00
|
|
|
ops::Deref,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
use thiserror::Error;
|
2023-09-19 22:55:53 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
mod file;
|
|
|
|
mod fileinfo;
|
|
|
|
mod thumbnail;
|
2023-09-21 03:06:34 +00:00
|
|
|
pub mod utils;
|
2023-09-19 22:55:53 +00:00
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
pub use file::File;
|
2023-09-19 22:55:53 +00:00
|
|
|
pub use fileinfo::FileInfo;
|
|
|
|
pub use thumbnail::Thumbnail;
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum WriteFileError {
|
|
|
|
#[error("root file path does not exist")]
|
|
|
|
RootNotFound,
|
|
|
|
|
|
|
|
#[error("permission denied")]
|
|
|
|
PermissionDenied,
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
#[error("invalid path")]
|
|
|
|
InvalidPath,
|
|
|
|
|
|
|
|
#[error("image conversion failed")]
|
|
|
|
ImageError(#[from] image::ImageError),
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
#[error("JSON error")]
|
|
|
|
JSONError(#[from] serde_json::error::Error),
|
|
|
|
|
|
|
|
#[error("IO error")]
|
|
|
|
IOError(#[from] std::io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ReadFileError {
|
|
|
|
#[error("file not found")]
|
2023-09-23 23:15:56 +00:00
|
|
|
FileNotFound(PathBuf),
|
2023-09-23 00:03:58 +00:00
|
|
|
|
|
|
|
#[error("path is not a file")]
|
|
|
|
NotAFile,
|
|
|
|
|
|
|
|
#[error("permission denied")]
|
|
|
|
PermissionDenied,
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
#[error("invalid path")]
|
|
|
|
InvalidPath,
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
#[error("JSON error")]
|
|
|
|
JSONError(#[from] serde_json::error::Error),
|
|
|
|
|
|
|
|
#[error("IO error")]
|
|
|
|
IOError(#[from] std::io::Error),
|
|
|
|
}
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum PathError {
|
|
|
|
#[error("path cannot be derived from input")]
|
|
|
|
InvalidPath,
|
|
|
|
}
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-09-23 19:17:49 +00:00
|
|
|
pub struct PathResolver {
|
|
|
|
base: PathBuf,
|
|
|
|
id: String,
|
|
|
|
extension: String,
|
|
|
|
}
|
2023-09-23 00:03:58 +00:00
|
|
|
|
|
|
|
impl PathResolver {
|
2023-09-23 19:17:49 +00:00
|
|
|
fn new(base: &Path, id: &str, extension: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
base: base.to_owned(),
|
|
|
|
id: id.to_owned(),
|
|
|
|
extension: extension.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
fn file_path(&self) -> PathBuf {
|
2023-09-23 19:17:49 +00:00
|
|
|
let mut path = self.base.clone();
|
|
|
|
path.push(self.id.clone());
|
|
|
|
path.set_extension(self.extension.clone());
|
|
|
|
path
|
2023-09-23 00:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn metadata_path(&self) -> PathBuf {
|
2023-09-23 19:17:49 +00:00
|
|
|
let mut path = self.base.clone();
|
|
|
|
path.push(self.id.clone());
|
2023-09-23 00:03:58 +00:00
|
|
|
path.set_extension("json");
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thumbnail_path(&self) -> PathBuf {
|
2023-09-23 19:17:49 +00:00
|
|
|
let mut path = self.base.clone();
|
|
|
|
path.push(self.id.clone());
|
|
|
|
path.set_extension(format!("tn.{}", self.extension));
|
2023-09-23 00:03:58 +00:00
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
impl TryFrom<String> for PathResolver {
|
|
|
|
type Error = PathError;
|
|
|
|
fn try_from(s: String) -> Result<Self, Self::Error> {
|
|
|
|
PathResolver::try_from(s.as_str())
|
2023-09-23 01:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<PathBuf> for PathResolver {
|
|
|
|
type Error = PathError;
|
|
|
|
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
|
|
|
|
PathResolver::try_from(path.as_path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&Path> for PathResolver {
|
|
|
|
type Error = PathError;
|
|
|
|
fn try_from(path: &Path) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
base: path
|
|
|
|
.parent()
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
.ok_or(PathError::InvalidPath)?,
|
|
|
|
id: path
|
|
|
|
.file_stem()
|
|
|
|
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
|
|
|
.ok_or(PathError::InvalidPath)?,
|
|
|
|
extension: path
|
|
|
|
.extension()
|
|
|
|
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
|
|
|
.ok_or(PathError::InvalidPath)?,
|
|
|
|
})
|
2023-09-23 01:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2023-09-23 00:03:58 +00:00
|
|
|
pub struct FileId(String);
|
|
|
|
|
|
|
|
impl From<String> for FileId {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Self(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for FileId {
|
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
Self(s.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for FileId {
|
|
|
|
type Target = String;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FileRoot {
|
|
|
|
fn root(&self) -> PathBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Context(PathBuf);
|
|
|
|
|
|
|
|
impl FileRoot for Context {
|
|
|
|
fn root(&self) -> PathBuf {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 03:43:45 +00:00
|
|
|
pub struct Store {
|
2023-09-19 22:55:53 +00:00
|
|
|
files_root: PathBuf,
|
|
|
|
}
|
|
|
|
|
2023-09-23 03:43:45 +00:00
|
|
|
impl Store {
|
|
|
|
pub fn new(files_root: PathBuf) -> Self {
|
|
|
|
Self { files_root }
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
pub fn list_files(&self) -> Vec<Result<File, ReadFileError>> {
|
|
|
|
unimplemented!()
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 00:03:58 +00:00
|
|
|
pub fn add_file(&mut self, filename: String, content: Vec<u8>) -> Result<File, WriteFileError> {
|
|
|
|
let context = Context(self.files_root.clone());
|
|
|
|
let mut file = File::new(filename, context)?;
|
|
|
|
file.set_content(content)?;
|
|
|
|
Ok(file)
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
pub fn delete_file(&mut self, id: FileId) -> Result<(), WriteFileError> {
|
2023-09-23 03:43:45 +00:00
|
|
|
/*
|
2023-09-19 22:55:53 +00:00
|
|
|
let f = File::open(&id, &self.files_root)?;
|
|
|
|
f.delete()
|
2023-09-23 03:43:45 +00:00
|
|
|
*/
|
|
|
|
unimplemented!()
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
pub fn get_metadata(&self, id: FileId) -> Result<FileInfo, ReadFileError> {
|
|
|
|
let mut path = self.files_root.clone();
|
|
|
|
path.push(PathBuf::from((*id).clone()));
|
|
|
|
path.set_extension("json");
|
|
|
|
FileInfo::load(path)
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
pub fn get_file(&self, id: FileId) -> Result<(FileInfo, File), ReadFileError> {
|
|
|
|
let info = self.get_metadata(id.clone())?;
|
|
|
|
|
|
|
|
let resolver = PathResolver {
|
|
|
|
base: self.files_root.clone(),
|
|
|
|
id: (*id).clone(),
|
|
|
|
extension: info.extension.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let f = File::load(resolver)?;
|
|
|
|
Ok((info, f))
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
pub fn get_thumbnail(&self, id: FileId) -> Result<(FileInfo, File), ReadFileError> {
|
2023-09-23 00:03:58 +00:00
|
|
|
unimplemented!()
|
2023-09-19 22:55:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 01:56:43 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2023-09-23 23:15:56 +00:00
|
|
|
use crate::store::utils::FileCleanup;
|
|
|
|
use cool_asserts::assert_matches;
|
2023-09-23 03:43:45 +00:00
|
|
|
use std::io::Read;
|
2023-09-23 01:56:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn paths() {
|
2023-09-23 19:17:49 +00:00
|
|
|
let resolver = PathResolver::try_from("path/82420255-d3c8-4d90-a582-f94be588c70c.png")
|
|
|
|
.expect("to have a valid path");
|
2023-09-23 01:56:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
resolver.file_path(),
|
2023-09-23 19:17:49 +00:00
|
|
|
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.png")
|
2023-09-23 01:56:43 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resolver.metadata_path(),
|
|
|
|
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.json")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resolver.thumbnail_path(),
|
2023-09-23 19:17:49 +00:00
|
|
|
PathBuf::from("path/82420255-d3c8-4d90-a582-f94be588c70c.tn.png")
|
2023-09-23 01:56:43 +00:00
|
|
|
);
|
|
|
|
}
|
2023-09-23 03:43:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn adds_files() {
|
|
|
|
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 file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
let _file = FileCleanup(PathBuf::from(format!("var/{}.png", *file_record.id)));
|
2023-09-23 03:43:45 +00:00
|
|
|
let _md = FileCleanup(PathBuf::from(format!("var/{}.json", *file_record.id)));
|
|
|
|
let _tn = FileCleanup(PathBuf::from(format!("var/{}.tn", *file_record.id)));
|
|
|
|
|
2023-09-23 23:15:56 +00:00
|
|
|
store
|
|
|
|
.get_file(file_record.id.clone())
|
|
|
|
.expect("to retrieve the file");
|
|
|
|
|
2023-09-23 19:17:49 +00:00
|
|
|
assert!(PathBuf::from(format!("var/{}.png", *file_record.id)).exists());
|
2023-09-23 23:15:56 +00:00
|
|
|
assert!(PathBuf::from(format!("var/{}.json", *file_record.id)).exists());
|
|
|
|
assert!(PathBuf::from(format!("var/{}.tn.png", *file_record.id)).exists());
|
2023-09-23 03:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-09-23 23:15:56 +00:00
|
|
|
fn sets_up_metadata_for_file() {
|
|
|
|
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 file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
|
|
|
|
|
|
|
|
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", *file_record.id)));
|
|
|
|
|
|
|
|
let info = store
|
|
|
|
.get_metadata(file_record.id.clone())
|
|
|
|
.expect("to retrieve the file");
|
|
|
|
|
|
|
|
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!(extension, "png".to_owned());
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(PathBuf::from(format!("var/{}.png", *file_record.id)).exists());
|
|
|
|
assert!(PathBuf::from(format!("var/{}.json", *file_record.id)).exists());
|
|
|
|
assert!(PathBuf::from(format!("var/{}.tn.png", *file_record.id)).exists());
|
|
|
|
}
|
2023-09-23 03:43:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sets_up_thumbnail_for_file() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deletes_associated_files() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lists_files_in_the_db() {}
|
2023-09-23 01:56:43 +00:00
|
|
|
}
|