1use ic_cdk::export::candid::{self, CandidType, Deserialize};
2use ic_cdk::api::call::CallResult;
3use candid::{Nat, Principal};
4
5#[derive(CandidType, Deserialize, Debug)]
6pub struct State {
7 balance: Nat,
8 memory_size: Nat,
9 stable_memory_size: u64,
10}
11
12#[derive(CandidType, Deserialize, Debug)]
13pub struct AssetExt {
14 pub file_extension: String,
15 pub upload_status: bool,
16 pub bucket_id: Principal,
17 pub aes_pub_key: Option<String>,
18 pub file_name: String,
19 pub file_key: String,
20 pub total_size: u64,
21 pub need_query_times: Nat,
22}
23
24#[derive(CandidType, Deserialize, Debug)]
25pub struct GET { pub flag: Nat, pub file_key: String }
26
27#[derive(CandidType, Deserialize, Debug)]
28pub struct OtherFile {
29 file_extension: String,
30 file_location: FileLocation,
31 file_name: String,
32 file_key: String,
33 file_url: String,
34}
35
36#[derive(CandidType, Deserialize, Debug)]
37struct ThumbNail { file_extension: String, image: Vec<u8> }
38
39#[derive(CandidType, Deserialize, Debug)]
40pub struct Avatar { pub data: Vec<u8>, pub data_type: String }
41
42#[derive(CandidType, Deserialize, Debug)]
43pub enum DataErr {
44 FileKeyErr,
45 FilePublic,
46 BlobSizeError,
47 PermissionDenied,
48 SharedRepeat,
49 FlagErr,
50 SharedNotSet,
51 MemoryInsufficient,
52 FileAesPubKeyNotExist,
53 UserAccessErr,
54 DeviceNotExist,
55 FileRepeat,
56 ShareRepeat,
57}
58
59#[derive(CandidType, Deserialize, Debug)]
60pub enum AvlSMResult { ok(u64), err(DataErr) }
61
62#[derive(CandidType, Deserialize, Debug)]
63pub enum CanisterStateResult { ok(State), err(DataErr) }
64
65#[derive(CandidType, Deserialize, Debug)]
66enum SetShareFileResult { ok(String), err(DataErr) }
67
68#[derive(CandidType, Deserialize, Debug)]
69enum GetSharedAesPublicResult { ok(String), err(DataErr) }
70
71#[derive(CandidType, Deserialize, Debug)]
72enum GetDefaultDeviceShareDapResult { ok(String), err(DataErr) }
73
74#[derive(CandidType, Deserialize, Debug)]
75enum DeleteOtherResult { ok(String), err(DataErr) }
76
77#[derive(CandidType, Deserialize, Debug)]
78pub enum DeleteKeyResult { ok(String), err(DataErr) }
79
80#[derive(CandidType, Deserialize, Debug)]
81enum DeleteShareFileResult { ok(String), err(DataErr) }
82
83#[derive(CandidType, Deserialize, Debug)]
84enum DeleteSharedFileResult { ok(String), err(DataErr) }
85
86#[derive(CandidType, Deserialize, Debug)]
87pub enum ClearAllResult { ok(String), err(DataErr) }
88
89#[derive(CandidType, Deserialize, Debug)]
90pub enum CycleBalanceResult { ok(Nat), err(DataErr) }
91
92#[derive(CandidType, Deserialize, Debug)]
93pub enum FileExt {
94 EncryptFileExt(AssetExt),
95 SharedFileExt{
96 file_extension: String,
97 other: Principal,
98 description: String,
99 file_name: String,
100 file_key: String,
101 isPublic: bool,
102 },
103 PlainFileExt(AssetExt),
104}
105
106#[derive(CandidType, Deserialize, Debug)]
107pub enum PutResult { ok(FileExt), err(DataErr) }
108
109#[derive(CandidType, Deserialize, Debug)]
110pub enum GetAssetExtKeyResult { ok(FileExt), err(DataErr) }
111
112#[derive(CandidType, Deserialize, Debug)]
113enum FileLocation { IPFS, Arweave }
114
115#[derive(CandidType, Deserialize, Debug)]
116pub enum GetAssetExtsResult {
117 ok(Vec<FileExt>,Vec<FileExt>,Vec<FileExt>,Vec<OtherFile>,Vec<OtherFile>,),
118 err(DataErr),
119}
120
121#[derive(CandidType, Deserialize, Debug)]
122enum GetCipherResult { ok(Vec<Vec<u8>>), err(DataErr) }
123
124#[derive(CandidType, Deserialize, Debug)]
125enum GetFileShareOtherResult { ok(Vec<Principal>), err(DataErr) }
126
127#[derive(CandidType, Deserialize, Debug)]
128enum GetOtherKeyResult { ok(OtherFile), err(DataErr) }
129
130#[derive(CandidType, Deserialize, Debug)]
131pub enum GetPlainResult { ok(Vec<u8>), err(DataErr) }
132
133#[derive(CandidType, Deserialize, Debug)]
134enum GetShareFilesResult { ok(Vec<FileExt>), err(DataErr) }
135
136#[derive(CandidType, Deserialize, Debug)]
137enum GetThumbnailResult { ok(ThumbNail), err(DataErr) }
138
139#[derive(CandidType, Deserialize, Debug)]
140pub struct Chunk { pub data: Vec<u8> }
141
142#[derive(CandidType, Deserialize, Debug)]
143pub enum PUT {
144 segment{
145 file_extension: String,
146 order: Nat,
147 chunk_number: Nat,
148 chunk: Chunk,
149 aes_pub_key: Option<String>,
150 file_name: String,
151 file_key: String,
152 total_size: u64,
153 },
154 thumb_nail{
155 file_extension: String,
156 aes_pub_key: Option<String>,
157 file_name: String,
158 file_key: String,
159 image: Vec<u8>,
160 },
161}
162
163#[derive(CandidType, Deserialize, Debug)]
164pub enum FilePut {
165 EncryptFilePut(PUT),
166 SharedFilePut{
167 file_extension: String,
168 other: Principal,
169 aes_pub_key: Option<String>,
170 description: String,
171 file_name: String,
172 file_key: String,
173 isPublic: bool,
174 },
175 PlainFilePut(PUT),
176}
177
178#[derive(CandidType, Deserialize, Debug)]
179enum RecordResult { ok(bool), err(DataErr) }
180
181#[derive(CandidType, Deserialize, Debug)]
182pub enum UploadResult { ok, err(DataErr) }
183
184type DataBox = candid::Service;
185struct SERVICE(Principal);
186impl SERVICE{
187
188 pub async fn avl_sm(&self) -> CallResult<(AvlSMResult,)> {
189 ic_cdk::call(self.0, "avlSM", ()).await
190 }
191
192 pub async fn canister_state(&self) -> CallResult<(CanisterStateResult,)> {
193 ic_cdk::call(self.0, "canisterState", ()).await
194 }
195
196 pub async fn clear_all(&self) -> CallResult<(ClearAllResult,)> {
197 ic_cdk::call(self.0, "clearall", ()).await
198 }
199
200 pub async fn cycle_balance(&self) -> CallResult<(CycleBalanceResult,)> {
201 ic_cdk::call(self.0, "cycleBalance", ()).await
202 }
203
204 pub async fn delete_share_file(
205 &self,
206 encrypt_file: String,
207 other: Principal,
208 ) -> CallResult<(DeleteShareFileResult,)> {
209 ic_cdk::call(self.0, "deleteShareFile", (encrypt_file,other,)).await
210 }
211
212 pub async fn delete_shared_file(&self, shared_file: String) -> CallResult<
213 (DeleteSharedFileResult,)
214 > { ic_cdk::call(self.0, "deleteSharedFile", (shared_file,)).await }
215
216 pub async fn delete_file(&self, file_key: String) -> CallResult<(DeleteKeyResult,)> {
217 ic_cdk::call(self.0, "deletekey", (file_key,)).await
218 }
219
220 pub async fn delete_other(
221 &self,
222 file_key: String,
223 file_location: FileLocation,
224 ) -> CallResult<(DeleteOtherResult,)> {
225 ic_cdk::call(self.0, "deleteother", (file_key,file_location,)).await
226 }
227
228 pub async fn get_asset_ext_key(&self, file_key: String) -> CallResult<(GetAssetExtKeyResult,)> {
229 ic_cdk::call(self.0, "getAssetextkey", (file_key,)).await
230 }
231
232 pub async fn get_asset_exts(&self) -> CallResult<(GetAssetExtsResult,)> {
233 ic_cdk::call(self.0, "getAssetexts", ()).await
234 }
235
236 pub async fn get_cipher(&self, g: GET) -> CallResult<(GetCipherResult,)> {
237 ic_cdk::call(self.0, "getCipher", (g,)).await
238 }
239
240 pub async fn get_default_device_share_dap(&self, encrypt_file: String) -> CallResult<
241 (GetDefaultDeviceShareDapResult,)
242 > { ic_cdk::call(self.0, "getDefaultDeviceShareDap", (encrypt_file,)).await }
243
244 pub async fn get_file_share_other(&self, encrypt_file: String) -> CallResult<
245 (GetFileShareOtherResult,)
246 > { ic_cdk::call(self.0, "getFileShareOther", (encrypt_file,)).await }
247
248 pub async fn get_other_key(
249 &self,
250 file_key: String,
251 file_location: FileLocation,
252 ) -> CallResult<(GetOtherKeyResult,)> {
253 ic_cdk::call(self.0, "getOtherkey", (file_key,file_location,)).await
254 }
255
256 pub async fn get_owner(&self) -> CallResult<(Principal,)> {
257 ic_cdk::call(self.0, "getOwner", ()).await
258 }
259
260 pub async fn get_plain(&self, g: GET) -> CallResult<(GetPlainResult,)> {
261 ic_cdk::call(self.0, "getPlain", (g,)).await
262 }
263
264 pub async fn get_share_files(&self) -> CallResult<(GetShareFilesResult,)> {
265 ic_cdk::call(self.0, "getShareFiles", ()).await
266 }
267
268 pub async fn get_shared_aes_public(&self, shared_file: String) -> CallResult<
269 (GetSharedAesPublicResult,)
270 > { ic_cdk::call(self.0, "getSharedAesPublic", (shared_file,)).await }
271
272 pub async fn get_thumbnail(&self, file_key: String) -> CallResult<(GetThumbnailResult,)> {
273 ic_cdk::call(self.0, "getThumbnail", (file_key,)).await
274 }
275
276 pub async fn get_version(&self) -> CallResult<(Nat,)> {
277 ic_cdk::call(self.0, "getVersion", ()).await
278 }
279
280 pub async fn put(&self, file_put: FilePut) -> CallResult<(PutResult,)> {
281 ic_cdk::call(self.0, "put", (file_put,)).await
282 }
283
284 pub async fn record(&self, other_file: OtherFile) -> CallResult<(RecordResult,)> {
285 ic_cdk::call(self.0, "record", (other_file,)).await
286 }
287
288 pub async fn set_share_file(
289 &self,
290 encrypt_file: String,
291 other: Principal,
292 default_aes_pubkey: String,
293 ) -> CallResult<(SetShareFileResult,)> {
294 ic_cdk::call(self.0, "setShareFile", (encrypt_file,other,default_aes_pubkey,)).await
295 }
296
297 pub async fn upload_avatar(&self, args: Avatar) -> CallResult<(UploadResult,)> {
298 ic_cdk::call(self.0, "upload", (args,)).await
299 }
300
301}