junobuild_storage/
types.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
pub mod state {
    use crate::types::config::StorageConfig;
    use crate::types::store::Asset;
    use candid::{CandidType, Deserialize};
    use junobuild_collections::types::rules::Rules;
    use junobuild_shared::types::core::Key;
    use junobuild_shared::types::domain::CustomDomains;
    use serde::Serialize;
    use std::collections::HashMap;

    /// Represents the relative path of an asset in the storage.
    ///
    /// This type, `FullPath`, is an alias for `Key`, indicating the relative path of an asset within the storage system.
    ///
    /// `FullPath` is commonly used to identify the location of assets within a storage system.
    pub type FullPath = Key;

    pub type AssetsHeap = HashMap<FullPath, Asset>;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct StorageHeapState {
        pub assets: AssetsHeap,
        pub rules: Rules,
        pub config: StorageConfig,
        pub custom_domains: CustomDomains,
    }
}

pub mod runtime_state {
    use crate::certification::types::certified::CertifiedAssetHashes;
    use crate::types::store::{Batch, Chunk};
    use junobuild_shared::rate::types::RateTokenStore;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    pub type Batches = HashMap<BatchId, Batch>;
    pub type Chunks = HashMap<ChunkId, Chunk>;

    pub type BatchId = u128;
    pub type ChunkId = u128;

    #[derive(Default, Serialize, Deserialize)]
    pub struct State {
        // Unstable state: State that resides only on the heap, that’s lost after an upgrade.
        #[serde(skip, default)]
        pub runtime: RuntimeState,
    }

    #[derive(Default, Clone)]
    pub struct RuntimeState {
        pub storage: StorageRuntimeState,
    }

    #[derive(Default, Clone)]
    pub struct StorageRuntimeState {
        pub batches: Batches,
        pub chunks: Chunks,
        pub asset_hashes: CertifiedAssetHashes,
        pub rate_tokens: RateTokenStore,
    }
}

pub mod store {
    use crate::http::types::HeaderField;
    use crate::types::interface::CommitBatch;
    use crate::types::runtime_state::BatchId;
    use crate::types::state::FullPath;
    use candid::CandidType;
    use ic_certification::Hash;
    use junobuild_collections::types::core::CollectionKey;
    use junobuild_shared::types::core::Blob;
    use junobuild_shared::types::state::{Timestamp, UserId, Version};
    use serde::{Deserialize, Serialize};
    use std::clone::Clone;
    use std::collections::HashMap;

    #[derive(CandidType, Deserialize, Clone)]
    pub struct Chunk {
        pub batch_id: BatchId,
        pub order_id: u128,
        pub content: Blob,
    }

