optimisations
This commit is contained in:
@ -2,6 +2,7 @@ use crate::models::{ClipboardData, ClipboardEntry, Image};
|
||||
use rusqlite::Connection;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
use uuid::Uuid;
|
||||
|
||||
@ -12,20 +13,12 @@ pub struct Database {
|
||||
|
||||
impl Database {
|
||||
pub fn init(dir_path: &str) -> Result<Self, Box<dyn Error>> {
|
||||
if !std::fs::exists(dir_path)? {
|
||||
fs::create_dir(dir_path)?;
|
||||
} else {
|
||||
println!("{:?} dir already exists.", { dir_path });
|
||||
}
|
||||
let base_path = Path::new(dir_path);
|
||||
let images_path = base_path.join("images");
|
||||
std::fs::create_dir_all(&images_path)?;
|
||||
|
||||
let image_path = format!("{}/images", dir_path);
|
||||
if !std::fs::exists(&image_path)? {
|
||||
fs::create_dir(&image_path)?;
|
||||
} else {
|
||||
println!("{:?} dir already exists.", { image_path });
|
||||
}
|
||||
let db_path = base_path.join("clipboard.db");
|
||||
|
||||
let db_path = format!("{}/clipboard.db", dir_path);
|
||||
let conn = Connection::open(&db_path)?;
|
||||
|
||||
conn.execute(
|
||||
@ -51,7 +44,9 @@ impl Database {
|
||||
ClipboardData::Text(text) => ("text", text.clone()),
|
||||
ClipboardData::Image(img) => {
|
||||
if let Some(bytes) = &img.bytes {
|
||||
let img_path = format!("{}/images/{}.png", self.dir_path, img.id);
|
||||
let img_path = Path::new(&self.dir_path)
|
||||
.join("images")
|
||||
.join(format!("{}.png", img.id));
|
||||
fs::write(&img_path, bytes)?;
|
||||
}
|
||||
("image", img.id.to_string())
|
||||
@ -91,18 +86,6 @@ impl Database {
|
||||
ClipboardData::Image(Image { id, bytes: None })
|
||||
};
|
||||
|
||||
// let data = if ty == "text" {
|
||||
// ClipboardData::Text(content)
|
||||
// } else {
|
||||
// let id = Uuid::parse_str(&content)?;
|
||||
// let img_path = format!("{}/images/{}.png", self.dir_path, id);
|
||||
// let bytes = fs::read(&img_path).unwrap_or_default();
|
||||
// ClipboardData::Image(Image {
|
||||
// bytes: Some(bytes),
|
||||
// id,
|
||||
// })
|
||||
// };
|
||||
|
||||
entries.push(ClipboardEntry {
|
||||
content: data,
|
||||
timestamp,
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
use std::{fs, io};
|
||||
use uuid::Uuid;
|
||||
@ -21,11 +22,16 @@ pub struct Image {
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn file_path(&self, base_dir: &str) -> PathBuf {
|
||||
std::path::Path::new(base_dir)
|
||||
.join("images")
|
||||
.join(format!("{}.png", self.id))
|
||||
}
|
||||
|
||||
pub fn load_bytes(&self, dir_path: &str) -> io::Result<Vec<u8>> {
|
||||
if let Some(b) = &self.bytes {
|
||||
return Ok(b.clone());
|
||||
}
|
||||
let img_path = format!("{}/images/{}.png", dir_path, self.id);
|
||||
fs::read(img_path)
|
||||
fs::read(self.file_path(dir_path))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
#[cfg(feature = "wayland")]
|
||||
pub mod wayland;
|
||||
#[cfg(feature = "x11")]
|
||||
pub mod x11;
|
||||
|
||||
@ -1,7 +1,44 @@
|
||||
use crate::database::Database;
|
||||
use crate::{database::Database, models::ClipboardEntry};
|
||||
use arboard::Clipboard;
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use std::{error::Error, sync::mpsc::channel};
|
||||
use wayland_clipboard_listener::{WlClipboardPasteStream, WlListenType};
|
||||
|
||||
pub fn start(db: Database, mut clipboard: Clipboard) -> Result<(), Box<dyn Error>> {
|
||||
let (tx, rx) = channel();
|
||||
|
||||
std::thread::spawn(
|
||||
move || match WlClipboardPasteStream::init(WlListenType::ListenOnCopy) {
|
||||
Ok(mut stream) => {
|
||||
for _ in stream.paste_stream().flatten() {
|
||||
std::thread::sleep(Duration::new(1, 0));
|
||||
if let Err(e) = tx.send(()) {
|
||||
eprintln!("{}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
for _ in rx {
|
||||
println!("Clipboard update!");
|
||||
// if let Ok(entry) = ClipboardEntry::new(&mut clipboard) {
|
||||
// if let Err(e) = db.append(entry) {
|
||||
// eprintln!("SQLite writing error: {}", e);
|
||||
// } else {
|
||||
// println!("SQLite edited!");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
match ClipboardEntry::new(&mut clipboard) {
|
||||
Ok(entry) => db.append(entry)?,
|
||||
Err(e) => eprintln!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user