junobuild_satellite/storage/
store.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::controllers::store::get_controllers;
use crate::hooks::invoke_assert_delete_asset;
use crate::memory::STATE;
use candid::Principal;
use junobuild_collections::assert_stores::{assert_permission, public_permission};
use junobuild_collections::msg::msg_storage_collection_not_empty;
use junobuild_collections::types::core::CollectionKey;
use junobuild_collections::types::rules::{Memory, Rule};
use junobuild_shared::controllers::is_controller;
use junobuild_shared::list::list_values;
use junobuild_shared::types::state::Controllers;

use crate::rules::assert_stores::is_known_user;
use crate::storage::certified_assets::runtime::init_certified_assets as init_runtime_certified_assets;
use crate::storage::state::{
    count_assets_stable, delete_asset as delete_state_asset, delete_domain as delete_state_domain,
    get_asset as get_state_asset, get_assets_stable, get_config as get_state_config, get_config,
    get_content_chunks as get_state_content_chunks, get_domain as get_state_domain,
    get_domains as get_state_domains, get_public_asset as get_state_public_asset,
    get_rule as get_state_rule, insert_config as insert_state_config,
    insert_domain as insert_state_domain,
};
use crate::storage::strategy_impls::{StorageAssertions, StorageState, StorageUpload};
use crate::types::store::StoreContext;
use junobuild_shared::types::core::{Blob, DomainName};
use junobuild_shared::types::domain::CustomDomains;
use junobuild_shared::types::list::{ListParams, ListResults};
use junobuild_storage::constants::{ROOT_404_HTML, ROOT_INDEX_HTML};
use junobuild_storage::heap_utils::{
    collect_assets_heap, collect_delete_assets_heap, count_assets_heap,
};
use junobuild_storage::msg::{ERROR_ASSET_NOT_FOUND, UPLOAD_NOT_ALLOWED};
use junobuild_storage::runtime::{
    delete_certified_asset as delete_runtime_certified_asset,
    increment_and_assert_rate as increment_and_assert_rate_runtime,
    update_certified_asset as update_runtime_certified_asset,
};
use junobuild_storage::store::{commit_batch as commit_batch_storage, create_batch, create_chunk};
use junobuild_storage::types::config::StorageConfig;
use junobuild_storage::types::interface::{AssetNoContent, CommitBatch, InitAssetKey, UploadChunk};
use junobuild_storage::types::runtime_state::{BatchId, ChunkId};
use junobuild_storage::types::state::FullPath;
use junobuild_storage::types::store::{Asset, AssetEncoding};
use junobuild_storage::utils::{
    filter_collection_values, filter_values, get_token_protected_asset, map_asset_no_content,
    should_include_asset_for_deletion,
};
use junobuild_storage::well_known::update::update_custom_domains_asset;
use junobuild_storage::well_known::utils::build_custom_domain;

///
/// Getter, list and delete
///

/// Get content chunks of an asset.
///
/// This function retrieves content chunks of an asset based on the specified parameters.
/// It returns an `Option<Blob>` representing the content chunks, or `None` if no chunks are found.
///
/// # Parameters
/// - `encoding`: A reference to the `AssetEncoding` indicating the encoding of the asset.
/// - `chunk_index`: The index of the content chunk to retrieve.
/// - `memory`: A reference to the `Memory` type (Heap or Stable).
///
/// # Returns
/// - `Some(Blob)`: Content chunks of the asset.
/// - `None`: No content chunks found.
///
/// This function allows you to retrieve content chunks of an asset stored in a Juno collection's store.
pub fn get_content_chunks_store(
    encoding: &AssetEncoding,
    chunk_index: usize,
    memory: &Memory,
) -> Option<Blob> {
    get_state_content_chunks(encoding, chunk_index, memory)
}

