Implements attachment uploading
This commit is contained in:
@@ -52,6 +52,16 @@ pub enum Commands {
|
||||
conversation_id: String,
|
||||
text: String,
|
||||
},
|
||||
|
||||
/// Downloads an attachment from the server to the attachment store. Returns the path to the attachment.
|
||||
DownloadAttachment {
|
||||
attachment_id: String,
|
||||
},
|
||||
|
||||
/// Uploads an attachment to the server, returns upload guid.
|
||||
UploadAttachment {
|
||||
path: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -89,6 +99,8 @@ impl Commands {
|
||||
conversation_id,
|
||||
text,
|
||||
} => client.enqueue_outgoing_message(conversation_id, text).await,
|
||||
Commands::UploadAttachment { path } => client.upload_attachment(path).await,
|
||||
Commands::DownloadAttachment { attachment_id } => client.download_attachment(attachment_id).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,4 +237,48 @@ impl DaemonCli {
|
||||
KordophoneRepository::delete_all_conversations(&self.proxy())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to delete all conversations: {}", e))
|
||||
}
|
||||
|
||||
pub async fn download_attachment(&mut self, attachment_id: String) -> Result<()> {
|
||||
// Trigger download.
|
||||
KordophoneRepository::download_attachment(&self.proxy(), &attachment_id, false)?;
|
||||
|
||||
// Get attachment info.
|
||||
let attachment_info = KordophoneRepository::get_attachment_info(&self.proxy(), &attachment_id)?;
|
||||
let (path, preview_path, downloaded, preview_downloaded) = attachment_info;
|
||||
|
||||
if downloaded {
|
||||
println!("Attachment already downloaded: {}", path);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("Downloading attachment: {}", attachment_id);
|
||||
|
||||
// Attach to the signal that the attachment has been downloaded.
|
||||
let _id = self.proxy().match_signal(
|
||||
move |h: dbus_interface::NetBuzzertKordophoneRepositoryAttachmentDownloadCompleted, _: &Connection, _: &dbus::message::Message| {
|
||||
println!("Signal: Attachment downloaded: {}", path);
|
||||
std::process::exit(0);
|
||||
},
|
||||
);
|
||||
|
||||
let _id = self.proxy().match_signal(
|
||||
|h: dbus_interface::NetBuzzertKordophoneRepositoryAttachmentDownloadFailed, _: &Connection, _: &dbus::message::Message| {
|
||||
println!("Signal: Attachment download failed: {}", h.attachment_id);
|
||||
std::process::exit(1);
|
||||
},
|
||||
);
|
||||
|
||||
// Wait for the signal.
|
||||
loop {
|
||||
self.conn.process(std::time::Duration::from_millis(1000))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn upload_attachment(&mut self, path: String) -> Result<()> {
|
||||
let upload_guid = KordophoneRepository::upload_attachment(&self.proxy(), &path)?;
|
||||
println!("Upload GUID: {}", upload_guid);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user