better d-bus interface for attachments
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use std::{
|
||||
io::{BufReader, BufWriter, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
io::{BufWriter, Write},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use anyhow::{Error, Result};
|
||||
use futures_util::{poll, StreamExt};
|
||||
use anyhow::Result;
|
||||
use futures_util::StreamExt;
|
||||
use kordophone::APIInterface;
|
||||
use thiserror::Error;
|
||||
use tokio::pin;
|
||||
@@ -64,20 +64,23 @@ impl AttachmentStore {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_attachent<C, F>(
|
||||
pub async fn download_attachment<C, F, Fut>(
|
||||
&mut self,
|
||||
attachment: &Attachment,
|
||||
mut client_factory: F,
|
||||
) -> Result<()>
|
||||
where
|
||||
C: APIInterface,
|
||||
F: AsyncFnMut() -> Result<C>,
|
||||
F: FnMut() -> Fut,
|
||||
Fut: std::future::Future<Output = Result<C>>,
|
||||
{
|
||||
if attachment.downloaded {
|
||||
log::error!(target: target::ATTACHMENTS, "Attempted to download existing attachment.");
|
||||
log::info!(target: target::ATTACHMENTS, "Attachment already downloaded: {}", attachment.guid);
|
||||
return Err(AttachmentStoreError::AttachmentAlreadyDownloaded.into());
|
||||
}
|
||||
|
||||
log::info!(target: target::ATTACHMENTS, "Starting download for attachment: {}", attachment.guid);
|
||||
|
||||
// Create temporary file first, we'll atomically swap later.
|
||||
assert!(!std::fs::exists(&attachment.path).unwrap());
|
||||
let file = std::fs::File::create(&attachment.path)?;
|
||||
@@ -85,7 +88,7 @@ impl AttachmentStore {
|
||||
|
||||
log::trace!(target: target::ATTACHMENTS, "Created attachment file at {}", &attachment.path.display());
|
||||
|
||||
let mut client = (client_factory)().await?;
|
||||
let mut client = client_factory().await?;
|
||||
let stream = client
|
||||
.fetch_attachment_data(&attachment.guid)
|
||||
.await
|
||||
@@ -99,6 +102,13 @@ impl AttachmentStore {
|
||||
writer.write(data.as_ref())?;
|
||||
}
|
||||
|
||||
log::info!(target: target::ATTACHMENTS, "Completed download for attachment: {}", attachment.guid);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if an attachment should be downloaded
|
||||
pub fn should_download(&self, attachment_id: &str) -> bool {
|
||||
let attachment = self.get_attachment(&attachment_id.to_string());
|
||||
!attachment.downloaded
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,12 @@ pub enum Event {
|
||||
/// - reply: Reply of the attachment object, if known.
|
||||
GetAttachment(String, Reply<Attachment>),
|
||||
|
||||
/// Downloads an attachment from the server.
|
||||
/// Parameters:
|
||||
/// - attachment_id: The attachment ID to download
|
||||
/// - reply: Reply indicating success or failure
|
||||
DownloadAttachment(String, Reply<()>),
|
||||
|
||||
/// Delete all conversations from the database.
|
||||
DeleteAllConversations(Reply<()>),
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ pub mod target {
|
||||
pub static EVENT: &str = "event";
|
||||
pub static SETTINGS: &str = "settings";
|
||||
pub static UPDATES: &str = "updates";
|
||||
pub static ATTACHMENTS: &str = "attachments";
|
||||
}
|
||||
|
||||
pub struct Daemon {
|
||||
@@ -284,6 +285,12 @@ impl Daemon {
|
||||
let attachment = self.attachment_store.get_attachment(&guid);
|
||||
reply.send(attachment).unwrap();
|
||||
}
|
||||
|
||||
Event::DownloadAttachment(attachment_id, reply) => {
|
||||
// For now, just return success - we'll implement the actual download logic later
|
||||
log::info!(target: target::ATTACHMENTS, "Download requested for attachment: {}", attachment_id);
|
||||
reply.send(()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user