junobuild_storage/
strategies.rs

1use crate::types::config::StorageConfig;
2use crate::types::state::{AssetAccessToken, FullPath};
3use crate::types::store::{
4    Asset, AssetAssertUpload, AssetEncoding, Batch, EncodingType, ReferenceId,
5};
6use candid::Principal;
7use ic_certification::HashTree;
8use junobuild_collections::types::core::CollectionKey;
9use junobuild_collections::types::rules::{Memory, Permission, Rule};
10use junobuild_shared::types::core::Blob;
11use junobuild_shared::types::domain::CustomDomains;
12use junobuild_shared::types::state::Controllers;
13
14pub trait StorageAssertionsStrategy {
15    fn assert_key(
16        &self,
17        full_path: &FullPath,
18        description: &Option<String>,
19        collection: &CollectionKey,
20    ) -> Result<(), String>;
21
22    fn assert_write_on_dapp_collection(&self, caller: Principal, controllers: &Controllers)
23        -> bool;
24
25    fn assert_write_on_system_collection(
26        &self,
27        caller: Principal,
28        collection: &CollectionKey,
29        controllers: &Controllers,
30    ) -> bool;
31
32    fn assert_create_permission(
33        &self,
34        permission: &Permission,
35        caller: Principal,
36        collection: &CollectionKey,
37        controllers: &Controllers,
38    ) -> bool;
39
40    fn assert_update_permission(
41        &self,
42        permission: &Permission,
43        owner: Principal,
44        caller: Principal,
45        collection: &CollectionKey,
46        controllers: &Controllers,
47    ) -> bool;
48
49    fn assert_list_permission(
50        &self,
51        permission: &Permission,
52        owner: Principal,
53        caller: Principal,
54        collection: &CollectionKey,
55        controllers: &Controllers,
56    ) -> bool;
57
58    fn invoke_assert_upload_asset(
59        &self,
60        caller: &Principal,
61        asset: &AssetAssertUpload,
62    ) -> Result<(), String>;
63
64    fn increment_and_assert_storage_usage(
65        &self,
66        caller: &Principal,
67        controllers: &Controllers,
68        collection: &CollectionKey,
69        max_changes_per_user: Option<u32>,
70    ) -> Result<(), String>;
71}
72
73pub trait StorageStateStrategy {
74    fn get_content_chunks(
75        &self,
76        encoding: &AssetEncoding,
77        chunk_index: usize,
78        memory: &Memory,
79    ) -> Option<Blob>;
80
81    fn get_public_asset(
82        &self,
83        full_path: FullPath,
84        token: AssetAccessToken,
85    ) -> Option<(Asset, Memory)>;
86
87    fn get_rule(&self, collection: &CollectionKey) -> Result<Rule, String>;
88
89    fn get_config(&self) -> StorageConfig;
90
91    fn get_domains(&self) -> CustomDomains;
92
93    fn get_asset(
94        &self,
95        collection: &CollectionKey,
96        full_path: &FullPath,
97        rule: &Rule,
98    ) -> Option<Asset>;
99
100    fn insert_asset(
101        &self,
102        collection: &CollectionKey,
103        full_path: &FullPath,
104        asset: &Asset,
105        rule: &Rule,
106    );
107
108    fn insert_asset_encoding(
109        &self,
110        full_path: &FullPath,
111        encoding_type: &str,
112        encoding: &AssetEncoding,
113        asset: &mut Asset,
114        rule: &Rule,
115    );
116
117    fn delete_asset(
118        &self,
119        collection: &CollectionKey,
120        full_path: &FullPath,
121        rule: &Rule,
122    ) -> Option<Asset>;
123
124    fn init_certified_assets(&self);
125}
126
127pub trait StorageUploadStrategy {
128    fn insert_asset_encoding(
129        &self,
130        reference_id: &Option<ReferenceId>,
131        full_path: &FullPath,
132        encoding_type: &EncodingType,
133        encoding: &AssetEncoding,
134        asset: &mut Asset,
135        rule: &Rule,
136    ) -> Result<(), String>;
137
138    fn insert_asset(&self, batch: &Batch, asset: &Asset, rule: &Rule) -> Result<(), String>;
139
140    fn get_asset(
141        &self,
142        reference_id: &Option<ReferenceId>,
143        collection: &CollectionKey,
144        full_path: &FullPath,
145        rule: &Rule,
146    ) -> Result<Option<Asset>, String>;
147}
148
149pub trait StorageCertificateStrategy {
150    fn update_certified_data(&self);
151
152    // We use this function to access the auth signatures root hash
153    // in the storage crate when we build an HTTP response because the
154    // tree is a fork of both assets and signatures.
155    fn get_pruned_labeled_sigs_root_hash_tree(&self) -> HashTree;
156}