细节调整及优化(26.8.3)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
//!
|
||||
//! 表结构见 `init_db`。所有方法线程安全(内部 Mutex 包裹 Connection)。
|
||||
|
||||
use base64::Engine as _;
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use specta::Type;
|
||||
use std::fs;
|
||||
@@ -38,6 +39,8 @@ pub struct NewItem {
|
||||
pub kind: String,
|
||||
pub content: Option<String>,
|
||||
pub blob: Option<Vec<u8>>,
|
||||
/// 图片缩略图 PNG(仅 image 类型,供弹窗悬停预览)
|
||||
pub thumb: Option<Vec<u8>>,
|
||||
pub preview: String,
|
||||
pub size: i64,
|
||||
pub hash: String,
|
||||
@@ -88,6 +91,19 @@ impl Storage {
|
||||
CREATE INDEX IF NOT EXISTS idx_hash ON clipboard_history(hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_kind ON clipboard_history(kind);",
|
||||
);
|
||||
// 迁移:老库无 thumb 列(图片缩略图,供弹窗悬停预览),补列
|
||||
let has_thumb = conn
|
||||
.prepare("PRAGMA table_info(clipboard_history)")
|
||||
.ok()
|
||||
.map(|mut stmt| {
|
||||
stmt.query_map([], |r| r.get::<_, String>(1))
|
||||
.map(|rows| rows.filter_map(|c| c.ok()).any(|c| c == "thumb"))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.unwrap_or(false);
|
||||
if !has_thumb {
|
||||
let _ = conn.execute("ALTER TABLE clipboard_history ADD COLUMN thumb BLOB", []);
|
||||
}
|
||||
}
|
||||
|
||||
/// 插入新条目;若 dedup 为 true 且 hash 已存在则仅更新 created_at,返回条目 id。
|
||||
@@ -120,12 +136,13 @@ impl Storage {
|
||||
let now = now_ms();
|
||||
let res = conn.execute(
|
||||
"INSERT INTO clipboard_history
|
||||
(kind, content, blob, preview, size, hash, pinned, pinned_order, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, 0, NULL, ?7)",
|
||||
(kind, content, blob, thumb, preview, size, hash, pinned, pinned_order, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 0, NULL, ?8)",
|
||||
params![
|
||||
item.kind,
|
||||
item.content,
|
||||
item.blob.as_deref(),
|
||||
item.thumb.as_deref(),
|
||||
item.preview,
|
||||
item.size,
|
||||
item.hash,
|
||||
@@ -249,6 +266,26 @@ impl Storage {
|
||||
})
|
||||
}
|
||||
|
||||
/// 获取图片缩略图 PNG base64(弹窗悬停预览用)。
|
||||
/// 老数据无缩略图时回退为 `thumb_from_dib` 现场生成。
|
||||
pub fn get_thumb(&self, id: i64, thumb_from_dib: impl Fn(&[u8]) -> Option<Vec<u8>>) -> Option<String> {
|
||||
let (thumb, blob) = {
|
||||
let conn = self.conn.lock().ok()?;
|
||||
conn.query_row(
|
||||
"SELECT thumb, blob FROM clipboard_history WHERE id = ?1 AND kind = 'image'",
|
||||
params![id],
|
||||
|r| Ok((r.get::<_, Option<Vec<u8>>>(0)?, r.get::<_, Option<Vec<u8>>>(1)?)),
|
||||
)
|
||||
.ok()?
|
||||
};
|
||||
// conn 已在此处释放
|
||||
let png = match thumb {
|
||||
Some(t) if !t.is_empty() => Some(t),
|
||||
_ => blob.as_deref().and_then(|b| thumb_from_dib(b)),
|
||||
};
|
||||
png.map(|p| base64::engine::general_purpose::STANDARD.encode(&p))
|
||||
}
|
||||
|
||||
/// 获取原始字段供 copy_back 写回(避免 base64 转换开销)
|
||||
pub fn get_raw_for_copy(&self, id: i64) -> Option<(String, Option<String>, Option<Vec<u8>>)> {
|
||||
let conn = self.conn.lock().ok()?;
|
||||
|
||||
Reference in New Issue
Block a user