junobuild_storage/well_known/
utils.rs

1use crate::constants::{WELL_KNOWN_CUSTOM_DOMAINS, WELL_KNOWN_II_ALTERNATIVE_ORIGINS};
2use crate::types::store::{Asset, AssetKey};
3use crate::utils::{create_asset_with_content, map_content_type_headers};
4use ic_cdk::api::time;
5use junobuild_collections::constants::assets::COLLECTION_ASSET_KEY;
6use junobuild_shared::ic::api::id;
7use junobuild_shared::types::domain::CustomDomain;
8use junobuild_shared::types::state::Timestamp;
9use junobuild_shared::version::next_version;
10
11pub fn map_custom_domains_asset(custom_domains: &str, existing_asset: Option<Asset>) -> Asset {
12    let key = AssetKey {
13        name: "custom-domains".to_string(),
14        full_path: WELL_KNOWN_CUSTOM_DOMAINS.to_string(),
15        token: None,
16        collection: COLLECTION_ASSET_KEY.to_string(),
17        owner: id(),
18        description: None,
19    };
20
21    let headers = map_content_type_headers("application/octet-stream");
22
23    create_asset_with_content(custom_domains, &headers, existing_asset, key)
24}
25
26pub fn map_alternative_origins_asset(
27    alternative_origins: &str,
28    existing_asset: Option<Asset>,
29) -> Asset {
30    let key = AssetKey {
31        name: "ii-alternative-origins".to_string(),
32        full_path: WELL_KNOWN_II_ALTERNATIVE_ORIGINS.to_string(),
33        token: None,
34        collection: COLLECTION_ASSET_KEY.to_string(),
35        owner: id(),
36        description: None,
37    };
38
39    let headers = map_content_type_headers("application/json");
40
41    create_asset_with_content(alternative_origins, &headers, existing_asset, key)
42}
43
44pub fn build_custom_domain(domain: Option<CustomDomain>, bn_id: &Option<String>) -> CustomDomain {
45    let now = time();
46
47    let created_at: Timestamp = match domain.clone() {
48        None => now,
49        Some(domain) => domain.created_at,
50    };
51
52    let version = next_version(&domain);
53
54    let updated_at: Timestamp = now;
55
56    CustomDomain {
57        bn_id: bn_id.to_owned(),
58        created_at,
59        updated_at,
60        version: Some(version),
61    }
62}