junobuild-storage 0.7.0

Web and storage features of Juno.
Documentation
use crate::constants::{WELL_KNOWN_CUSTOM_DOMAINS, WELL_KNOWN_II_ALTERNATIVE_ORIGINS};
use crate::types::store::{Asset, AssetKey};
use crate::utils::{create_asset_with_content, map_content_type_headers};
use crate::well_known::types::WellKnownAsset;
use ic_cdk::api::time;
use junobuild_collections::constants::assets::COLLECTION_ASSET_KEY;
use junobuild_shared::data::version::next_version;
use junobuild_shared::ic::api::id;
use junobuild_shared::types::domain::CustomDomain;
use junobuild_shared::types::state::Timestamp;

pub fn map_custom_domains_asset(
    custom_domains: &str,
    existing_asset: Option<Asset>,
) -> WellKnownAsset {
    let key = AssetKey {
        name: "custom-domains".to_string(),
        full_path: WELL_KNOWN_CUSTOM_DOMAINS.to_string(),
        token: None,
        collection: COLLECTION_ASSET_KEY.to_string(),
        owner: id(),
        description: None,
    };

    let headers = map_content_type_headers("application/octet-stream");

    create_asset_with_content(custom_domains, &headers, existing_asset, key)
}

pub fn map_alternative_origins_asset(
    alternative_origins: &str,
    existing_asset: Option<Asset>,
) -> WellKnownAsset {
    let key = AssetKey {
        name: "ii-alternative-origins".to_string(),
        full_path: WELL_KNOWN_II_ALTERNATIVE_ORIGINS.to_string(),
        token: None,
        collection: COLLECTION_ASSET_KEY.to_string(),
        owner: id(),
        description: None,
    };

    let headers = map_content_type_headers("application/json");

    create_asset_with_content(alternative_origins, &headers, existing_asset, key)
}

pub fn build_custom_domain(domain: Option<CustomDomain>, bn_id: &Option<String>) -> CustomDomain {
    let now = time();

    let created_at: Timestamp = match domain.clone() {
        None => now,
        Some(domain) => domain.created_at,
    };

    let version = next_version(&domain);

    let updated_at: Timestamp = now;

    CustomDomain {
        bn_id: bn_id.to_owned(),
        created_at,
        updated_at,
        version: Some(version),
    }
}