2026-07-11 13:16:10 +07:00
|
|
|
use rusqlite::{Connection, params};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
|
|
pub fn store_blob(conn: &Connection, session_id: &str, blob_key: &str, data: &[u8], mime_type: Option<&str>) -> Result<()> {
|
|
|
|
|
let created_at = chrono::Utc::now().timestamp_millis();
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT OR REPLACE INTO blobs (session_id, blob_key, data, mime_type, created_at) VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
|
|
|
params![session_id, blob_key, data, mime_type, created_at],
|
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn retrieve_blob(conn: &Connection, session_id: &str, blob_key: &str) -> Result<Option<Vec<u8>>> {
|
|
|
|
|
let result = conn.query_row(
|
|
|
|
|
"SELECT data FROM blobs WHERE session_id = ?1 AND blob_key = ?2",
|
|
|
|
|
params![session_id, blob_key],
|
|
|
|
|
|row| row.get(0),
|
|
|
|
|
);
|
|
|
|
|
match result {
|
|
|
|
|
Ok(data) => Ok(Some(data)),
|
|
|
|
|
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
|
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 01:27:12 +07:00
|
|
|
#[allow(dead_code)]
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn delete_blob(conn: &Connection, session_id: &str, blob_key: &str) -> Result<bool> {
|
|
|
|
|
let rows = conn.execute(
|
|
|
|
|
"DELETE FROM blobs WHERE session_id = ?1 AND blob_key = ?2",
|
|
|
|
|
params![session_id, blob_key],
|
|
|
|
|
)?;
|
|
|
|
|
Ok(rows > 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn list_blob_keys(conn: &Connection, session_id: &str) -> Result<Vec<String>> {
|
|
|
|
|
let mut stmt = conn.prepare(
|
|
|
|
|
"SELECT blob_key FROM blobs WHERE session_id = ?1 ORDER BY created_at ASC"
|
|
|
|
|
)?;
|
|
|
|
|
let rows = stmt.query_map(params![session_id], |row| {
|
|
|
|
|
row.get::<_, String>(0)
|
|
|
|
|
})?;
|
|
|
|
|
let mut keys = Vec::new();
|
|
|
|
|
for row in rows {
|
|
|
|
|
keys.push(row?);
|
|
|
|
|
}
|
|
|
|
|
Ok(keys)
|
|
|
|
|
}
|