plumb all known attachments via dbus if known
This commit is contained in:
@@ -13,6 +13,7 @@ use kordophone_db::database::DatabaseAccess;
|
||||
|
||||
use crate::daemon::events::Event;
|
||||
use crate::daemon::events::Reply;
|
||||
use crate::daemon::models::Attachment;
|
||||
use crate::daemon::Daemon;
|
||||
|
||||
use std::sync::Arc;
|
||||
@@ -26,23 +27,6 @@ mod target {
|
||||
pub static ATTACHMENTS: &str = "attachments";
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Attachment {
|
||||
pub guid: String,
|
||||
pub base_path: PathBuf,
|
||||
}
|
||||
|
||||
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.
|
||||
@@ -75,8 +59,13 @@ pub struct AttachmentStore {
|
||||
}
|
||||
|
||||
impl AttachmentStore {
|
||||
pub fn new(data_dir: &PathBuf, database: Arc<Mutex<Database>>, daemon_event_sink: Sender<Event>) -> AttachmentStore {
|
||||
let store_path = data_dir.join("attachments");
|
||||
pub fn get_default_store_path() -> PathBuf {
|
||||
let data_dir = Daemon::get_data_dir().expect("Unable to get data path");
|
||||
data_dir.join("attachments")
|
||||
}
|
||||
|
||||
pub fn new(database: Arc<Mutex<Database>>, daemon_event_sink: Sender<Event>) -> AttachmentStore {
|
||||
let store_path = Self::get_default_store_path();
|
||||
log::info!(target: target::ATTACHMENTS, "Attachment store path: {}", store_path.display());
|
||||
|
||||
// Create the attachment store if it doesn't exist
|
||||
@@ -98,11 +87,16 @@ impl AttachmentStore {
|
||||
self.event_sink.take().unwrap()
|
||||
}
|
||||
|
||||
fn get_attachment(&self, guid: &String, preview: bool) -> Attachment {
|
||||
let base_path = self.store_path.join(guid);
|
||||
fn get_attachment(&self, guid: &String) -> Attachment {
|
||||
Self::get_attachment_impl(&self.store_path, guid)
|
||||
}
|
||||
|
||||
pub fn get_attachment_impl(store_path: &PathBuf, guid: &String) -> Attachment {
|
||||
let base_path = store_path.join(guid);
|
||||
Attachment {
|
||||
guid: guid.to_owned(),
|
||||
base_path: base_path,
|
||||
metadata: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +141,7 @@ impl AttachmentStore {
|
||||
|
||||
match event {
|
||||
AttachmentStoreEvent::QueueDownloadAttachment(guid, preview) => {
|
||||
let attachment = self.get_attachment(&guid, preview);
|
||||
let attachment = self.get_attachment(&guid);
|
||||
if !attachment.is_downloaded(preview) {
|
||||
self.download_attachment(&attachment, preview).await.unwrap_or_else(|e| {
|
||||
log::error!(target: target::ATTACHMENTS, "Error downloading attachment: {}", e);
|
||||
@@ -158,7 +152,7 @@ impl AttachmentStore {
|
||||
}
|
||||
|
||||
AttachmentStoreEvent::GetAttachmentInfo(guid, reply) => {
|
||||
let attachment = self.get_attachment(&guid, false);
|
||||
let attachment = self.get_attachment(&guid);
|
||||
reply.send(attachment).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user