optimisations

This commit is contained in:
2026-03-08 14:45:26 +01:00
parent d46c35c49f
commit 05ced3b7ea
5 changed files with 182 additions and 100 deletions

View File

@ -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,