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, Permission, 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 assert_key(
15        &self,
16        full_path: &FullPath,
17        description: &Option<String>,
18        collection: &CollectionKey,
19    ) -> Result<(), String>;
20
21    fn assert_write_on_dapp_collection(&self, caller: Principal, controllers: &Controllers)
22        -> bool;
23
24    fn assert_write_on_system_collection(
25        &self,
26        caller: Principal,
27        collection: &CollectionKey,
28        controllers: &Controllers,
29    ) -> bool;
30
31    fn assert_create_permission(
32        &self,
33        permission: &Permission,
34        caller: Principal,
35        collection: &CollectionKey,
36        controllers: &Controllers,
37    ) -> bool;
38
39    fn assert_update_permission(
40        &self,
41        permission: &Permission,
42        owner: Principal,
43        caller: Principal,
44        collection: &CollectionKey,
45        controllers: &Controllers,
46    ) -> bool;
47
48    fn assert_list_permission(
49        &self,
50        permission: &Permission,
51        owner: Principal,
52        caller: Principal,
53        collection: &CollectionKey,
54        controllers: &Controllers,
55    ) -> bool;
56
57    fn invoke_assert_upload_asset(
58        &self,
59        caller: &Principal,
60        asset: &AssetAssertUpload,
61    ) -> Result<(), String>;
62
63    fn increment_and_assert_storage_usage(
64        &self,
65        caller: &Principal,
66        controllers: &Controllers,
67        collection: &CollectionKey,
68        max_changes_per_user: Option<u32>,
69    ) -> Result<(), String>;
70}
71
72pub trait StorageStateStrategy {
73    fn get_content_chunks(
74        &self,
75        encoding: &AssetEncoding,
76        chunk_index: usize,
77        memory: &Memory,
78    ) -> Option<Blob>;
79
80    fn get_public_asset(
81        &self,
82        full_path: FullPath,
83        token: Option<String>,
84    ) -> Option<(Asset, Memory)>;
85
86    fn get_rule(&self, collection: &CollectionKey) -> Result<Rule, String>;
87
88    fn get_config(&self) -> StorageConfig;
89
90    fn get_domains(&self) -> CustomDomains;
91
92    fn get_asset(
93        &self,
94        collection: &CollectionKey,
95        full_path: &FullPath,
96        rule: &Rule,
97    ) -> Option<Asset>;
98
99    fn insert_asset(
100        &self,
101        collection: &CollectionKey,
102        full_path: &FullPath,
103        asset: &Asset,
104        rule: &Rule,
105    );
106
107    fn delete_asset(
108        &self,
109        collection: &CollectionKey,
110        full_path: &FullPath,
111        rule: &Rule,
112    ) -> Option<Asset>;
113}
114
115pub trait StorageUploadStrategy {
116    fn insert_asset_encoding(
117        &self,
118        reference_id: &Option<ReferenceId>,
119        full_path: &FullPath,
120        encoding_type: &EncodingType,
121        encoding: &AssetEncoding,
122        asset: &mut Asset,
123        rule: &Rule,
124    ) -> Result<(), String>;
125
126    fn insert_asset(&self, batch: &Batch, asset: &Asset, rule: &Rule) -> Result<(), String>;
127
128    fn get_asset(
129        &self,
130        reference_id: &Option<ReferenceId>,
131        collection: &CollectionKey,
132        full_path: &FullPath,
133        rule: &Rule,
134    ) -> Result<Option<Asset>, String>;
135}