/// Delete an asset from a collection's store.
///
/// This function deletes an asset from a collection's store based on the specified parameters.
/// It returns a `Result<Option<Asset>, String>` where `Ok(Some(Asset))` indicates successful deletion
/// of the asset, `Ok(None)` represents no asset found for the specified path, or an error message
/// as `Err(String)` if the deletion encounters issues.
///
/// # Parameters
/// - `caller`: The `Principal` representing the caller initiating the deletion.
/// - `collection`: A reference to the `CollectionKey` representing the collection from which to delete the asset.
/// - `full_path`: A `FullPath` identifying the asset to be deleted.
///
/// # Returns
/// - `Ok(Some(Asset))`: Indicates successful deletion of the asset.
/// - `Ok(None)`: Indicates no asset found for the specified path.
/// - `Err(String)`: An error message if the deletion operation fails.
///
/// This function allows you to securely delete assets from a Juno collection's store.
pub fn delete_asset_store(
    caller: Principal,
    collection: &CollectionKey,
    full_path: FullPath,
) -> Result<Option<Asset>, String> {
    let controllers: Controllers = get_controllers();
    let config = get_config_store();

    let context = StoreContext {
        caller,
        controllers: &controllers,
        collection,
    };

    secure_delete_asset_impl(&context, full_path, &config)
}

pub fn delete_assets_store(collection: &CollectionKey) -> Result<(), String> {
    let rule = get_state_rule(collection)?;

    let full_paths = match rule.mem() {
        Memory::Heap => STATE.with(|state| {
            let state_ref = state.borrow();
            collect_delete_assets_heap(collection, &state_ref.heap.storage.assets)
        }),
        Memory::Stable => STATE.with(|state| {
            let stable = get_assets_stable(collection, &state.borrow().stable.assets);
            stable
                .iter()
                .filter(|(_, asset)| {
                    should_include_asset_for_deletion(collection, &asset.key.full_path)
                })
                .map(|(_, asset)| asset.key.full_path.clone())
                .collect()
        }),
    };

    delete_assets_impl(&full_paths, collection, &rule)
}

/// List assets in a collection.
///
/// This function retrieves a list of assets from a collection's store based on the specified parameters.
/// It returns a `Result<ListResults<AssetNoContent>, String>` where `Ok(ListResults)` contains the retrieved assets,
/// or an error message as `Err(String)` if the operation encounters issues.
///
/// # Parameters
/// - `caller`: The `Principal` representing the caller initiating the operation. If used in serverless functions, you can use `ic_cdk::id()` to pass an administrator controller.
/// - `collection`: A reference to the `CollectionKey` representing the collection from which to list the assets.
/// - `filters`: A reference to `ListParams` containing the filter criteria for listing the assets.
///
/// # Returns
/// - `Ok(ListResults<AssetNoContent>)`: Contains the list of retrieved assets, without their content, matching the filter criteria.
/// - `Err(String)`: An error message if the operation fails.
///
/// This function lists assets in a Juno collection's store, applying the specified filter criteria to retrieve the assets.
/// The returned list includes the assets without their content (`AssetNoContent`), which is useful for operations where only
/// metadata or references are needed.
pub fn list_assets_store(
    caller: Principal,
    collection: &CollectionKey,
    filters: &ListParams,
) -> Result<ListResults<AssetNoContent>, String> {
    let controllers: Controllers = get_controllers();

    let context = StoreContext {
        caller,
        controllers: &controllers,
        collection,
    };

    secure_list_assets_impl(&context, filters)
}

/// Count assets in a collection.
///
/// This function retrieves the count of assets from a collection's store based on the specified parameters.
/// It returns a `Result<usize, String>` where `Ok(usize)` contains the count of assets matching the filter criteria,
/// or an error message as `Err(String)` if the operation encounters issues.
///
/// # Parameters
/// - `caller`: The `Principal` representing the caller initiating the operation. If used in serverless functions, you can use `ic_cdk::id()` to pass an administrator controller.
/// - `collection`: A reference to the `CollectionKey` representing the collection from which to count the assets.
/// - `filters`: A reference to `ListParams` containing the filter criteria for counting the assets.
///
/// # Returns
/// - `Ok(usize)`: Contains the count of assets matching the filter criteria.
/// - `Err(String)`: An error message if the operation fails.
///
/// This function counts assets in a Juno collection's store by listing them and then determining the length of the result set.
///
/// # Note
/// This implementation can be improved, as it currently relies on `list_assets_store` underneath, meaning that all assets matching the filter criteria are still read from the store. This might lead to unnecessary overhead, especially for large collections. Optimizing this function to count assets directly without retrieving them could enhance performance.
pub fn count_assets_store(
    caller: Principal,
    collection: &CollectionKey,
    filters: &ListParams,
) -> Result<usize, String> {
    let results = list_assets_store(caller, collection, filters)?;
    Ok(results.items_length)
}

