Some checks reported errors
continuous-integration/drone/push Build encountered an error
Signed-off-by: kjuulh <contact@kjuulh.io>
109 lines
3.2 KiB
Rust
109 lines
3.2 KiB
Rust
use std::{env::temp_dir, path::PathBuf};
|
|
|
|
use super::release_manager::models::{ArtifactID, UploadArtifactID};
|
|
|
|
pub mod extensions;
|
|
|
|
#[derive(Clone)]
|
|
pub struct FileStore {
|
|
client: aws_sdk_s3::Client,
|
|
|
|
bucket: String,
|
|
}
|
|
|
|
use aws_sdk_s3::primitives::ByteStream;
|
|
use tokio::io::BufReader;
|
|
|
|
impl FileStore {
|
|
pub fn new(client: aws_sdk_s3::Client) -> Self {
|
|
Self {
|
|
client,
|
|
bucket: std::env::var("BUCKET_NAME").unwrap_or("flux-releaser".into()),
|
|
}
|
|
}
|
|
|
|
pub async fn upload_file(&self, artifact_id: ArtifactID, file: PathBuf) -> anyhow::Result<()> {
|
|
tracing::trace!("uploading files: {}", artifact_id.to_string());
|
|
|
|
self.client
|
|
.put_object()
|
|
.bucket(&self.bucket)
|
|
.key(format!("archives/{}.tar", &artifact_id.to_string()))
|
|
.body(ByteStream::from_path(file).await?)
|
|
.send()
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn upload_temp(&self, id: UploadArtifactID, file: PathBuf) -> anyhow::Result<()> {
|
|
tracing::trace!("uploading temp files: {}", id.to_string());
|
|
|
|
self.client
|
|
.put_object()
|
|
.bucket(&self.bucket)
|
|
.key(format!("temp/{}.tar", &id.to_string()))
|
|
.body(ByteStream::from_path(file).await?)
|
|
.send()
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn get_archive(&self, artifact_id: ArtifactID) -> anyhow::Result<PathBuf> {
|
|
tracing::trace!("getting archive: {}", artifact_id.to_string());
|
|
|
|
let archive_name = format!("archives/{}.tar", &artifact_id.to_string());
|
|
|
|
let obj = self
|
|
.client
|
|
.get_object()
|
|
.bucket(&self.bucket)
|
|
.key(&archive_name)
|
|
.send()
|
|
.await?;
|
|
|
|
let archive_path = temp_dir()
|
|
.join("flux_releaser")
|
|
.join("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)
|
|
}
|
|
|
|
pub async fn get_temp(&self, artifact_id: UploadArtifactID) -> anyhow::Result<PathBuf> {
|
|
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(&self.bucket)
|
|
.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)
|
|
}
|
|
}
|