    // When stable memory is used, chunks are saved within a StableBTreeMap and their keys - StableEncodingChunkKey - are saved for reference as serialized values
    pub type BlobOrKey = Blob;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct AssetEncoding {
        pub modified: Timestamp,
        pub content_chunks: Vec<BlobOrKey>,
        pub total_length: u128,
        pub sha256: Hash,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct AssetKey {
        // myimage.jpg
        pub name: String,
        // /images/myimage.jpg
        pub full_path: FullPath,
        // ?token=1223-3345-5564-3333
        pub token: Option<String>,
        // Assets are prefixed with full_path because these are unique. Collection is there for read (list) and write but all assets are available through http_request (that's why we use the token).
        pub collection: CollectionKey,
        // For security check purpose
        pub owner: UserId,
        // A description field which can be useful for search purpose
        pub description: Option<String>,
    }

    pub type EncodingType = String;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct Asset {
        pub key: AssetKey,
        pub headers: Vec<HeaderField>,
        pub encodings: HashMap<EncodingType, AssetEncoding>,
        pub created_at: Timestamp,
        pub updated_at: Timestamp,
        pub version: Option<Version>,
    }

    pub trait BatchExpiry {
        fn expires_at(&self) -> Timestamp;
    }

    pub type ReferenceId = u128;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct Batch {
        pub key: AssetKey,
        pub reference_id: Option<ReferenceId>,
        pub expires_at: Timestamp,
        pub encoding_type: Option<EncodingType>,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct AssetAssertUpload {
        pub current: Option<Asset>,
        pub batch: Batch,
        pub commit_batch: CommitBatch,
    }
}

pub mod interface {
    use candid::{CandidType, Deserialize};
    use ic_certification::Hash;
    use junobuild_collections::types::core::CollectionKey;
    use junobuild_shared::types::core::Blob;
    use junobuild_shared::types::state::{Timestamp, Version};
    use serde::Serialize;

    use crate::http::types::HeaderField;
    use crate::types::runtime_state::{BatchId, ChunkId};
    use crate::types::state::FullPath;
    use crate::types::store::{AssetKey, EncodingType};

    #[derive(CandidType, Deserialize)]
    pub struct InitAssetKey {
        pub name: String,
        pub full_path: FullPath,
        pub token: Option<String>,
        pub collection: CollectionKey,
        pub encoding_type: Option<EncodingType>,
        pub description: Option<String>,
    }

    #[derive(CandidType)]
    pub struct InitUploadResult {
        pub batch_id: BatchId,
    }

    #[derive(CandidType, Deserialize)]
    pub struct UploadChunk {
        pub batch_id: BatchId,
        pub content: Blob,
        pub order_id: Option<u128>,
    }

    #[derive(CandidType)]
    pub struct UploadChunkResult {
        pub chunk_id: ChunkId,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub struct CommitBatch {
        pub batch_id: BatchId,
        pub headers: Vec<HeaderField>,
        pub chunk_ids: Vec<ChunkId>,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct AssetNoContent {
        pub key: AssetKey,
        pub headers: Vec<HeaderField>,
        pub encodings: Vec<(EncodingType, AssetEncodingNoContent)>,
        pub created_at: Timestamp,
        pub updated_at: Timestamp,
        pub version: Option<Version>,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct AssetEncodingNoContent {
        pub modified: Timestamp,
        pub total_length: u128,
        pub sha256: Hash,
    }
}

pub mod config {
    use crate::http::types::{HeaderField, StatusCode};
    use candid::CandidType;
    use junobuild_shared::types::config::ConfigMaxMemorySize;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    pub type StorageConfigHeaders = HashMap<String, Vec<HeaderField>>;
    pub type StorageConfigRewrites = HashMap<String, String>;
    pub type StorageConfigRedirects = HashMap<String, StorageConfigRedirect>;

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum StorageConfigIFrame {
        Deny,
        SameOrigin,
        AllowAny,
    }

    #[derive(CandidType, Serialize, Deserialize, Clone)]
    pub enum StorageConfigRawAccess {
        Deny,
        Allow,
    }

    pub type StorageConfigMaxMemorySize = ConfigMaxMemorySize;

    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
    pub struct StorageConfig {
        pub headers: StorageConfigHeaders,
        pub rewrites: StorageConfigRewrites,
        pub redirects: Option<StorageConfigRedirects>,
        pub iframe: Option<StorageConfigIFrame>,
        pub raw_access: Option<StorageConfigRawAccess>,
        pub max_memory_size: Option<StorageConfigMaxMemorySize>,
    }

    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
    pub struct StorageConfigRedirect {
        pub location: String,
        pub status_code: StatusCode,
    }
}

pub mod http_request {
    use crate::http::types::StatusCode;
    use crate::types::config::{StorageConfigIFrame, StorageConfigRedirect};
    use crate::types::store::Asset;
    use candid::{CandidType, Deserialize};
    use junobuild_collections::types::rules::Memory;

    #[derive(CandidType, Deserialize, Clone)]
    pub struct MapUrl {
        pub path: String,
        pub token: Option<String>,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub enum Routing {
        Default(RoutingDefault),
        Rewrite(RoutingRewrite),
        Redirect(RoutingRedirect),
        RedirectRaw(RoutingRedirectRaw),
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct RoutingDefault {
        pub url: String,
        pub asset: Option<(Asset, Memory)>,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct RoutingRewrite {
        pub url: String,
        pub asset: Option<(Asset, Memory)>,
        pub source: String,
        pub status_code: StatusCode,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct RoutingRedirect {
        pub url: String,
        pub redirect: StorageConfigRedirect,
        pub iframe: StorageConfigIFrame,
    }

    #[derive(CandidType, Deserialize, Clone)]
    pub struct RoutingRedirectRaw {
        pub redirect_url: String,
        pub iframe: StorageConfigIFrame,
    }
}