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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user