Merge branch 'master' into feature/include-tag-counts

This commit is contained in:
Cameron Cordes
2024-01-17 22:47:46 -05:00
5 changed files with 586 additions and 781 deletions

View File

@@ -26,11 +26,12 @@ use actix_web::{
web::{self, BufMut, BytesMut},
App, HttpRequest, HttpResponse, HttpServer, Responder,
};
use chrono::Utc;
use diesel::sqlite::Sqlite;
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use rayon::prelude::*;
use log::{debug, error, info};
use log::{debug, error, info, warn};
use crate::auth::login;
use crate::data::*;
@@ -162,11 +163,30 @@ async fn upload_image(
true,
) {
if !full_path.is_file() && is_image_or_video(&full_path) {
let mut file = File::create(full_path).unwrap();
let mut file = File::create(&full_path).unwrap();
file.write_all(&file_content).unwrap();
info!("Uploaded: {:?}", full_path);
} else {
error!("File already exists: {:?}", full_path);
return HttpResponse::BadRequest().body("File already exists");
warn!("File already exists: {:?}", full_path);
let new_path = format!(
"{}/{}_{}.{}",
full_path.parent().unwrap().to_str().unwrap(),
full_path.file_stem().unwrap().to_str().unwrap(),
Utc::now().timestamp(),
full_path
.extension()
.expect("Uploaded file should have an extension")
.to_str()
.unwrap()
);
info!("Uploaded: {}", new_path);
let mut file = File::create(new_path).unwrap();
file.write_all(&file_content).unwrap();
return HttpResponse::Ok().finish();
}
} else {
error!("Invalid path for upload: {:?}", full_path);
@@ -175,6 +195,7 @@ async fn upload_image(
} else {
return HttpResponse::BadRequest().body("No file body read");
}
HttpResponse::Ok().finish()
}
@@ -394,7 +415,7 @@ fn create_thumbnails() {
let thumb_path = Path::new(thumbnail_directory).join(relative_path);
std::fs::create_dir_all(thumb_path.parent().unwrap())
.expect("There was an issue creating directory");
debug!("Saving thumbnail: {:?}", thumb_path);
info!("Saving thumbnail: {:?}", thumb_path);
image.save(thumb_path).expect("Failure saving thumbnail");
})
.for_each(drop);
@@ -515,44 +536,52 @@ fn run_migrations(
fn watch_files() {
std::thread::spawn(|| {
let (wtx, wrx) = channel();
let mut watcher = watcher(wtx, std::time::Duration::from_secs(10)).unwrap();
watcher
.watch(dotenv::var("BASE_PATH").unwrap(), RecursiveMode::Recursive)
.unwrap();
let mut watcher = RecommendedWatcher::new(wtx, Config::default()).unwrap();
let base_str = dotenv::var("BASE_PATH").unwrap();
let base_path = Path::new(&base_str);
watcher.watch(base_path, RecursiveMode::Recursive).unwrap();
loop {
let ev = wrx.recv();
if let Ok(event) = ev {
match event {
DebouncedEvent::Create(_) => create_thumbnails(),
DebouncedEvent::Rename(orig, _) | DebouncedEvent::Write(orig) => {
let image_base_path = PathBuf::from(env::var("BASE_PATH").unwrap());
let image_relative = orig.strip_prefix(&image_base_path).unwrap();
if let Ok(old_thumbnail) =
env::var("THUMBNAILS").map(PathBuf::from).map(|mut base| {
base.push(image_relative);
base
})
{
if let Err(e) = std::fs::remove_file(&old_thumbnail) {
error!(
"Error removing thumbnail: {}\n{}",
old_thumbnail.display(),
e
);
} else {
info!("Deleted moved thumbnail: {}", old_thumbnail.display());
if let Ok(Ok(event)) = ev {
match event.kind {
EventKind::Create(_) => create_thumbnails(),
EventKind::Modify(kind) => {
debug!("All modified paths: {:?}", event.paths);
debug!("Modify kind: {:?}", kind);
create_thumbnails();
if let Some(orig) = event.paths.first() {
let image_base_path = PathBuf::from(env::var("BASE_PATH").unwrap());
let image_relative = orig.strip_prefix(&image_base_path).unwrap();
if let Ok(old_thumbnail) =
env::var("THUMBNAILS").map(PathBuf::from).map(|mut base| {
base.push(image_relative);
base
})
{
if let Err(e) = std::fs::remove_file(&old_thumbnail) {
error!(
"Error removing thumbnail: {}\n{}",
old_thumbnail.display(),
e
);
} else {
info!("Deleted moved thumbnail: {}", old_thumbnail.display());
create_thumbnails();
}
}
}
}
DebouncedEvent::Remove(_) => {
EventKind::Remove(_) => {
update_media_counts(&PathBuf::from(env::var("BASE_PATH").unwrap()))
}
_ => continue,
};
}
_ => {}
}
};
}
});
}