This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
storage.mo
246 lines (196 loc) · 8.05 KB
/
storage.mo
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
import Nat "mo:base/Nat";
import Text "mo:base/Text";
import HashMap "mo:base/HashMap";
import Iter "mo:base/Iter";
import Error "mo:base/Error";
import Blob "mo:base/Blob";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Types "../types/types";
import HTTP "../types/http.types";
import StorageTypes "./storage.types";
import Utils "../utils/utils";
import StorageStore "./storage.store";
import WalletUtils "../utils/wallet.utils";
actor class StorageBucket(owner: Types.UserId) = this {
private let BATCH_EXPIRY_NANOS = 300_000_000_000;
private type Asset = StorageTypes.Asset;
private type AssetKey = StorageTypes.AssetKey;
private type AssetEncoding = StorageTypes.AssetEncoding;
private type Chunk = StorageTypes.Chunk;
private type HttpRequest = HTTP.HttpRequest;
private type HttpResponse = HTTP.HttpResponse;
private type HeaderField = HTTP.HeaderField;
private type StreamingCallbackHttpResponse = HTTP.StreamingCallbackHttpResponse;
private type StreamingCallbackToken = HTTP.StreamingCallbackToken;
private type StreamingStrategy = HTTP.StreamingStrategy;
private let walletUtils: WalletUtils.WalletUtils = WalletUtils.WalletUtils();
private stable let user: Types.UserId = owner;
// Preserve the application state on upgrades
private stable var entries : [(Text, Asset)] = [];
let storageStore: StorageStore.StorageStore = StorageStore.StorageStore();
/**
* HTTP
*/
public shared query({caller}) func http_request({method: Text; url: Text;} : HttpRequest) : async HttpResponse {
try {
if (Text.notEqual(method, "GET")) {
return {
body = Blob.toArray(Text.encodeUtf8("Method Not Allowed."));
headers = [];
status_code = 405;
streaming_strategy = null;
};
};
let result: Result.Result<Asset, Text> = storageStore.getAssetForUrl(url);
switch (result) {
case (#ok {key: AssetKey; headers: [HeaderField]; encoding: AssetEncoding;}) {
return {
body = encoding.contentChunks[0];
headers;
status_code = 200;
streaming_strategy = createStrategy(key, encoding, headers);
};
};
case (#err error) {
};
};
return {
body = Blob.toArray(Text.encodeUtf8("Permission denied. Could not perform this operation."));
headers = [];
status_code = 403;
streaming_strategy = null;
};
} catch (err) {
return {
body = Blob.toArray(Text.encodeUtf8("Unexpected error: " # Error.message(err)));
headers = [];
status_code = 500;
streaming_strategy = null;
};
}
};
public shared query({caller}) func http_request_streaming_callback(streamingToken: StreamingCallbackToken) : async StreamingCallbackHttpResponse {
let result: Result.Result<Asset, Text> = storageStore.getAsset(streamingToken.fullPath, streamingToken.token);
switch (result) {
case (#ok {key: AssetKey; headers: [HeaderField]; encoding: AssetEncoding;}) {
return {
token = createToken(key, streamingToken.index, encoding, headers);
body = encoding.contentChunks[streamingToken.index];
};
};
case (#err error) {
throw Error.reject("Streamed asset not found: " # error);
};
};
};
private func createStrategy(key: AssetKey, encoding: AssetEncoding, headers: [HeaderField]) : ?StreamingStrategy {
let streamingToken: ?StreamingCallbackToken = createToken(key, 0, encoding, headers);
switch (streamingToken) {
case (null) { null };
case (?streamingToken) {
// Hack: https://forum.dfinity.org/t/cryptic-error-from-icx-proxy/6944/8
// Issue: https://github.com/dfinity/candid/issues/273
let self: Principal = Principal.fromActor(this);
let canisterId: Text = Principal.toText(self);
let canister = actor (canisterId) : actor { http_request_streaming_callback : shared () -> async () };
return ?#Callback({
token = streamingToken;
callback = canister.http_request_streaming_callback;
});
};
};
};
private func createToken(key: AssetKey, chunkIndex: Nat, encoding: AssetEncoding, headers: [HeaderField]) : ?StreamingCallbackToken {
if (chunkIndex + 1 >= encoding.contentChunks.size()) {
return null;
};
let streamingToken: ?StreamingCallbackToken = ?{
fullPath = key.fullPath;
token = key.token;
headers;
index = chunkIndex + 1;
sha256 = null;
};
return streamingToken;
};
/**
* Upload
*/
public shared({caller}) func initUpload(key: AssetKey) : async ({batchId : Nat}) {
if (Utils.isPrincipalNotEqual(caller, user)) {
throw Error.reject("User does not have the permission to upload data.");
};
let nextBatchID: Nat = storageStore.createBatch(key);
return {batchId = nextBatchID};
};
public shared({caller}) func uploadChunk(chunk: Chunk) : async ({chunkId : Nat}) {
if (Utils.isPrincipalNotEqual(caller, user)) {
throw Error.reject("User does not have the permission to a upload any chunks of content.");
};
let (result: {#chunkId: Nat; #error: Text;}) = storageStore.createChunk(chunk);
switch (result) {
case (#error error) {
throw Error.reject(error);
};
case (#chunkId chunkId) {
return {chunkId};
};
};
};
public shared({caller}) func commitUpload(
{batchId; chunkIds; headers;} : {
batchId: Nat;
headers: [HeaderField];
chunkIds: [Nat];
},
) : async () {
if (Utils.isPrincipalNotEqual(caller, user)) {
throw Error.reject("User does not have the permission to commit an upload.");
};
let ({error}: {error: ?Text;}) = storageStore.commitBatch({batchId; headers; chunkIds;});
switch (error) {
case (?error) {
throw Error.reject(error);
};
case null {};
};
};
/**
* List and delete
*/
public shared query({ caller }) func list(folder: ?Text) : async [AssetKey] {
if (Utils.isPrincipalNotEqual(caller, user)) {
throw Error.reject("User does not have the permission to list the assets.");
};
let keys: [AssetKey] = storageStore.getKeys(folder);
return keys;
};
public shared({ caller }) func del({fullPath; token;}: {fullPath: Text; token: ?Text;}) : async () {
if (Utils.isPrincipalNotEqual(caller, user)) {
throw Error.reject("User does not have the permission to delete an asset.");
};
let result: Result.Result<Asset, Text> = storageStore.deleteAsset(fullPath, token);
switch (result) {
case (#ok asset) {};
case (#err error) {
throw Error.reject("Asset cannot be deleted: " # error);
};
};
};
/**
* Canister mgmt
*/
// TODO: inter-canister call secure caller === manager canister !!!
// Or as only controllers can execute following is enough security?
public shared({ caller }) func transferCycles(): async() {
await walletUtils.transferCycles(caller);
};
system func preupgrade() {
entries := Iter.toArray(storageStore.preupgrade().entries());
};
system func postupgrade() {
storageStore.postupgrade(entries);
entries := [];
};
}