rework sql

This commit is contained in:
2026-03-08 00:12:53 +01:00
parent 55e33bf497
commit 27ede7fc64
7 changed files with 2114 additions and 2 deletions

114
rklipd/src/database.rs Normal file
View File

@ -0,0 +1,114 @@
use crate::models::{ClipboardData, ClipboardEntry, Image};
use rusqlite::Connection;
use std::error::Error;
use std::fs;
use std::time::{Duration, UNIX_EPOCH};
use uuid::Uuid;
pub struct Database {
conn: Connection,
dir_path: String,
}
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 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 = format!("{}/clipboard.db", dir_path);
let conn = Connection::open(&db_path)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT NOT NULL,
content TEXT NOT NULL,
timestamp INTEGER NOT NULL
)",
[],
)?;
Ok(Self {
conn,
dir_path: dir_path.to_string(),
})
}
pub fn append(&self, entry: ClipboardEntry) -> Result<(), Box<dyn Error>> {
let timestamp_millis = entry.timestamp.duration_since(UNIX_EPOCH)?.as_millis() as i64;
let (entry_type, content) = match &entry.content {
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);
fs::write(&img_path, bytes)?;
}
("image", img.id.to_string())
}
};
self.conn.execute(
"INSERT INTO history (type, content, timestamp) VALUES (?1, ?2, ?3)",
(entry_type, content, timestamp_millis),
)?;
Ok(())
}
pub fn read_history(&self) -> Result<Vec<ClipboardEntry>, Box<dyn Error>> {
let mut stmt = self
.conn
.prepare("SELECT type, content, timestamp FROM history ORDER BY timestamp ASC")?;
let rows = stmt.query_map([], |row| {
let ty: String = row.get(0)?;
let content: String = row.get(1)?;
let timestamp: i64 = row.get(2)?;
Ok((ty, content, timestamp))
})?;
let mut entries = Vec::new();
for row in rows {
let (ty, content, timestamp) = row?;
let timestamp = UNIX_EPOCH + Duration::from_millis(timestamp as u64);
let data = if ty == "text" {
ClipboardData::Text(content)
} else {
let id = Uuid::parse_str(&content)?;
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,
});
}
Ok(entries)
}
}