-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f2f2c47
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
.vscode/settings.json | ||
Cargo.lock | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "rust-thumbnail" | ||
version = "0.1.0" | ||
edition = "2021" | ||
author="SegFault" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
notify= "6.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::{ path::Path, env, fs, thread, time::Duration, sync::{Arc, atomic::{Ordering, AtomicBool}}}; | ||
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher, Event, EventKind}; | ||
use notify::event::ModifyKind::Any; | ||
|
||
fn main() { | ||
let found = Arc::new(AtomicBool::new(false)); | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap(); | ||
let temp_dir = env::var("TEMP").unwrap(); | ||
watcher.watch(Path::new(&temp_dir), RecursiveMode::Recursive).unwrap(); | ||
println!("Listening for workshop folder.."); | ||
|
||
for res in rx { | ||
match res { | ||
Ok(event) => handle_change(event, found.clone()), | ||
Err(error) => println!("Error: {error:?}"), | ||
} | ||
} | ||
} | ||
|
||
fn handle_change(event: Event, found: Arc<AtomicBool>) { | ||
if found.load(Ordering::Relaxed) { | ||
return; | ||
} | ||
|
||
if event.kind == EventKind::Modify(Any) { | ||
let path = event.paths[0].parent().unwrap(); | ||
let file_name = event.paths[0].file_name().unwrap(); | ||
let folder_name = path.file_name().unwrap(); | ||
if file_name == "icon_background.png" && folder_name.to_str().unwrap().starts_with("tmp") { | ||
found.store(true, Ordering::Relaxed); | ||
println!("Temp Workshop found: {:?}", path.file_name().unwrap()); | ||
handle_copy(path, "icon_background.png"); | ||
handle_copy(path, "icon.png"); | ||
thread::spawn( move || { | ||
thread::sleep(Duration::from_secs(5)); | ||
found.store(false, Ordering::Relaxed); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
fn handle_copy(folder: &Path, file: &str) { | ||
let source_path = Path::new("./to_inject").join(file); | ||
let target_path = Path::new(folder).join(file); | ||
|
||
if source_path.exists() { | ||
match fs::copy(source_path, target_path) { | ||
Ok(_) => println!("Successfully copied: {} ", file), | ||
Err(err) => println!("Failed to copy file: {}", err), | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.