monorepo/file-service/src/store/mod.rs

420 lines
10 KiB
Rust
Raw Normal View History

2023-09-23 23:15:56 +00:00
use serde::{Deserialize, Serialize};
2023-10-03 15:10:37 +00:00
use sqlx::sqlite::SqlitePool;
2023-09-25 03:52:29 +00:00
use std::collections::HashSet;
2023-10-03 15:10:37 +00:00
use std::{ops::Deref, path::PathBuf, sync::Arc};
use thiserror::Error;
2023-10-03 15:10:37 +00:00
use tokio::sync::RwLock;
2023-09-24 16:08:09 +00:00
mod filehandle;
mod fileinfo;
2023-09-24 16:08:09 +00:00
pub use filehandle::FileHandle;
pub use fileinfo::FileInfo;
#[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,
2023-09-24 16:08:09 +00:00
#[error("no metadata available")]
NoMetadata,
#[error("file could not be loaded")]
LoadError(#[from] ReadFileError),
2023-09-23 19:17:49 +00:00
#[error("image conversion failed")]
ImageError(#[from] image::ImageError),
#[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),
#[error("path is not a file")]
NotAFile,
#[error("permission denied")]
PermissionDenied,
2023-09-23 19:17:49 +00:00
#[error("invalid path")]
InvalidPath,
#[error("JSON error")]
JSONError(#[from] serde_json::error::Error),
#[error("IO error")]
IOError(#[from] std::io::Error),
}
2023-10-03 15:10:37 +00:00
#[derive(Debug, Error)]
pub enum AuthError {
#[error("database failed")]
SqlError,
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct Username(String);
impl From<String> for Username {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for Username {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}
impl From<Username> for PathBuf {
fn from(s: Username) -> Self {
Self::from(&s)
}
}
impl From<&Username> for PathBuf {
fn from(s: &Username) -> Self {
let Username(s) = s;
Self::from(s)
}
}
impl Deref for Username {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct AuthToken(String);
impl From<String> for AuthToken {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for AuthToken {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}
impl From<AuthToken> for PathBuf {
fn from(s: AuthToken) -> Self {
Self::from(&s)
}
}
impl From<&AuthToken> for PathBuf {
fn from(s: &AuthToken) -> Self {
let AuthToken(s) = s;
Self::from(s)
}
}
impl Deref for AuthToken {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
2023-10-03 15:10:37 +00:00
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct SessionToken(String);
impl From<String> for SessionToken {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for SessionToken {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}
impl From<SessionToken> for PathBuf {
fn from(s: SessionToken) -> Self {
Self::from(&s)
}
}
impl From<&SessionToken> for PathBuf {
fn from(s: &SessionToken) -> Self {
let SessionToken(s) = s;
Self::from(s)
}
}
impl Deref for SessionToken {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
2023-09-24 16:08:09 +00:00
#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq)]
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 From<FileId> for PathBuf {
fn from(s: FileId) -> Self {
Self::from(&s)
}
}
impl From<&FileId> for PathBuf {
fn from(s: &FileId) -> Self {
let FileId(s) = s;
Self::from(s)
}
}
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-10-03 15:10:37 +00:00
#[derive(Clone)]
pub struct App {
authdb: Arc<RwLock<AuthDB>>,
store: Arc<RwLock<Store>>,
}
impl App {
pub fn new(authdb: AuthDB, store: Store) -> Self {
Self {
authdb: Arc::new(RwLock::new(authdb)),
store: Arc::new(RwLock::new(store)),
}
}
pub async fn auth_token(&self, token: AuthToken) -> Result<SessionToken, AuthError> {
self.authdb.read().await.auth_token(token).await
}
2023-10-03 15:10:37 +00:00
pub async fn auth_session(&self, token: SessionToken) -> Result<Username, AuthError> {
self.authdb.read().await.auth_session(token).await
2023-10-03 15:10:37 +00:00
}
pub async fn list_files(&self) -> Result<HashSet<FileId>, ReadFileError> {
self.store.read().await.list_files()
}
pub async fn get_file(&self, id: &FileId) -> Result<FileHandle, ReadFileError> {
self.store.read().await.get_file(id)
}
pub async fn add_file(
&self,
filename: String,
content: Vec<u8>,
) -> Result<FileHandle, WriteFileError> {
self.store.write().await.add_file(filename, content)
}
}
#[derive(Clone)]
pub struct AuthDB {
pool: SqlitePool,
}
impl AuthDB {
pub async fn new(path: PathBuf) -> Result<Self, sqlx::Error> {
let pool = SqlitePool::connect(&format!("sqlite://{}", path.to_str().unwrap())).await?;
Ok(Self { pool })
}
async fn auth_token(&self, _token: AuthToken) -> Result<SessionToken, AuthError> {
unimplemented!()
}
async fn auth_session(&self, _token: SessionToken) -> Result<Username, AuthError> {
2023-10-03 15:10:37 +00:00
/*
let conn = self.pool.acquire().await.map_err(|_| AuthError::SqlError)?;
conn.transaction(|tr| {})
*/
unimplemented!()
}
}
pub struct Store {
files_root: PathBuf,
}
impl Store {
pub fn new(files_root: PathBuf) -> Self {
Self { files_root }
}
2023-09-25 03:52:29 +00:00
pub fn list_files(&self) -> Result<HashSet<FileId>, ReadFileError> {
let paths = std::fs::read_dir(&self.files_root)?;
let info_files = paths
2023-09-25 03:52:29 +00:00
.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
}
2023-09-25 03:52:29 +00:00
})
.collect::<HashSet<FileId>>();
Ok(info_files)
}
2023-09-24 16:08:09 +00:00
pub fn add_file(
&mut self,
filename: String,
content: Vec<u8>,
) -> Result<FileHandle, WriteFileError> {
let mut file = FileHandle::new(filename, self.files_root.clone())?;
file.set_content(content)?;
Ok(file)
}
2023-09-24 16:08:09 +00:00
pub fn get_file(&self, id: &FileId) -> Result<FileHandle, ReadFileError> {
FileHandle::load(id, &self.files_root)
}
pub fn delete_file(&mut self, id: &FileId) -> Result<(), WriteFileError> {
2023-09-24 16:08:09 +00:00
let handle = FileHandle::load(id, &self.files_root)?;
handle.delete();
Ok(())
}
pub fn get_metadata(&self, id: &FileId) -> Result<FileInfo, ReadFileError> {
2023-09-23 23:15:56 +00:00
let mut path = self.files_root.clone();
path.push(PathBuf::from(id));
2023-09-23 23:15:56 +00:00
path.set_extension("json");
FileInfo::load(path)
}
}
2023-09-23 01:56:43 +00:00
#[cfg(test)]
mod test {
2023-09-25 04:58:35 +00:00
use super::*;
2023-09-23 23:15:56 +00:00
use cool_asserts::assert_matches;
2023-09-24 16:08:09 +00:00
use std::{collections::HashSet, io::Read};
2023-09-25 04:58:35 +00:00
use tempdir::TempDir;
2023-09-23 01:56:43 +00:00
fn with_file<F>(test_fn: F)
where
2023-09-25 04:58:35 +00:00
F: FnOnce(Store, FileId, TempDir),
{
2023-09-25 04:58:35 +00:00
let tmp = TempDir::new("var").unwrap();
2023-09-25 03:52:29 +00:00
let mut buf = Vec::new();
let mut file = std::fs::File::open("fixtures/rawr.png").unwrap();
file.read_to_end(&mut buf).unwrap();
2023-09-25 04:58:35 +00:00
let mut store = Store::new(PathBuf::from(tmp.path()));
let file_record = store.add_file("rawr.png".to_owned(), buf).unwrap();
2023-09-25 04:58:35 +00:00
test_fn(store, file_record.id, tmp);
}
#[test]
fn adds_files() {
2023-09-25 04:58:35 +00:00
with_file(|store, id, tmp| {
2023-09-24 16:08:09 +00:00
let file = store.get_file(&id).expect("to retrieve the file");
assert_eq!(file.content().map(|file| file.len()).unwrap(), 23777);
2023-09-23 23:15:56 +00:00
2023-09-25 04:58:35 +00:00
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]
2023-09-23 23:15:56 +00:00
fn sets_up_metadata_for_file() {
2023-09-25 04:58:35 +00:00
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");
2023-09-25 04:58:35 +00:00
assert_eq!(hash, "b6cd35e113b95d62f53d9cbd27ccefef47d3e324aef01a2db6c0c6d3a43c89ee".to_owned());
assert_eq!(extension, "png".to_owned());
});
2023-09-23 23:15:56 +00:00
});
}
2023-09-24 16:08:09 +00:00
/*
#[test]
fn sets_up_thumbnail_for_file() {
with_file(|store, id| {
let (_, thumbnail) = store.get_thumbnail(&id).expect("to retrieve the thumbnail");
assert_eq!(thumbnail.content().map(|file| file.len()).unwrap(), 48869);
});
}
2023-09-24 16:08:09 +00:00
*/
#[test]
fn deletes_associated_files() {
2023-09-25 04:58:35 +00:00
with_file(|mut store, id, tmp| {
store.delete_file(&id).expect("file to be deleted");
2023-09-25 04:58:35 +00:00
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]
2023-09-24 16:08:09 +00:00
fn lists_files_in_the_db() {
2023-09-25 04:58:35 +00:00
with_file(|store, id, _| {
2023-09-24 16:08:09 +00:00
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));
2023-09-24 16:08:09 +00:00
});
}
2023-09-23 01:56:43 +00:00
}