Extract FileSystem to a trait for better testability

Added some tests around filtering and searching by Tags. Added the
ability to use an in-memory Sqlite DB for more integration tests.
This commit is contained in:
Cameron Cordes
2023-03-22 13:58:48 -04:00
parent 5a2dce85e8
commit 4ded708911
3 changed files with 245 additions and 36 deletions

View File

@@ -35,7 +35,7 @@ use log::{debug, error, info};
use crate::auth::login;
use crate::data::*;
use crate::database::*;
use crate::files::{is_image_or_video, is_valid_full_path};
use crate::files::{is_image_or_video, is_valid_full_path, RealFileSystem};
use crate::service::ServiceBuilder;
use crate::state::AppState;
use crate::tags::*;
@@ -156,9 +156,10 @@ async fn upload_image(
let path = file_path.unwrap_or_else(|| app_state.base_path.clone());
if !file_content.is_empty() {
let full_path = PathBuf::from(&path).join(file_name.unwrap());
if let Some(full_path) =
is_valid_full_path(&app_state.base_path, full_path.to_str().unwrap_or(""))
{
if let Some(full_path) = is_valid_full_path(
&app_state.base_path,
&full_path.to_str().unwrap().to_string(),
) {
if !full_path.is_file() && is_image_or_video(&full_path) {
let mut file = File::create(full_path).unwrap();
file.write_all(&file_content).unwrap();
@@ -474,7 +475,10 @@ fn main() -> std::io::Result<()> {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/login").route(web::post().to(login::<SqliteUserDao>)))
.service(web::resource("/photos").route(web::get().to(files::list_photos::<SqliteTagDao>)))
.service(
web::resource("/photos")
.route(web::get().to(files::list_photos::<SqliteTagDao, RealFileSystem>)),
)
.service(get_image)
.service(upload_image)
.service(generate_video)
@@ -486,6 +490,7 @@ fn main() -> std::io::Result<()> {
.service(get_file_metadata)
.add_feature(add_tag_services::<_, SqliteTagDao>)
.app_data(app_data.clone())
.app_data::<Data<RealFileSystem>>(Data::new(RealFileSystem::new(app_data.base_path.clone())))
.app_data::<Data<Mutex<SqliteUserDao>>>(Data::new(Mutex::new(user_dao)))
.app_data::<Data<Mutex<Box<dyn FavoriteDao>>>>(Data::new(Mutex::new(Box::new(
favorites_dao,