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

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
database/target database/target
*.db *.db
.env .env
/tmp
# Default ignored files # Default ignored files
.idea/shelf/ .idea/shelf/

1
.idea/image-api.iml generated
View File

@@ -3,6 +3,7 @@
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

1262
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -27,9 +27,9 @@ chrono = "0.4"
dotenv = "0.15" dotenv = "0.15"
bcrypt = "0.15.0" bcrypt = "0.15.0"
image = { version = "0.24.7", default-features = false, features = ["jpeg", "png", "jpeg_rayon"] } image = { version = "0.24.7", default-features = false, features = ["jpeg", "png", "jpeg_rayon"] }
walkdir = "2" walkdir = "2.4.0"
rayon = "1.5" rayon = "1.5"
notify = "4.0" notify = "6.1.1"
path-absolutize = "3.0" path-absolutize = "3.0"
log="0.4" log="0.4"
env_logger= "0.10.1" env_logger= "0.10.1"

View File

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