Merge branch 'master' into feature/include-tag-counts
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@
|
||||
database/target
|
||||
*.db
|
||||
.env
|
||||
/tmp
|
||||
|
||||
# Default ignored files
|
||||
.idea/shelf/
|
||||
|
||||
1
.idea/image-api.iml
generated
1
.idea/image-api.iml
generated
@@ -3,6 +3,7 @@
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
||||
1262
Cargo.lock
generated
1262
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -27,9 +27,9 @@ chrono = "0.4"
|
||||
dotenv = "0.15"
|
||||
bcrypt = "0.15.0"
|
||||
image = { version = "0.24.7", default-features = false, features = ["jpeg", "png", "jpeg_rayon"] }
|
||||
walkdir = "2"
|
||||
walkdir = "2.4.0"
|
||||
rayon = "1.5"
|
||||
notify = "4.0"
|
||||
notify = "6.1.1"
|
||||
path-absolutize = "3.0"
|
||||
log="0.4"
|
||||
env_logger= "0.10.1"
|
||||
|
||||
63
src/main.rs
63
src/main.rs
@@ -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,17 +536,22 @@ 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) => {
|
||||
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);
|
||||
|
||||
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) =
|
||||
@@ -547,12 +573,15 @@ fn watch_files() {
|
||||
}
|
||||
}
|
||||
}
|
||||
DebouncedEvent::Remove(_) => {
|
||||
}
|
||||
|
||||
EventKind::Remove(_) => {
|
||||
update_media_counts(&PathBuf::from(env::var("BASE_PATH").unwrap()))
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user