From 0023e2aa70a5b3cb062068b913ad97f64020ca1a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 22 Dec 2024 21:13:22 +0000 Subject: [PATCH] feat: write downloaded attachments to temp directory --- src/import/attachment.rs | 12 +++++++---- src/lib.rs | 6 ++++-- src/nextcloud/tests/board/create_label.rs | 1 + src/nextcloud/tests/card/add_attachment.rs | 1 + src/nextcloud/tests/card/add_label.rs | 1 + src/nextcloud/tests/card/create.rs | 1 + src/nextcloud/tests/card/get.rs | 1 + src/nextcloud/tests/deck/get.rs | 1 + src/tests/check.rs | 1 + src/tests/given.rs | 1 + src/trello/attachment.rs | 7 ++++++- src/trello/client.rs | 24 +++++++++++++++------- src/trello/tests/attachment/get.rs | 1 + src/trello/tests/attachment/save.rs | 1 + src/trello/tests/board/get.rs | 1 + src/trello/tests/card/get.rs | 1 + src/trello/tests/member/get.rs | 1 + src/trello/tests/stack/get.rs | 1 + 18 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/import/attachment.rs b/src/import/attachment.rs index 9b628ab..145fc05 100644 --- a/src/import/attachment.rs +++ b/src/import/attachment.rs @@ -1,6 +1,4 @@ // -use std::path::PathBuf; - use kameo::{mailbox::unbounded::UnboundedMailbox, Actor}; use crate::{ @@ -47,16 +45,21 @@ impl Actor for ImportAttachmentActor { this.trello_attachment.name ); // - - - download the attachment from trello + let dir_path = this.ctx.temp_fs.base().join(&*this.trello_attachment.id); + let dir = this.ctx.temp_fs.dir(&dir_path); + dir.create()?; + let file_path = dir_path.join(&*this.trello_attachment.name); let attachment_path = this .ctx .trello_client() .save_attachment( &this.trello_card_id, &this.trello_attachment.id, - Some(&PathBuf::from(&(*this.trello_attachment.id))), + Some(&file_path), + &this.ctx.temp_fs, ) .await?; - let attachment_file = this.ctx.fs.file(&attachment_path); + let attachment_file = this.ctx.temp_fs.file(&attachment_path); // - - - upload the attachment to nextcloud card let attachment = this .ctx @@ -78,6 +81,7 @@ impl Actor for ImportAttachmentActor { ); // delete local copy of attachment attachment_file.remove()?; + dir.remove()?; Ok(actor_ref.stop_gracefully().await?) }); diff --git a/src/lib.rs b/src/lib.rs index 75cca03..c06d6b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ // use std::path::PathBuf; +use crate::{nextcloud::client::DeckClient, trello::client::TrelloClient}; use clap::Parser; use color_eyre::eyre::eyre; use config::AppConfig; +use kxio::fs::TempFileSystem; use kxio::{fs::FileSystem, kxeprintln as e, kxprintln as p, net::Net, print::Printer}; -use crate::{nextcloud::client::DeckClient, trello::client::TrelloClient}; - use execute::Execute; mod api_result; @@ -81,6 +81,7 @@ impl From for Ctx { #[derive(Clone)] pub(crate) struct FullCtx { pub fs: FileSystem, + pub temp_fs: TempFileSystem, pub net: Net, pub prt: Printer, pub cfg: AppConfig, @@ -128,6 +129,7 @@ pub async fn run(ctx: &Ctx, commands: &Commands) -> color_eyre::Result<()> { .command .execute(&FullCtx { fs: ctx.fs.clone(), + temp_fs: kxio::fs::temp()?, net: ctx.net.clone(), prt: ctx.prt.clone(), cfg, diff --git a/src/nextcloud/tests/board/create_label.rs b/src/nextcloud/tests/board/create_label.rs index e5ad4d2..ff71d45 100644 --- a/src/nextcloud/tests/board/create_label.rs +++ b/src/nextcloud/tests/board/create_label.rs @@ -34,6 +34,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/nextcloud/tests/card/add_attachment.rs b/src/nextcloud/tests/card/add_attachment.rs index bf7ce2e..327313e 100644 --- a/src/nextcloud/tests/card/add_attachment.rs +++ b/src/nextcloud/tests/card/add_attachment.rs @@ -54,6 +54,7 @@ fn ctx_path() -> (FullCtx, PathBuf, TempFileSystem) { ( FullCtx { fs: fs.as_real(), + temp_fs: temp_fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/nextcloud/tests/card/add_label.rs b/src/nextcloud/tests/card/add_label.rs index 7a1f6d6..7593f4d 100644 --- a/src/nextcloud/tests/card/add_label.rs +++ b/src/nextcloud/tests/card/add_label.rs @@ -42,6 +42,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/nextcloud/tests/card/create.rs b/src/nextcloud/tests/card/create.rs index 172e0fd..fee09ca 100644 --- a/src/nextcloud/tests/card/create.rs +++ b/src/nextcloud/tests/card/create.rs @@ -33,6 +33,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/nextcloud/tests/card/get.rs b/src/nextcloud/tests/card/get.rs index fb15550..e5569e9 100644 --- a/src/nextcloud/tests/card/get.rs +++ b/src/nextcloud/tests/card/get.rs @@ -38,6 +38,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/nextcloud/tests/deck/get.rs b/src/nextcloud/tests/deck/get.rs index 08cdef6..a25a870 100644 --- a/src/nextcloud/tests/deck/get.rs +++ b/src/nextcloud/tests/deck/get.rs @@ -21,6 +21,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/tests/check.rs b/src/tests/check.rs index c848ffd..12f7631 100644 --- a/src/tests/check.rs +++ b/src/tests/check.rs @@ -63,6 +63,7 @@ async fn test_full_ctx_clients() -> Result<()> { let full_ctx = FullCtx { fs: ctx.fs, + temp_fs: fs.clone(), net: ctx.net, prt: ctx.prt, cfg: config, diff --git a/src/tests/given.rs b/src/tests/given.rs index d68a45c..32d3428 100644 --- a/src/tests/given.rs +++ b/src/tests/given.rs @@ -47,6 +47,7 @@ pub(crate) fn a_nextcloud_config() -> NextcloudConfig { pub(crate) fn a_full_context(fs: TempFileSystem, mock_net: MockNet) -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: a_printer(), cfg: AppConfig { diff --git a/src/trello/attachment.rs b/src/trello/attachment.rs index 3ca845b..09fa215 100644 --- a/src/trello/attachment.rs +++ b/src/trello/attachment.rs @@ -55,7 +55,12 @@ impl Execute for TrelloAttachmentCommand { let trello_attachment_id = TrelloAttachmentId::new(attachment_id); let file_name = ctx .trello_client() - .save_attachment(&trello_card_id, &trello_attachment_id, file_name.as_ref()) + .save_attachment( + &trello_card_id, + &trello_attachment_id, + file_name.as_ref(), + &ctx.fs, + ) .await?; p!(ctx.prt, "Wrote: {}", file_name.display()); Ok(()) diff --git a/src/trello/client.rs b/src/trello/client.rs index 9630f0c..cf25921 100644 --- a/src/trello/client.rs +++ b/src/trello/client.rs @@ -2,7 +2,10 @@ use std::{collections::HashMap, path::PathBuf}; use color_eyre::eyre::Context; -use kxio::net::{Net, ReqBuilder}; +use kxio::{ + fs::FileSystem, + net::{Net, ReqBuilder}, +}; use crate::{ api_result::APIResult, @@ -72,13 +75,15 @@ impl<'ctx> TrelloClient<'ctx> { card_id: &TrelloCardId, attachment_id: &TrelloAttachmentId, file_name: Option<&PathBuf>, + fs: &FileSystem, ) -> color_eyre::Result { let attachment = self.card_attachment(card_id, attachment_id).await.result?; let url = attachment.url; - let file_name = file_name - .cloned() - .unwrap_or_else(|| PathBuf::from((*attachment.file_name).clone())); - let file_name = self.ctx.fs.base().join(file_name); + let file_name = fs.base().join( + file_name + .cloned() + .unwrap_or_else(|| PathBuf::from((*attachment.file_name).clone())), + ); let resp = with_exponential_backoff!( &self.ctx, self.ctx @@ -93,8 +98,13 @@ impl<'ctx> TrelloClient<'ctx> { .map_err(Into::into) ) .context("downloading attachment")?; - let file = self.ctx.fs.file(&file_name); - file.write(resp).context("writing to disk")?; + let file = fs.file(&file_name); + file.write(resp) + .map_err(|e| { + crate::e!(self.ctx.prt, "{}", e); + e + }) + .with_context(|| f!("writing to disk: {} -> {}", file_name.display(), file))?; Ok(file_name) } } diff --git a/src/trello/tests/attachment/get.rs b/src/trello/tests/attachment/get.rs index 5e5af88..0c6f0b0 100644 --- a/src/trello/tests/attachment/get.rs +++ b/src/trello/tests/attachment/get.rs @@ -50,6 +50,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/trello/tests/attachment/save.rs b/src/trello/tests/attachment/save.rs index 919b9c9..36a0f7a 100644 --- a/src/trello/tests/attachment/save.rs +++ b/src/trello/tests/attachment/save.rs @@ -82,6 +82,7 @@ fn ctx_fs() -> (FullCtx, TempFileSystem) { ( FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/trello/tests/board/get.rs b/src/trello/tests/board/get.rs index 02c4a52..678d888 100644 --- a/src/trello/tests/board/get.rs +++ b/src/trello/tests/board/get.rs @@ -35,6 +35,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/trello/tests/card/get.rs b/src/trello/tests/card/get.rs index 488be6d..55edd4a 100644 --- a/src/trello/tests/card/get.rs +++ b/src/trello/tests/card/get.rs @@ -35,6 +35,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/trello/tests/member/get.rs b/src/trello/tests/member/get.rs index fd0de4d..68e7472 100644 --- a/src/trello/tests/member/get.rs +++ b/src/trello/tests/member/get.rs @@ -27,6 +27,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig { diff --git a/src/trello/tests/stack/get.rs b/src/trello/tests/stack/get.rs index f35ea90..afb7ad3 100644 --- a/src/trello/tests/stack/get.rs +++ b/src/trello/tests/stack/get.rs @@ -37,6 +37,7 @@ fn ctx() -> FullCtx { FullCtx { fs: fs.as_real(), + temp_fs: fs.clone(), net: mock_net.into(), prt: given::a_printer(), cfg: AppConfig {