/// Get an asset from a collection's store.
///
/// This function retrieves an asset from a collection's store based on the specified parameters.
/// It returns a `Result<Option<Asset>, String>` where `Ok(Some(Asset))` indicates a successfully retrieved
/// asset, `Ok(None)` represents no asset found for the specified path, or an error message as `Err(String)`
/// if retrieval encounters issues.
///
/// # Parameters
/// - `caller`: The `Principal` representing the caller requesting the asset.
/// - `collection`: A reference to the `CollectionKey` representing the collection from which to retrieve the asset.
/// - `full_path`: A `FullPath` identifying the asset to be retrieved.
///
/// # Returns
/// - `Ok(Some(Asset))`: Indicates successful retrieval of the asset.
/// - `Ok(None)`: Indicates no asset found for the specified path.
/// - `Err(String)`: An error message if the retrieval operation fails.
///
/// This function allows you to securely retrieve an asset from a Juno collection's store.
pub fn get_asset_store(
    caller: Principal,
    collection: &CollectionKey,
    full_path: FullPath,
) -> Result<Option<Asset>, String> {
    let controllers: Controllers = get_controllers();

    let context = StoreContext {
        caller,
        controllers: &controllers,
        collection,
    };

    secure_get_asset_impl(&context, full_path)
}

fn secure_get_asset_impl(
    context: &StoreContext,
    full_path: FullPath,
) -> Result<Option<Asset>, String> {
    let rule = get_state_rule(context.collection)?;

    get_asset_impl(context, full_path, &rule)
}

fn get_asset_impl(
    context: &StoreContext,
    full_path: FullPath,
    rule: &Rule,
) -> Result<Option<Asset>, String> {
    let asset = get_state_asset(context.collection, &full_path, rule);

    match asset {
        None => Ok(None),
        Some(asset) => {
            if !assert_permission(
                &rule.read,
                asset.key.owner,
                context.caller,
                context.controllers,
            ) {
                return Ok(None);
            }

            Ok(Some(asset))
        }
    }
}

pub fn get_public_asset_store(
    full_path: FullPath,
    token: Option<String>,
) -> Option<(Asset, Memory)> {
    let (asset, memory) = get_state_public_asset(&full_path);

    match asset {
        None => None,
        Some(asset) => match &asset.key.token {
            None => Some((asset.clone(), memory)),
            Some(asset_token) => {
                let protected_asset = get_token_protected_asset(&asset, asset_token, token);
                protected_asset.map(|protected_asset| (protected_asset, memory))
            }
        },
    }
}

pub fn assert_assets_collection_empty_store(collection: &CollectionKey) -> Result<(), String> {
    let rule = get_state_rule(collection)?;

    match rule.mem() {
        Memory::Heap => STATE.with(|state| {
            let state_ref = state.borrow();
            let assets = collect_assets_heap(collection, &state_ref.heap.storage.assets);
            assert_assets_collection_empty_impl(&assets, collection)
        }),
        Memory::Stable => STATE.with(|state| {
            let stable = get_assets_stable(collection, &state.borrow().stable.assets);
            let assets: Vec<(&FullPath, &Asset)> = stable
                .iter()
                .map(|(_, asset)| (&asset.key.full_path, asset))
                .collect();
            assert_assets_collection_empty_impl(&assets, collection)
        }),
    }
}

fn assert_assets_collection_empty_impl(
    assets: &[(&FullPath, &Asset)],
    collection: &CollectionKey,
) -> Result<(), String> {
    let values = filter_collection_values(collection.clone(), assets);

    if !values.is_empty() {
        return Err(msg_storage_collection_not_empty(collection));
    }

    Ok(())
}

fn secure_list_assets_impl(
    context: &StoreContext,
    filters: &ListParams,
) -> Result<ListResults<AssetNoContent>, String> {
    let rule = get_state_rule(context.collection)?;

    match rule.mem() {
        Memory::Heap => STATE.with(|state| {
            let state_ref = state.borrow();
            let assets = collect_assets_heap(context.collection, &state_ref.heap.storage.assets);
            Ok(list_assets_impl(&assets, context, &rule, filters))
        }),
        Memory::Stable => STATE.with(|state| {
            let stable = get_assets_stable(context.collection, &state.borrow().stable.assets);
            let assets: Vec<(&FullPath, &Asset)> = stable
                .iter()
                .map(|(_, asset)| (&asset.key.full_path, asset))
                .collect();
            Ok(list_assets_impl(&assets, context, &rule, filters))
        }),
    }
}

