Private
Public Access
1
0

AttachmentStore now has its own runloop, can download attachments

This commit is contained in:
2025-06-05 20:19:34 -07:00
parent 595c7a764b
commit cbc7679f58
8 changed files with 160 additions and 70 deletions

View File

@@ -280,8 +280,9 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
async fn fetch_attachment_data( async fn fetch_attachment_data(
&mut self, &mut self,
guid: &String, guid: &String,
preview: bool,
) -> Result<ResponseStream, Self::Error> { ) -> Result<ResponseStream, Self::Error> {
let endpoint = format!("attachment?guid={}", guid); let endpoint = format!("attachment?guid={}&preview={}", guid, preview);
self.response_with_body_retry(&endpoint, Method::GET, Body::empty, true) self.response_with_body_retry(&endpoint, Method::GET, Body::empty, true)
.await .await
.map(hyper::Response::into_body) .map(hyper::Response::into_body)

View File

@@ -49,6 +49,7 @@ pub trait APIInterface {
async fn fetch_attachment_data( async fn fetch_attachment_data(
&mut self, &mut self,
guid: &String, guid: &String,
preview: bool,
) -> Result<Self::ResponseStream, Self::Error>; ) -> Result<Self::ResponseStream, Self::Error>;
// (POST) /authenticate // (POST) /authenticate

View File

@@ -89,15 +89,27 @@
<method name="GetAttachmentInfo"> <method name="GetAttachmentInfo">
<arg type="s" name="attachment_id" direction="in"/> <arg type="s" name="attachment_id" direction="in"/>
<arg type="(sbu)" name="attachment_info" direction="out"/> <arg type="(ssbb)" name="attachment_info" direction="out"/>
<annotation name="org.freedesktop.DBus.DocString" <annotation name="org.freedesktop.DBus.DocString"
value="Returns attachment info: (file_path: string, downloaded: bool, file_size: uint64)"/> value="Returns attachment info:
- path: string
- preview_path: string
- downloaded: boolean
- preview_downloaded: boolean
"/>
</method> </method>
<method name="DownloadAttachment"> <method name="DownloadAttachment">
<arg type="s" name="attachment_id" direction="in"/> <arg type="s" name="attachment_id" direction="in"/>
<arg type="b" name="preview" direction="in"/>
<annotation name="org.freedesktop.DBus.DocString" <annotation name="org.freedesktop.DBus.DocString"
value="Initiates download of the specified attachment if not already downloaded."/> value="Initiates download of the specified attachment if not already downloaded.
Arguments:
attachment_id: the attachment GUID
preview: whether to download the preview (true) or full attachment (false)
"/>
</method> </method>
<signal name="AttachmentDownloadStarted"> <signal name="AttachmentDownloadStarted">

View File

@@ -7,7 +7,20 @@ use anyhow::Result;
use futures_util::StreamExt; use futures_util::StreamExt;
use kordophone::APIInterface; use kordophone::APIInterface;
use thiserror::Error; use thiserror::Error;
use kordophone_db::database::Database;
use kordophone_db::database::DatabaseAccess;
use crate::daemon::events::Event;
use crate::daemon::events::Reply;
use crate::daemon::Daemon;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::pin; use tokio::pin;
use tokio::time::Duration;
mod target { mod target {
pub static ATTACHMENTS: &str = "attachments"; pub static ATTACHMENTS: &str = "attachments";
@@ -16,8 +29,31 @@ mod target {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Attachment { pub struct Attachment {
pub guid: String, pub guid: String,
pub path: PathBuf, pub base_path: PathBuf,
pub downloaded: bool, }
impl Attachment {
pub fn get_path(&self, preview: bool) -> PathBuf {
self.base_path.with_extension(if preview { "preview" } else { "full" })
}
pub fn is_downloaded(&self, preview: bool) -> bool {
std::fs::exists(&self.get_path(preview))
.expect(format!("Wasn't able to check for the existence of an attachment file path at {}", &self.get_path(preview).display()).as_str())
}
}
#[derive(Debug)]
pub enum AttachmentStoreEvent {
// Get the attachment info for a given attachment guid.
// Args: attachment guid, reply channel.
GetAttachmentInfo(String, Reply<Attachment>),
// Queue a download for a given attachment guid.
// Args:
// - attachment guid
// - preview: whether to download the preview (true) or full attachment (false)
QueueDownloadAttachment(String, bool),
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@@ -31,10 +67,15 @@ enum AttachmentStoreError {
pub struct AttachmentStore { pub struct AttachmentStore {
store_path: PathBuf, store_path: PathBuf,
database: Arc<Mutex<Database>>,
daemon_event_sink: Sender<Event>,
event_source: Receiver<AttachmentStoreEvent>,
event_sink: Option<Sender<AttachmentStoreEvent>>,
} }
impl AttachmentStore { impl AttachmentStore {
pub fn new(data_dir: &PathBuf) -> AttachmentStore { pub fn new(data_dir: &PathBuf, database: Arc<Mutex<Database>>, daemon_event_sink: Sender<Event>) -> AttachmentStore {
let store_path = data_dir.join("attachments"); let store_path = data_dir.join("attachments");
log::info!(target: target::ATTACHMENTS, "Attachment store path: {}", store_path.display()); log::info!(target: target::ATTACHMENTS, "Attachment store path: {}", store_path.display());
@@ -42,39 +83,31 @@ impl AttachmentStore {
std::fs::create_dir_all(&store_path) std::fs::create_dir_all(&store_path)
.expect("Wasn't able to create the attachment store path"); .expect("Wasn't able to create the attachment store path");
let (event_sink, event_source) = tokio::sync::mpsc::channel(100);
AttachmentStore { AttachmentStore {
store_path: store_path, store_path: store_path,
database: database,
daemon_event_sink: daemon_event_sink,
event_source: event_source,
event_sink: Some(event_sink),
} }
} }
pub fn get_attachment(&self, guid: &String) -> Attachment { pub fn get_event_sink(&mut self) -> Sender<AttachmentStoreEvent> {
let path = self.store_path.join(guid); self.event_sink.take().unwrap()
let path_exists = std::fs::exists(&path).expect( }
format!(
"Wasn't able to check for the existence of an attachment file path at {}",
&path.display()
)
.as_str(),
);
fn get_attachment(&self, guid: &String, preview: bool) -> Attachment {
let base_path = self.store_path.join(guid);
Attachment { Attachment {
guid: guid.to_owned(), guid: guid.to_owned(),
path: path, base_path: base_path,
downloaded: path_exists,
} }
} }
pub async fn download_attachment<C, F, Fut>( async fn download_attachment(&mut self, attachment: &Attachment, preview: bool) -> Result<()> {
&mut self, if attachment.is_downloaded(preview) {
attachment: &Attachment,
mut client_factory: F,
) -> Result<()>
where
C: APIInterface,
F: FnMut() -> Fut,
Fut: std::future::Future<Output = Result<C>>,
{
if attachment.downloaded {
log::info!(target: target::ATTACHMENTS, "Attachment already downloaded: {}", attachment.guid); log::info!(target: target::ATTACHMENTS, "Attachment already downloaded: {}", attachment.guid);
return Err(AttachmentStoreError::AttachmentAlreadyDownloaded.into()); return Err(AttachmentStoreError::AttachmentAlreadyDownloaded.into());
} }
@@ -82,15 +115,15 @@ impl AttachmentStore {
log::info!(target: target::ATTACHMENTS, "Starting download for attachment: {}", attachment.guid); log::info!(target: target::ATTACHMENTS, "Starting download for attachment: {}", attachment.guid);
// Create temporary file first, we'll atomically swap later. // Create temporary file first, we'll atomically swap later.
assert!(!std::fs::exists(&attachment.path).unwrap()); assert!(!std::fs::exists(&attachment.get_path(preview)).unwrap());
let file = std::fs::File::create(&attachment.path)?; let file = std::fs::File::create(&attachment.get_path(preview))?;
let mut writer = BufWriter::new(&file); let mut writer = BufWriter::new(&file);
log::trace!(target: target::ATTACHMENTS, "Created attachment file at {}", &attachment.path.display()); log::trace!(target: target::ATTACHMENTS, "Created attachment file at {}", &attachment.get_path(preview).display());
let mut client = client_factory().await?; let mut client = Daemon::get_client_impl(&mut self.database).await?;
let stream = client let stream = client
.fetch_attachment_data(&attachment.guid) .fetch_attachment_data(&attachment.guid, preview)
.await .await
.map_err(|e| AttachmentStoreError::APIClientError(format!("{:?}", e)))?; .map_err(|e| AttachmentStoreError::APIClientError(format!("{:?}", e)))?;
@@ -106,9 +139,31 @@ impl AttachmentStore {
Ok(()) Ok(())
} }
/// Check if an attachment should be downloaded pub async fn run(&mut self) {
pub fn should_download(&self, attachment_id: &str) -> bool { loop {
let attachment = self.get_attachment(&attachment_id.to_string()); tokio::select! {
!attachment.downloaded Some(event) = self.event_source.recv() => {
log::debug!(target: target::ATTACHMENTS, "Received attachment store event: {:?}", event);
match event {
AttachmentStoreEvent::QueueDownloadAttachment(guid, preview) => {
let attachment = self.get_attachment(&guid, preview);
if !attachment.is_downloaded(preview) {
self.download_attachment(&attachment, preview).await.unwrap_or_else(|e| {
log::error!(target: target::ATTACHMENTS, "Error downloading attachment: {}", e);
});
} else {
log::info!(target: target::ATTACHMENTS, "Attachment already downloaded: {}", guid);
}
}
AttachmentStoreEvent::GetAttachmentInfo(guid, reply) => {
let attachment = self.get_attachment(&guid, false);
reply.send(attachment).unwrap();
}
}
}
}
}
} }
} }

View File

@@ -65,8 +65,9 @@ pub enum Event {
/// Downloads an attachment from the server. /// Downloads an attachment from the server.
/// Parameters: /// Parameters:
/// - attachment_id: The attachment ID to download /// - attachment_id: The attachment ID to download
/// - preview: Whether to download the preview (true) or full attachment (false)
/// - reply: Reply indicating success or failure /// - reply: Reply indicating success or failure
DownloadAttachment(String, Reply<()>), DownloadAttachment(String, bool, Reply<()>),
/// Delete all conversations from the database. /// Delete all conversations from the database.
DeleteAllConversations(Reply<()>), DeleteAllConversations(Reply<()>),

View File

@@ -43,7 +43,8 @@ use post_office::PostOffice;
mod attachment_store; mod attachment_store;
pub use attachment_store::Attachment; pub use attachment_store::Attachment;
use attachment_store::AttachmentStore; pub use attachment_store::AttachmentStore;
pub use attachment_store::AttachmentStoreEvent;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum DaemonError { pub enum DaemonError {
@@ -75,7 +76,7 @@ pub struct Daemon {
outgoing_messages: HashMap<ConversationID, Vec<OutgoingMessage>>, outgoing_messages: HashMap<ConversationID, Vec<OutgoingMessage>>,
attachment_store: AttachmentStore, attachment_store_sink: Option<Sender<AttachmentStoreEvent>>,
version: String, version: String,
database: Arc<Mutex<Database>>, database: Arc<Mutex<Database>>,
@@ -105,9 +106,6 @@ impl Daemon {
let database_impl = Database::new(&database_path.to_string_lossy())?; let database_impl = Database::new(&database_path.to_string_lossy())?;
let database = Arc::new(Mutex::new(database_impl)); let database = Arc::new(Mutex::new(database_impl));
let data_path = Self::get_data_dir().expect("Unable to get data path");
let attachment_store = AttachmentStore::new(&data_path);
Ok(Self { Ok(Self {
version: "0.1.0".to_string(), version: "0.1.0".to_string(),
database, database,
@@ -118,7 +116,7 @@ impl Daemon {
post_office_sink, post_office_sink,
post_office_source: Some(post_office_source), post_office_source: Some(post_office_source),
outgoing_messages: HashMap::new(), outgoing_messages: HashMap::new(),
attachment_store: attachment_store, attachment_store_sink: None,
runtime, runtime,
}) })
} }
@@ -148,6 +146,14 @@ impl Daemon {
}); });
} }
// Attachment store
let data_path = Self::get_data_dir().expect("Unable to get data path");
let mut attachment_store = AttachmentStore::new(&data_path, self.database.clone(), self.event_sender.clone());
self.attachment_store_sink = Some(attachment_store.get_event_sink());
tokio::spawn(async move {
attachment_store.run().await;
});
while let Some(event) = self.event_receiver.recv().await { while let Some(event) = self.event_receiver.recv().await {
log::debug!(target: target::EVENT, "Received event: {:?}", event); log::debug!(target: target::EVENT, "Received event: {:?}", event);
self.handle_event(event).await; self.handle_event(event).await;
@@ -282,13 +288,24 @@ impl Daemon {
} }
Event::GetAttachment(guid, reply) => { Event::GetAttachment(guid, reply) => {
let attachment = self.attachment_store.get_attachment(&guid); self.attachment_store_sink
reply.send(attachment).unwrap(); .as_ref()
.unwrap()
.send(AttachmentStoreEvent::GetAttachmentInfo(guid, reply))
.await
.unwrap();
} }
Event::DownloadAttachment(attachment_id, reply) => { Event::DownloadAttachment(attachment_id, preview, reply) => {
// For now, just return success - we'll implement the actual download logic later log::info!(target: target::ATTACHMENTS, "Download requested for attachment: {}, preview: {}", &attachment_id, preview);
log::info!(target: target::ATTACHMENTS, "Download requested for attachment: {}", attachment_id);
self.attachment_store_sink
.as_ref()
.unwrap()
.send(AttachmentStoreEvent::QueueDownloadAttachment(attachment_id, preview))
.await
.unwrap();
reply.send(()).unwrap(); reply.send(()).unwrap();
} }
} }

View File

@@ -11,21 +11,18 @@ use crate::daemon::{
Attachment, DaemonResult, Attachment, DaemonResult,
}; };
use crate::dbus::endpoint::DbusRegistry;
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository; use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings; use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
#[derive(Clone)] #[derive(Clone)]
pub struct ServerImpl { pub struct ServerImpl {
event_sink: mpsc::Sender<Event>, event_sink: mpsc::Sender<Event>,
dbus_registry: DbusRegistry,
} }
impl ServerImpl { impl ServerImpl {
pub fn new(event_sink: mpsc::Sender<Event>, dbus_registry: DbusRegistry) -> Self { pub fn new(event_sink: mpsc::Sender<Event>) -> Self {
Self { Self {
event_sink: event_sink, event_sink: event_sink,
dbus_registry: dbus_registry,
} }
} }
@@ -187,28 +184,34 @@ impl DbusRepository for ServerImpl {
fn get_attachment_info( fn get_attachment_info(
&mut self, &mut self,
attachment_id: String, attachment_id: String,
) -> Result<(String, bool, u32), dbus::MethodErr> { ) -> Result<(String, String, bool, bool), dbus::MethodErr> {
self.send_event_sync(|r| Event::GetAttachment(attachment_id, r)) self.send_event_sync(|r| Event::GetAttachment(attachment_id, r))
.map(|attachment| { .map(|attachment| {
let file_size = if attachment.downloaded { let path = attachment.get_path(false);
std::fs::metadata(&attachment.path) let downloaded = attachment.is_downloaded(false);
.map(|m| m.len() as u32)
.unwrap_or(0) let preview_path = attachment.get_path(true);
} else { let preview_downloaded = attachment.is_downloaded(true);
0
};
( (
attachment.path.to_string_lossy().to_string(), // - path: string
attachment.downloaded, path.to_string_lossy().to_string(),
file_size,
// - preview_path: string
preview_path.to_string_lossy().to_string(),
// - downloaded: boolean
downloaded,
// - preview_downloaded: boolean
preview_downloaded,
) )
}) })
} }
fn download_attachment(&mut self, attachment_id: String) -> Result<(), dbus::MethodErr> { fn download_attachment(&mut self, attachment_id: String, preview: bool) -> Result<(), dbus::MethodErr> {
// For now, just trigger the download event - we'll implement the actual download logic later // For now, just trigger the download event - we'll implement the actual download logic later
self.send_event_sync(|r| Event::DownloadAttachment(attachment_id, r)) self.send_event_sync(|r| Event::DownloadAttachment(attachment_id, preview, r))
} }
} }

View File

@@ -58,7 +58,7 @@ async fn main() {
let dbus_registry = DbusRegistry::new(connection.clone()); let dbus_registry = DbusRegistry::new(connection.clone());
// Create and register server implementation // Create and register server implementation
let server = ServerImpl::new(daemon.event_sender.clone(), dbus_registry.clone()); let server = ServerImpl::new(daemon.event_sender.clone());
dbus_registry.register_object( dbus_registry.register_object(
interface::OBJECT_PATH, interface::OBJECT_PATH,