mithril_aggregator/file_uploaders/
local_snapshot_uploader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use anyhow::Context;
use async_trait::async_trait;
use reqwest::Url;
use slog::{debug, Logger};
use std::path::{Path, PathBuf};

use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;

use crate::file_uploaders::{url_sanitizer::sanitize_url_path, FileUploader, FileUri};
use crate::tools;

// It's only used by the legacy snapshot that uploads the entire Cardano database.
/// LocalSnapshotUploader is a file uploader working using local files
pub struct LocalSnapshotUploader {
    /// File server URL prefix
    server_url_prefix: Url,

    /// Target folder where to store files archive
    target_location: PathBuf,

    logger: Logger,
}

impl LocalSnapshotUploader {
    /// LocalSnapshotUploader factory
    pub(crate) fn new(
        server_url_prefix: Url,
        target_location: &Path,
        logger: Logger,
    ) -> StdResult<Self> {
        let logger = logger.new_with_component_name::<Self>();
        debug!(logger, "New LocalSnapshotUploader created"; "server_url_prefix" => &server_url_prefix.as_str());
        let server_url_prefix = sanitize_url_path(&server_url_prefix)?;

        Ok(Self {
            server_url_prefix,
            target_location: target_location.to_path_buf(),
            logger,
        })
    }
}

#[async_trait]
impl FileUploader for LocalSnapshotUploader {
    async fn upload(&self, filepath: &Path) -> StdResult<FileUri> {
        let archive_name = filepath.file_name().unwrap().to_str().unwrap();
        let target_path = &self.target_location.join(archive_name);
        tokio::fs::copy(filepath, target_path)
            .await
            .with_context(|| "File copy failure")?;

        let digest = tools::extract_digest_from_path(Path::new(archive_name))?;
        let location = &self
            .server_url_prefix
            .join("artifact/snapshot/")?
            .join(&format!("{digest}/"))?
            .join("download")?;
        let location = location.as_str().to_string();

        debug!(self.logger, "File 'uploaded' to local storage"; "location" => &location);
        Ok(FileUri(location))
    }
}

#[cfg(test)]
mod tests {
    use std::fs::File;
    use std::io::Write;
    use std::path::{Path, PathBuf};
    use tempfile::tempdir;

    use crate::file_uploaders::{FileUploader, FileUri};
    use crate::test_tools::TestLogger;

    use super::*;

    fn create_fake_archive(dir: &Path, digest: &str) -> PathBuf {
        let file_path = dir.join(format!("test.{digest}.tar.gz"));
        let mut file = File::create(&file_path).unwrap();
        writeln!(
            file,
            "I swear, this is an archive, not a temporary test file."
        )
        .unwrap();

        file_path
    }

    #[tokio::test]
    async fn should_extract_digest_to_deduce_location() {
        let source_dir = tempdir().unwrap();
        let target_dir = tempdir().unwrap();
        let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7";
        let archive = create_fake_archive(source_dir.path(), digest);
        let expected_location = format!(
            "http://test.com:8080/base-root/artifact/snapshot/{}/download",
            &digest
        );

        let url_prefix = Url::parse("http://test.com:8080/base-root").unwrap();
        let uploader =
            LocalSnapshotUploader::new(url_prefix, target_dir.path(), TestLogger::stdout())
                .unwrap();
        let location = uploader
            .upload(&archive)
            .await
            .expect("local upload should not fail");

        assert_eq!(FileUri(expected_location), location);
    }

    #[tokio::test]
    async fn should_copy_file_to_target_location() {
        let source_dir = tempdir().unwrap();
        let target_dir = tempdir().unwrap();
        let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7";
        let archive = create_fake_archive(source_dir.path(), digest);
        let uploader = LocalSnapshotUploader::new(
            Url::parse("http://test.com:8080/base-root/").unwrap(),
            target_dir.path(),
            TestLogger::stdout(),
        )
        .unwrap();
        uploader.upload(&archive).await.unwrap();

        assert!(target_dir
            .path()
            .join(archive.file_name().unwrap())
            .exists());
    }

    #[tokio::test]
    async fn should_error_if_path_is_a_directory() {
        let source_dir = tempdir().unwrap();
        let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7";
        create_fake_archive(source_dir.path(), digest);
        let target_dir = tempdir().unwrap();
        let uploader = LocalSnapshotUploader::new(
            Url::parse("http://test.com:8080/base-root/").unwrap(),
            target_dir.path(),
            TestLogger::stdout(),
        )
        .unwrap();
        uploader
            .upload(source_dir.path())
            .await
            .expect_err("Uploading a directory should fail");
    }
}