junobuild_storage/well_known/
update.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
use crate::constants::{WELL_KNOWN_CUSTOM_DOMAINS, WELL_KNOWN_II_ALTERNATIVE_ORIGINS};
use crate::runtime::{
    delete_certified_asset, update_certified_asset as update_runtime_certified_asset,
};
use crate::strategies::StorageStateStrategy;
use crate::types::store::Asset;
use crate::well_known::utils::{map_alternative_origins_asset, map_custom_domains_asset};
use junobuild_collections::constants::DEFAULT_ASSETS_COLLECTIONS;
use junobuild_shared::types::core::DomainName;

pub fn update_alternative_origins_asset(
    alternative_origins: &str,
    storage_state: &impl StorageStateStrategy,
) -> Result<(), String> {
    let full_path = WELL_KNOWN_II_ALTERNATIVE_ORIGINS.to_string();

    update_asset(
        &full_path,
        alternative_origins,
        storage_state,
        &map_alternative_origins_asset,
    )
}

pub fn update_custom_domains_asset(
    storage_state: &impl StorageStateStrategy,
) -> Result<(), String> {
    let full_path = WELL_KNOWN_CUSTOM_DOMAINS.to_string();

    let custom_domains = storage_state.get_domains();

    if custom_domains.is_empty() {
        return delete_asset(&full_path, storage_state);
    }

    let concat_custom_domains = custom_domains
        .into_keys()
        .collect::<Vec<DomainName>>()
        .join("\n");

    update_asset(
        &full_path,
        &concat_custom_domains,
        storage_state,
        &map_custom_domains_asset,
    )
}

pub fn delete_alternative_origins_asset(
    storage_state: &impl StorageStateStrategy,
) -> Result<(), String> {
    let full_path = WELL_KNOWN_II_ALTERNATIVE_ORIGINS.to_string();

    delete_asset(&full_path, storage_state)
}

fn update_asset(
    full_path: &String,
    content: &str,
    storage_state: &impl StorageStateStrategy,
    f: &dyn Fn(&str, Option<Asset>) -> Asset,
) -> Result<(), String> {
    let collection = DEFAULT_ASSETS_COLLECTIONS[0].0.to_string();

    // #app collection rule
    let rule = storage_state.get_rule(&collection)?;

    let existing_asset = storage_state.get_asset(&collection, full_path, &rule);

    let asset = f(content, existing_asset);

    storage_state.insert_asset(&collection, full_path, &asset, &rule);

    let config = storage_state.get_config();

    update_runtime_certified_asset(&asset, &config);

    Ok(())
}

fn delete_asset(
    full_path: &String,
    storage_state: &impl StorageStateStrategy,
) -> Result<(), String> {
    let collection = DEFAULT_ASSETS_COLLECTIONS[0].0.to_string();

    // #app collection rule
    let rule = storage_state.get_rule(&collection)?;

    let asset = storage_state.delete_asset(&collection, full_path, &rule);

    if let Some(asset) = asset {
        delete_certified_asset(&asset);
    }

    Ok(())
}