Skip to main content

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