diff --git a/Cargo.lock b/Cargo.lock index 15008fe..fba6a8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1184,6 +1184,7 @@ dependencies = [ "lazy_static", "nats", "prost", + "rand", "reqwest", "serde", "serde_json", diff --git a/crates/flux-releaser/Cargo.toml b/crates/flux-releaser/Cargo.toml index 24e4a76..578a7f9 100644 --- a/crates/flux-releaser/Cargo.toml +++ b/crates/flux-releaser/Cargo.toml @@ -23,6 +23,7 @@ nats = "0.24.1" walkdir = "2.4.0" tar = "0.4.40" tokio-stream = { version = "0.1.14", features = ["full"] } +rand = "0.8.5" [build-dependencies] tonic-build = "0.11.0" diff --git a/crates/flux-releaser/src/services/file_store.rs b/crates/flux-releaser/src/services/file_store.rs index 79f39fc..58e8a69 100644 --- a/crates/flux-releaser/src/services/file_store.rs +++ b/crates/flux-releaser/src/services/file_store.rs @@ -72,4 +72,32 @@ impl FileStore { Ok(archive_path) } + + pub async fn get_temp(&self, artifact_id: UploadArtifactID) -> anyhow::Result { + tracing::trace!("getting archive: {}", artifact_id.to_string()); + + let archive_name = format!("temp/{}.tar", &artifact_id.to_string()); + + let obj = self + .client + .get_object() + .bucket("mybucket") + .key(&archive_name) + .send() + .await?; + + let archive_path = temp_dir() + .join("flux_releaser") + .join("downloads/cache") + .join(&archive_name); + tokio::fs::create_dir_all(archive_path.parent().unwrap()).await?; + let mut archive_file = tokio::fs::File::create(&archive_path).await?; + let mut buf_reader = BufReader::new(obj.body.into_async_read()); + + tokio::io::copy(&mut buf_reader, &mut archive_file).await?; + + tracing::debug!("created archive: {}", archive_path.display()); + + Ok(archive_path) + } } diff --git a/crates/flux-releaser/src/services/release_manager/models.rs b/crates/flux-releaser/src/services/release_manager/models.rs index 47c09f5..0f6f480 100644 --- a/crates/flux-releaser/src/services/release_manager/models.rs +++ b/crates/flux-releaser/src/services/release_manager/models.rs @@ -51,6 +51,16 @@ impl From for UploadArtifactID { } } +impl TryFrom for UploadArtifactID { + type Error = anyhow::Error; + + fn try_from(value: String) -> Result { + let uuid = uuid::Uuid::parse_str(&value)?; + + Ok(Self(uuid)) + } +} + impl Deref for UploadArtifactID { type Target = uuid::Uuid; diff --git a/crates/flux-releaser/tests/publish_artifacts.rs b/crates/flux-releaser/tests/publish_artifacts.rs index acadb6c..0375446 100644 --- a/crates/flux-releaser/tests/publish_artifacts.rs +++ b/crates/flux-releaser/tests/publish_artifacts.rs @@ -12,6 +12,7 @@ use flux_releaser::{ }, services::file_store::extensions::FileStoreExt, }; +use rand::{thread_rng, Rng}; use tokio::{net::TcpListener, runtime::Runtime, time::sleep}; use uuid::Uuid; @@ -144,7 +145,9 @@ async fn can_upload_artifact() -> anyhow::Result<()> { let mut client = FluxReleaserClient::connect(format!("http://{}", endpoints.grpc)).await?; - let bytes: [u8; 10000] = [0; 10000]; + let mut bytes: [u8; 10000] = [0; 10000]; + + thread_rng().fill(&mut bytes[..]); let chunks = bytes .chunks(bytes.len() / 100) @@ -157,7 +160,14 @@ async fn can_upload_artifact() -> anyhow::Result<()> { let resp = client.upload_artifact(req).await?; - assert!(!resp.into_inner().upload_id.is_empty()); + let upload_id = resp.into_inner().upload_id; + assert!(!upload_id.is_empty()); + + let actual_path = app.file_store().get_temp(upload_id.try_into()?).await?; + + let actual_content = tokio::fs::read(actual_path).await?; + + assert_eq!(&bytes[..], actual_content.as_slice()); Ok(()) }