Skip to content

Commit fd971e8

Browse files
committed
refactor: Use a FileUri struct instead of a String for the location
1 parent 8d5e1c7 commit fd971e8

File tree

6 files changed

+47
-38
lines changed

6 files changed

+47
-38
lines changed

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use slog::{debug, warn, Logger};
55
use std::sync::Arc;
66
use thiserror::Error;
77

8-
use crate::{
9-
file_uploaders::FileLocation, snapshotter::OngoingSnapshot, FileUploader, Snapshotter,
10-
};
8+
use crate::{file_uploaders::FileUri, snapshotter::OngoingSnapshot, FileUploader, Snapshotter};
119

1210
use super::ArtifactBuilder;
1311
use mithril_common::logging::LoggerExtensions;
@@ -88,7 +86,7 @@ impl CardanoImmutableFilesFullArtifactBuilder {
8886
async fn upload_snapshot_archive(
8987
&self,
9088
ongoing_snapshot: &OngoingSnapshot,
91-
) -> StdResult<Vec<FileLocation>> {
89+
) -> StdResult<Vec<FileUri>> {
9290
debug!(self.logger, ">> upload_snapshot_archive");
9391
let location = self
9492
.snapshot_uploader
@@ -157,7 +155,12 @@ impl ArtifactBuilder<CardanoDbBeacon, Snapshot> for CardanoImmutableFilesFullArt
157155
})?;
158156

159157
let snapshot = self
160-
.create_snapshot(beacon, &ongoing_snapshot, snapshot_digest, locations)
158+
.create_snapshot(
159+
beacon,
160+
&ongoing_snapshot,
161+
snapshot_digest,
162+
locations.into_iter().map(Into::into).collect(),
163+
)
161164
.await?;
162165