fn list_assets_impl(
    assets: &[(&FullPath, &Asset)],
    &StoreContext {
        caller,
        controllers,
        collection,
    }: &StoreContext,
    rule: &Rule,
    filters: &ListParams,
) -> ListResults<AssetNoContent> {
    let matches = filter_values(
        caller,
        controllers,
        &rule.read,
        collection.clone(),
        filters,
        assets,
    );

    let values = list_values(&matches, filters);

    ListResults::<AssetNoContent> {
        items: values
            .items
            .into_iter()
            .map(|(_, asset)| map_asset_no_content(&asset))
            .collect(),
        items_length: values.items_length,
        items_page: values.items_page,
        matches_length: values.matches_length,
        matches_pages: values.matches_pages,
    }
}

fn secure_delete_asset_impl(
    context: &StoreContext,
    full_path: FullPath,
    config: &StorageConfig,
) -> Result<Option<Asset>, String> {
    let rule = get_state_rule(context.collection)?;

    delete_asset_impl(context, full_path, &rule, config)
}

fn delete_asset_impl(
    context: &StoreContext,
    full_path: FullPath,
    rule: &Rule,
    config: &StorageConfig,
) -> Result<Option<Asset>, String> {
    let asset = get_state_asset(context.collection, &full_path, rule);

    match asset {
        None => Err(ERROR_ASSET_NOT_FOUND.to_string()),
        Some(asset) => {
            if !assert_permission(
                &rule.write,
                asset.key.owner,
                context.caller,
                context.controllers,
            ) {
                return Err(ERROR_ASSET_NOT_FOUND.to_string());
            }

            increment_and_assert_rate_runtime(context.collection, &rule.rate_config)?;

            invoke_assert_delete_asset(&context.caller, &asset)?;

            let deleted = delete_state_asset(context.collection, &full_path, rule);
            delete_runtime_certified_asset(&asset);

            // We just removed the rewrite for /404.html in the certification tree therefore if /index.html exists, we want to reintroduce it as rewrite
            if *full_path == *ROOT_404_HTML {
                if let Some(index_asset) =
                    get_state_asset(context.collection, &ROOT_INDEX_HTML.to_string(), rule)
                {
                    update_runtime_certified_asset(&index_asset, config);
                }
            }

            Ok(deleted)
        }
    }
}

fn delete_assets_impl(
    full_paths: &Vec<FullPath>,
    collection: &CollectionKey,
    rule: &Rule,
) -> Result<(), String> {
    for full_path in full_paths {
        let deleted_asset = delete_state_asset(collection, full_path, rule);

        match deleted_asset {
            None => {}
            Some(deleted_asset) => {
                delete_runtime_certified_asset(&deleted_asset);
            }
        }
    }

    Ok(())
}

/// Count the number of assets in a collection's store.
///
/// This function retrieves the state rule for the specified collection and counts the assets
/// based on the memory type (Heap or Stable). It returns the count as a `Result<usize, String>`
/// on success, or an error message as `Err(String)` if an issue occurs during counting.
///
/// # Parameters
/// - `collection`: A reference to the `CollectionKey` representing the collection to count assets in.
///
/// # Returns
/// - `Ok(usize)`: The count of assets in the collection.
/// - `Err(String)`: An error message if counting fails.
///
/// This function provides a convenient way to determine the number of assets in a Juno collection's store.
pub fn count_collection_assets_store(collection: &CollectionKey) -> Result<usize, String> {
    let rule = get_state_rule(collection)?;

    match rule.mem() {
        Memory::Heap => STATE.with(|state| {
            let state_ref = state.borrow();
            let length = count_assets_heap(collection, &state_ref.heap.storage.assets);
            Ok(length)
        }),
        Memory::Stable => STATE.with(|state| {
            let length = count_assets_stable(collection, &state.borrow().stable.assets);
            Ok(length)
        }),
    }
}

