junobuild_storage/
impls.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::http::types::HeaderField;
use crate::types::config::{
    StorageConfig, StorageConfigHeaders, StorageConfigIFrame, StorageConfigRawAccess,
    StorageConfigRedirects, StorageConfigRewrites,
};
use crate::types::interface::{AssetEncodingNoContent, AssetNoContent};
use crate::types::state::StorageHeapState;
use crate::types::store::{Asset, AssetEncoding, AssetKey, Batch, BatchExpiry};
use ic_cdk::api::time;
use ic_stable_structures::storable::Bound;
use ic_stable_structures::Storable;
use junobuild_collections::constants::DEFAULT_ASSETS_COLLECTIONS;
use junobuild_collections::types::interface::SetRule;
use junobuild_collections::types::rules::{Memory, Rule, Rules};
use junobuild_shared::serializers::{deserialize_from_bytes, serialize_to_bytes};
use junobuild_shared::types::core::{Blob, Hash, Hashable};
use junobuild_shared::types::state::Timestamp;
use junobuild_shared::types::state::Timestamped;
use sha2::{Digest, Sha256};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::HashMap;

impl Default for StorageHeapState {
    fn default() -> Self {
        Self::new_with_storage_collections(Vec::from(DEFAULT_ASSETS_COLLECTIONS))
    }
}

impl StorageHeapState {
    pub fn new_with_storage_collections(storage_collections: Vec<(&str, SetRule)>) -> Self {
        let now = time();

        StorageHeapState {
            assets: HashMap::new(),
            rules: storage_collections
                .into_iter()
                .map(|(collection, rule)| {
                    (
                        collection.to_owned(),
                        Rule {
                            read: rule.read,
                            write: rule.write,
                            memory: Some(rule.memory.unwrap_or(Memory::Heap)),
                            mutable_permissions: Some(rule.mutable_permissions.unwrap_or(false)),
                            max_size: rule.max_size,
                            max_capacity: rule.max_capacity,
                            created_at: now,
                            updated_at: now,
                            version: rule.version,
                            rate_config: rule.rate_config,
                        },
                    )
                })
                .collect::<Rules>(),
            config: StorageConfig {
                headers: StorageConfigHeaders::default(),
                rewrites: StorageConfigRewrites::default(),
                redirects: Some(StorageConfigRedirects::default()),
                iframe: None,
                raw_access: None,
                max_memory_size: None,
            },
            custom_domains: HashMap::new(),
        }
    }
}

impl From<&Vec<Blob>> for AssetEncoding {
    fn from(content_chunks: &Vec<Blob>) -> Self {
        let mut total_length: u128 = 0;
        let mut hasher = Sha256::new();

        // Calculate sha256 and total length
        for chunk in content_chunks.iter() {
            total_length += u128::try_from(chunk.len()).unwrap();

            hasher.update(chunk);
        }

        let sha256 = hasher.finalize().into();

        AssetEncoding {
            modified: time(),
            content_chunks: content_chunks.clone(),
            total_length,
            sha256,
        }
    }
}

impl StorageConfig {
    pub fn unwrap_redirects(&self) -> StorageConfigRedirects {
        self.redirects.clone().unwrap_or_default()
    }

    pub fn unwrap_iframe(&self) -> StorageConfigIFrame {
        self.iframe.clone().unwrap_or(StorageConfigIFrame::Deny)
    }

    pub fn unwrap_raw_access(&self) -> StorageConfigRawAccess {
        self.raw_access
            .clone()
            .unwrap_or(StorageConfigRawAccess::Deny)
    }
}

impl Timestamped for AssetNoContent {
    fn created_at(&self) -> Timestamp {
        self.created_at
    }

    fn updated_at(&self) -> Timestamp {
        self.updated_at
    }

    fn cmp_updated_at(&self, other: &Self) -> Ordering {
        self.updated_at.cmp(&other.updated_at)
    }

    fn cmp_created_at(&self, other: &Self) -> Ordering {
        self.created_at.cmp(&other.created_at)
    }
}

impl From<&Asset> for AssetNoContent {
    fn from(asset: &Asset) -> Self {
        AssetNoContent {
            key: asset.key.clone(),
            headers: asset.headers.clone(),
            encodings: asset
                .encodings
                .clone()
                .into_iter()
                .map(|(key, encoding)| {
                    (
                        key,
                        AssetEncodingNoContent {
                            modified: encoding.modified,
                            total_length: encoding.total_length,
                            sha256: encoding.sha256,
                        },
                    )
                })
                .collect(),
            created_at: asset.created_at,
            updated_at: asset.updated_at,
            version: asset.version,
        }
    }
}

impl Storable for Asset {
    fn to_bytes(&self) -> Cow<[u8]> {
        serialize_to_bytes(self)
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        deserialize_from_bytes(bytes)
    }

    const BOUND: Bound = Bound::Unbounded;
}

impl Timestamped for Asset {
    fn created_at(&self) -> Timestamp {
        self.created_at
    }

    fn updated_at(&self) -> Timestamp {
        self.updated_at
    }

    fn cmp_updated_at(&self, other: &Self) -> Ordering {
        self.updated_at.cmp(&other.updated_at)
    }

    fn cmp_created_at(&self, other: &Self) -> Ordering {
        self.created_at.cmp(&other.created_at)
    }
}

impl BatchExpiry for Batch {
    fn expires_at(&self) -> Timestamp {
        self.expires_at
    }
}

impl Hashable for AssetKey {
    fn hash(&self) -> Hash {
        let mut hasher = Sha256::new();
        hasher.update(self.name.as_bytes());
        hasher.update(self.full_path.as_bytes());
        if let Some(token) = &self.token {
            hasher.update(token.as_bytes());
        }
        hasher.update(self.collection.as_bytes());
        hasher.update(serialize_to_bytes(&self.owner));
        if let Some(description) = &self.description {
            hasher.update(description.as_bytes());
        }
        hasher.finalize().into()
    }
}

impl Hashable for Asset {
    fn hash(&self) -> Hash {
        let mut hasher = Sha256::new();
        hasher.update(self.key.hash());
        for HeaderField(ref key, ref value) in &self.headers {
            hasher.update(key.as_bytes());
            hasher.update(value.as_bytes());
        }
        hasher.update(self.created_at.to_le_bytes());
        hasher.update(self.updated_at.to_le_bytes());
        if let Some(version) = self.version {
            hasher.update(version.to_le_bytes());
        }
        hasher.finalize().into()
    }
}

impl Hashable for AssetEncoding {
    fn hash(&self) -> Hash {
        let mut hasher = Sha256::new();
        hasher.update(self.modified.to_le_bytes());
        hasher.update(self.total_length.to_le_bytes());
        hasher.update(self.sha256);
        hasher.finalize().into()
    }
}