use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex};
use std::thread;
use hdrshot_core::{ConvertOptions, Outputs, share};
pub enum Event {
Started(usize),
Finished(usize, Result<Outputs, String>),
Trashed(usize, Result<(), String>),
Done(Option<Copied>),
}
pub type SharedClipboard = Arc<Mutex<Option<arboard::Clipboard>>>;
pub struct Job {
pub events: Receiver<Event>,
cancel: Arc<AtomicBool>,
thread: thread::JoinHandle<()>,
}
impl Job {
pub fn start(
files: Vec<(usize, PathBuf)>,
options: ConvertOptions,
clipboard: Option<SharedClipboard>,
trash: bool,
ctx: egui::Context,
) -> Self {
let (tx, events) = channel();
let cancel = Arc::new(AtomicBool::new(false));
let stop = cancel.clone();
let thread = thread::spawn(move || {
let send = |tx: &Sender<Event>, event| {
let _ = tx.send(event);
ctx.request_repaint();
};
let mut last = None;
let mut complete = Vec::new();
let count = files.len();
for (n, (index, path)) in files.into_iter().enumerate() {
if stop.load(Ordering::Relaxed) {
break;
}
send(&tx, Event::Started(index));
let result = if trash || (clipboard.is_some() && n + 1 == count) {
hdrshot_core::convert_file_with_discord_png(&path, &options).map(
|(outputs, discord)| {
if let (Some(hdr), Some(png)) = (&outputs.hdr, discord) {
save_discord_png(hdr, &png);
}
outputs
},
)
} else {
hdrshot_core::convert_file(&path, &options)
};
if let Ok(outputs) = &result
&& (outputs.hdr.is_some() || outputs.sdr.is_some())
{
last = Some((path.clone(), outputs.clone()));
}
if result.as_ref().is_ok_and(Outputs::complete) {
complete.push((index, path.clone()));
}
send(
&tx,
Event::Finished(index, result.map_err(|e| e.to_string())),
);
}
let copied = clipboard
.zip(last)
.map(|(clipboard, (input, outputs))| copy(&clipboard, &input, &outputs, &options));
if trash && !stop.load(Ordering::Relaxed) {
for (index, path) in complete {
let result = ::trash::delete(&path).map_err(|e| e.to_string());
send(&tx, Event::Trashed(index, result));
}
}
send(&tx, Event::Done(copied));
});
Self {
events,
cancel,
thread,
}
}
pub fn cancel(&self) {
self.cancel.store(true, Ordering::Relaxed);
}
pub fn is_cancelled(&self) -> bool {
self.cancel.load(Ordering::Relaxed)
}
pub fn finish(self) {
self.cancel();
let _ = self.thread.join();
}
}
pub struct Copied {
pub message: String,
pub too_big: Option<TooBig>,
}
impl From<String> for Copied {
fn from(message: String) -> Self {
Self {
message,
too_big: None,
}
}
}
fn discord_dir() -> PathBuf {
std::env::temp_dir().join("hdrshot-discord")
}
fn discord_path(hdr: &Path) -> PathBuf {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
hdr.hash(&mut hasher);
discord_dir()
.join(format!("{:016x}", hasher.finish()))
.join(hdr.with_extension("png").file_name().unwrap_or_default())
}
pub fn copy_makes_png(outputs: &Outputs) -> bool {
outputs.hdr.as_ref().is_some_and(|hdr| {
!hdr.extension()
.is_some_and(|e| e.eq_ignore_ascii_case("png"))
&& !discord_path(hdr).is_file()
})
}
fn save_discord_png(hdr: &Path, png: &[u8]) {
let path = discord_path(hdr);
if let Some(dir) = path.parent()
&& std::fs::create_dir_all(dir).is_ok()
{
let _ = std::fs::write(&path, png);
}
}
pub fn clean_up() {
let _ = std::fs::remove_dir_all(discord_dir());
}
#[derive(Clone)]
pub struct TooBig {
pub input: PathBuf,
pub name: String,
pub hdr: bool,
pub size: u64,
}
pub fn copy(
clipboard: &SharedClipboard,
input: &Path,
outputs: &Outputs,
options: &ConvertOptions,
) -> Copied {
let (path, hdr) = match (&outputs.hdr, &outputs.sdr) {
(Some(hdr), _) => (hdr.as_path(), true),
(None, Some(sdr)) => (sdr.as_path(), false),
(None, None) => return Copied::from("Nothing to copy".to_string()),
};
let png = path
.extension()
.is_some_and(|e| e.eq_ignore_ascii_case("png"));
let name = if png {
path.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
} else {
path.with_extension("png")
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
};
let ready = discord_path(path);
let copy = if !hdr {
Ok(path.to_path_buf())
} else if png {
std::fs::read(path).and_then(|bytes| share::clipboard_file(&name, &bytes, true))
} else if ready.is_file() {
Ok(ready)
} else {
share::discord_png(input, options)
.map_err(std::io::Error::other)
.and_then(|bytes| share::clipboard_file(&name, &bytes, false))
};
let copy = match copy {
Ok(copy) => copy,
Err(e) => return Copied::from(format!("Could not copy to the clipboard: {e}")),
};
if let Err(e) = set_file(clipboard, copy.clone()) {
return Copied::from(e);
}
let size = std::fs::metadata(©).map_or(0, |m| m.len());
if size > share::DISCORD_LIMIT {
Copied {
message: format!(
"Copied {name} ({}), more than Discord's {} limit",
share::megabytes(size),
share::megabytes(share::DISCORD_LIMIT)
),
too_big: Some(TooBig {
input: input.to_path_buf(),
name,
hdr,
size,
}),
}
} else {
Copied::from(format!("Copied {name} to the clipboard"))
}
}
pub fn copy_smaller(
clipboard: &SharedClipboard,
too_big: &TooBig,
options: &ConvertOptions,
) -> String {
let fail = |e: &dyn std::fmt::Display| format!("Could not make a smaller copy: {e}");
let smaller =
match share::smaller_png(&too_big.input, options, too_big.hdr, share::DISCORD_LIMIT) {
Ok(Some(smaller)) => smaller,
Ok(None) => return "Even a 720p copy is larger than 10 MB".into(),
Err(e) => return fail(&e),
};
let path = match share::clipboard_file(&too_big.name, &smaller.png, false) {
Ok(path) => path,
Err(e) => return fail(&e),
};
match set_file(clipboard, path) {
Ok(()) => format!(
"Copied {} at {}×{} ({})",
too_big.name,
smaller.width,
smaller.height,
share::megabytes(smaller.png.len() as u64)
),
Err(e) => e,
}
}
fn set_file(clipboard: &SharedClipboard, path: PathBuf) -> Result<(), String> {
let fail = |e: arboard::Error| format!("Could not copy to the clipboard: {e}");
let mut clipboard = clipboard.lock().unwrap_or_else(|e| e.into_inner());
let clipboard = match &mut *clipboard {
Some(c) => c,
None => clipboard.insert(arboard::Clipboard::new().map_err(fail)?),
};
clipboard.set().file_list(&[path]).map_err(fail)
}
pub fn in_background(
messages: Sender<Copied>,
ctx: egui::Context,
copy: impl FnOnce() -> Copied + Send + 'static,
) {
thread::spawn(move || {
let _ = messages.send(copy());
ctx.request_repaint();
});
}