junobuild_storage/
strategies.rs

1use crate::types::config::StorageConfig;
2use crate::types::state::FullPath;
3use crate::types::store::{
4    Asset, AssetAssertUpload, AssetEncoding, Batch, EncodingType, ReferenceId,
5};
6use candid::Principal;
7use junobuild_collections::types::core::CollectionKey;
8use junobuild_collections::types::rules::{Memory, Rule};
9use junobuild_shared::types::core::Blob;
10use junobuild_shared::types::domain::CustomDomains;
11use junobuild_shared::types::state::Controllers;
12
13pub trait StorageAssertionsStrategy {
14    fn invoke_assert_upload_asset(
15        &self,
16        caller: &Principal,
17        asset: &AssetAssertUpload,
18    ) -> Result<(), String>;
19
20    fn increment_and_assert_storage_usage(
21        &self,
22        caller: &Principal,
23        controllers: &Controllers,
24        collection: &CollectionKey,
25        max_changes_per_user: Option<u32>,
26    ) -> Result<(), String>;
27}
28
29pub trait StorageStateStrategy {
30    fn get_content_chunks(
31        &self,
32        encoding: &AssetEncoding,
33        chunk_index: usize,
34        memory: &Memory,
35    ) -> Option<Blob>;
36
37    fn get_public_asset(
38        &self,
39        full_path: FullPath,
40        token: Option<String>,
41    ) -> Option<(Asset, Memory)>;
42
43    fn get_rule(&self, collection: &CollectionKey) -> Result<Rule, String>;
44
45    fn get_config(&self) -> StorageConfig;
46
47    fn get_domains(&self) -> CustomDomains;
48
49    fn get_asset(
50        &self,
51        collection: &CollectionKey,
52        full_path: &FullPath,
53        rule: &Rule,
54    ) -> Option<Asset>;
55
56    fn insert_asset(
57        &self,
58        collection: &CollectionKey,
59        full_path: &FullPath,
60        asset: &Asset,
61        rule: &Rule,
62    );
63
64    fn delete_asset(
65        &self,
66        collection: &CollectionKey,
67        full_path: &FullPath,
68        rule: &Rule,
69    ) -> Option<Asset>;
70}
71
72pub trait StorageUploadStrategy {
73    fn insert_asset_encoding(
74        &self,
75        full_path: &FullPath,
76        encoding_type: &EncodingType,
77        encoding: &AssetEncoding,
78        asset: &mut Asset,
79        rule: &Rule,
80    );
81
82    fn insert_asset(&self, batch: &Batch, asset: &Asset, rule: &Rule) -> Result<(), String>;
83
84    fn get_asset(
85        &self,
86        reference_id: &Option<ReferenceId>,
87        collection: &CollectionKey,
88        full_path: &FullPath,
89        rule: &Rule,
90    ) -> Result<Option<Asset>, String>;
91}