163166
Ok(snapshot)
@@ -173,7 +176,7 @@ mod tests {
173176
use mithril_common::{entities::CompressionAlgorithm, test_utils::fake_data};
174177

175178
use crate::{
176-
file_uploaders::MockFileUploader, test_tools::TestLogger, DumbUploader, DumbSnapshotter,
179+
file_uploaders::MockFileUploader, test_tools::TestLogger, DumbSnapshotter, DumbUploader,
177180
};
178181

179182
use super::*;
@@ -211,6 +214,7 @@ mod tests {
211214
let remote_locations = vec![dumb_snapshot_uploader
212215
.get_last_upload()
213216
.unwrap()
217+
.map(Into::into)
214218
.expect("A snapshot should have been 'uploaded'")];
215219
let artifact_expected = Snapshot::new(
216220
snapshot_digest.to_owned(),

mithril-aggregator/src/file_uploaders/dumb_uploader.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use async_trait::async_trait;
33
use mithril_common::StdResult;
44
use std::{path::Path, sync::RwLock};
55

6-
use super::{FileLocation, FileUploader};
6+
use crate::file_uploaders::{FileUploader, FileUri};
77

88
/// Dummy uploader for test purposes.
99
///
1010
/// It actually does NOT upload any file but remembers the last file it
1111
/// was asked to upload. This is intended to by used by integration tests.
1212
pub struct DumbUploader {
13-
last_uploaded: RwLock<Option<String>>,
13+
last_uploaded: RwLock<Option<FileUri>>,
1414
}
1515

1616
impl DumbUploader {
@@ -22,13 +22,13 @@ impl DumbUploader {
2222
}
2323

2424
/// Return the last upload that was triggered.
25-
pub fn get_last_upload(&self) -> StdResult<Option<String>> {
25+
pub fn get_last_upload(&self) -> StdResult<Option<FileUri>> {
2626
let value = self
2727
.last_uploaded
2828
.read()
2929
.map_err(|e| anyhow!("Error while saving filepath location: {e}"))?;
3030

31-
Ok(value.as_ref().map(|v| v.to_string()))
31+
Ok(value.as_ref().map(Clone::clone))
3232
}
3333
}
3434

@@ -41,13 +41,13 @@ impl Default for DumbUploader {
4141
#[async_trait]
4242
impl FileUploader for DumbUploader {
4343
/// Upload a file
44-
async fn upload(&self, filepath: &Path) -> StdResult<FileLocation> {
44+
async fn upload(&self, filepath: &Path) -> StdResult<FileUri> {
4545
let mut value = self
4646
.last_uploaded
4747
.write()
4848
.map_err(|e| anyhow!("Error while saving filepath location: {e}"))?;
4949

50-
let location = filepath.to_string_lossy().to_string();
50+
let location = FileUri(filepath.to_string_lossy().to_string());
5151
*value = Some(location.clone());
5252

5353
Ok(location)
@@ -69,9 +69,9 @@ mod tests {
6969
.upload(Path::new("/tmp/whatever"))
7070
.await
7171
.expect("uploading with a dumb uploader should not fail");
72-
assert_eq!(res, "/tmp/whatever".to_string());
72+
assert_eq!(res, FileUri("/tmp/whatever".to_string()));
7373
assert_eq!(
74-
Some("/tmp/whatever".to_string()),
74+
Some(FileUri("/tmp/whatever".to_string())),
7575
uploader
7676
.get_last_upload()
7777
.expect("getting dumb uploader last value after a fake download should not fail")

mithril-aggregator/src/file_uploaders/gcp_uploader.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ use cloud_storage::{
66
};
77
use slog::{info, Logger};
88
use std::{env, path::Path};
9-
use tokio_util::{codec::BytesCodec, codec::FramedRead};
9+
use tokio_util::codec::{BytesCodec, FramedRead};
1010

11-
use mithril_common::logging::LoggerExtensions;
12-
use mithril_common::StdResult;
11+
use mithril_common::{logging::LoggerExtensions, StdResult};
1312

14-
use crate::file_uploaders::FileLocation;
15-
use crate::FileUploader;
13+
use crate::{file_uploaders::FileUri, FileUploader};
1614

1715
/// GcpUploader represents a Google Cloud Platform file uploader interactor
1816
pub struct GcpUploader {
@@ -31,21 +29,21 @@ impl GcpUploader {
3129
}
3230
}
3331

34-
fn get_location(&self, filename: &str) -> String {
35-
if self.use_cdn_domain {
36-
format!("https://{}/{}", self.bucket, filename)
37-
} else {
38-
format!(
39-
"https://storage.googleapis.com/{}/{}",
40-
self.bucket, filename
41-
)
32+
fn get_location(&self, filename: &str) -> FileUri {
33+
let mut uri = vec![];
34+
if !self.use_cdn_domain {
35+
uri.push("storage.googleapis.com");
4236
}
37+
uri.push(&self.bucket);
38+
uri.push(filename);
39+
40+
FileUri(format!("https://{}", uri.join("/")))
4341
}
4442
}
4543

4644
#[async_trait]
4745
impl FileUploader for GcpUploader {
48-
async fn upload(&self, filepath: &Path) -> StdResult<FileLocation> {
46+
async fn upload(&self, filepath: &Path) -> StdResult<FileUri> {
4947
if env::var("GOOGLE_APPLICATION_CREDENTIALS_JSON").is_err() {
5048
return Err(anyhow!(
5149
"Missing GOOGLE_APPLICATION_CREDENTIALS_JSON environment variable".to_string()
@@ -118,7 +116,7 @@ mod tests {
118116

119117
let location = file_uploader.get_location(filename);
120118

121-
assert_eq!(expected_location, location);
119+
assert_eq!(FileUri(expected_location), location);
122120
}
123121

124122
#[tokio::test]
@@ -135,6 +133,6 @@ mod tests {
135133

136134
let location = file_uploader.get_location(filename);
137135

138-
assert_eq!(expected_location, location);
136+
assert_eq!(FileUri(expected_location), location);
139137
}
140138
}

mithril-aggregator/src/file_uploaders/interface.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ use async_trait::async_trait;
22
use mithril_common::StdResult;
33
use std::path::Path;
44

5-
pub type FileLocation = String;
5+
#[derive(Debug, PartialEq, Clone)]
6+
pub struct FileUri(pub String);
7+
8+
impl From<FileUri> for String {
9+
fn from(file_uri: FileUri) -> Self {
10+
file_uri.0
11+
}
12+
}
613

714
/// FileUploader represents a file uploader interactor
815
#[cfg_attr(test, mockall::automock)]
916
#[async_trait]
1017
pub trait FileUploader: Sync + Send {
1118
/// Upload a file
12-
async fn upload(&self, filepath: &Path) -> StdResult<FileLocation>;
19+
async fn upload(&self, filepath: &Path) -> StdResult<FileUri>;
1320
}

mithril-aggregator/src/file_uploaders/local_uploader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
66
use mithril_common::logging::LoggerExtensions;
77
use mithril_common::StdResult;
88

9-
use crate::file_uploaders::{FileLocation, FileUploader};
9+
use crate::file_uploaders::{FileUploader, FileUri};
1010
use crate::http_server;
1111
use crate::tools;
1212

@@ -36,7 +36,7 @@ impl LocalUploader {
3636

3737
#[async_trait]
3838
impl FileUploader for LocalUploader {
39-
async fn upload(&self, filepath: &Path) -> StdResult<FileLocation> {
39+
async fn upload(&self, filepath: &Path) -> StdResult<FileUri> {
4040
let archive_name = filepath.file_name().unwrap().to_str().unwrap();
4141
let target_path = &self.target_location.join(archive_name);
4242
tokio::fs::copy(filepath, target_path)
@@ -54,7 +54,7 @@ impl FileUploader for LocalUploader {
5454
);
5555

5656
debug!(self.logger, "File 'uploaded' to local storage"; "location" => &location);
57-
Ok(location)
57+
Ok(FileUri(location))
5858
}
5959
}
6060

@@ -65,7 +65,7 @@ mod tests {
6565
use std::path::{Path, PathBuf};
6666
use tempfile::tempdir;
6767

68-
use crate::file_uploaders::FileUploader;
68+
use crate::file_uploaders::{FileUploader, FileUri};
6969
use crate::http_server;
7070
use crate::test_tools::TestLogger;
7171

@@ -103,7 +103,7 @@ mod tests {
103103
.await
104104
.expect("local upload should not fail");
105105

106-
assert_eq!(expected_location, location);
106+
assert_eq!(FileUri(expected_location), location);
107107
}
108108

109109
#[tokio::test]

mithril-aggregator/src/file_uploaders/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ mod local_uploader;
55

66
pub use dumb_uploader::*;
77
pub use gcp_uploader::GcpUploader;
8-
pub use interface::FileLocation;
98
pub use interface::FileUploader;
9+
pub use interface::FileUri;
1010
pub use local_uploader::LocalUploader;
1111

1212
#[cfg(test)]

0 commit comments

Comments
 (0)