/// Delete multiple assets from a collection's store based on filter criteria.
///
/// This function deletes assets from a collection's store that match the specified filter criteria.
/// It returns a `Result<Vec<Option<Asset>>, String>`, where `Ok(Vec<Option<Asset>>)` contains a vector of
/// `Option<Asset>` values for each deleted asset (or `None` if no asset was found for that entry), or an
/// error message as `Err(String)` if the deletion encounters issues.
///
/// # Parameters
/// - `caller`: The `Principal` representing the caller initiating the deletion.
/// - `collection`: A `CollectionKey` representing the collection from which to delete the assets.
/// - `filters`: A reference to `ListParams` containing the filter criteria for selecting assets to delete.
///
/// # Returns
/// - `Ok(Vec<Option<Asset>>)`:
///   - Each element in the vector represents the result of a delete operation for an asset:
///     - `Some(Asset)`: The successfully deleted asset.
///     - `None`: Indicates no asset found matching the specified filter criteria for this entry.
/// - `Err(String)`: An error message if the deletion operation fails.
///
/// This function allows batch deletion of assets in a Juno collection's store that match the specified
/// filter criteria, providing context for each deleted asset or error messages if any issues occur.
pub fn delete_filtered_assets_store(
    caller: Principal,
    collection: CollectionKey,
    filters: &ListParams,
) -> Result<Vec<Option<Asset>>, String> {
    let controllers: Controllers = get_controllers();

    let context = StoreContext {
        caller,
        controllers: &controllers,
        collection: &collection,
    };

    let assets = secure_list_assets_impl(&context, filters)?;

    delete_filtered_assets_store_impl(&context, &assets)
}

fn delete_filtered_assets_store_impl(
    context: &StoreContext,
    assets: &ListResults<AssetNoContent>,
) -> Result<Vec<Option<Asset>>, String> {
    let rule = get_state_rule(context.collection)?;
    let config = get_config_store();

    let mut results: Vec<Option<Asset>> = Vec::new();

    for (_, asset) in &assets.items {
        let deleted_asset =
            delete_asset_impl(context, asset.key.full_path.clone(), &rule, &config)?;

        results.push(deleted_asset);
    }

    Ok(results)
}

///
/// Upload batch and chunks
///

pub fn create_batch_store(caller: Principal, init: InitAssetKey) -> Result<BatchId, String> {
    let controllers: Controllers = get_controllers();
    let config = get_config();

    secure_create_batch_impl(caller, &controllers, &config, init)
}

pub fn create_chunk_store(caller: Principal, chunk: UploadChunk) -> Result<ChunkId, String> {
    let config = get_config();

    create_chunk(caller, &config, chunk)
}

pub fn commit_batch_store(caller: Principal, commit_batch: CommitBatch) -> Result<Asset, String> {
    let controllers: Controllers = get_controllers();
    let config = get_config();

    let asset = commit_batch_storage(
        caller,
        &controllers,
        &config,
        commit_batch,
        &StorageAssertions,
        &StorageState,
        &StorageUpload,
    )?;

    let config = get_config();

    update_runtime_certified_asset(&asset, &config);

    Ok(asset)
}

fn secure_create_batch_impl(
    caller: Principal,
    controllers: &Controllers,
    config: &StorageConfig,
    init: InitAssetKey,
) -> Result<BatchId, String> {
    let rule = get_state_rule(&init.collection)?;

    if !(public_permission(&rule.write)
        || is_known_user(caller)
        || is_controller(caller, controllers))
    {
        return Err(UPLOAD_NOT_ALLOWED.to_string());
    }

    create_batch(caller, controllers, config, init, None, &StorageState)
}

///
/// Config
///

pub fn set_config_store(config: &StorageConfig) {
    insert_state_config(config);

    init_runtime_certified_assets();
}

pub fn get_config_store() -> StorageConfig {
    get_state_config()
}

///
/// Domain
///

pub fn set_domain_store(domain_name: &DomainName, bn_id: &Option<String>) -> Result<(), String> {
    set_domain_impl(domain_name, bn_id)
}

pub fn delete_domain_store(domain_name: &DomainName) -> Result<(), String> {
    delete_domain_impl(domain_name)
}

pub fn get_custom_domains_store() -> CustomDomains {
    get_state_domains()
}

fn delete_domain_impl(domain_name: &DomainName) -> Result<(), String> {
    delete_state_domain(domain_name);

    update_custom_domains_asset(&StorageState)
}

fn set_domain_impl(domain_name: &DomainName, bn_id: &Option<String>) -> Result<(), String> {
    set_state_domain_impl(domain_name, bn_id);

    update_custom_domains_asset(&StorageState)
}

fn set_state_domain_impl(domain_name: &DomainName, bn_id: &Option<String>) {
    let domain = get_state_domain(domain_name);

    let custom_domain = build_custom_domain(domain, bn_id);

    insert_state_domain(domain_name, &custom_domain);
}