/
FileStorage.mo
348 lines (284 loc) · 8.78 KB
/
FileStorage.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import Blob "mo:base/Blob";
import Buffer "mo:base/Buffer";
import Error "mo:base/Error";
import Float "mo:base/Float";
import Hash "mo:base/Hash";
import HashMap "mo:base/HashMap";
import Iter "mo:base/Iter";
import Map "mo:hashmap/Map";
import Nat "mo:base/Nat";
import Nat32 "mo:base/Nat32";
import Option "mo:base/Option";
import Order "mo:base/Order";
import Prim "mo:prim";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Text "mo:base/Text";
import Time "mo:base/Time";
import Timer "mo:base/Timer";
import { ofBlob } "./CRC32";
import Types "./types";
import Utils "./utils";
import Debug "mo:base/Debug";
actor class FileStorage() = this {
type Asset = Types.Asset;
type Asset_ID = Types.Asset_ID;
type AssetChunk = Types.AssetChunk;
type AssetProperties = Types.AssetProperties;
type Chunk_ID = Types.Chunk_ID;
let ACTOR_NAME : Text = "FileStorage";
let VERSION : Nat = 1;
stable var timer_id : Nat = 0;
// change me when in production
let IS_PROD : Bool = false;
let { nhash; thash } = Map;
private var assets = Map.new<Asset_ID, Asset>(thash);
private var chunks = Map.new<Chunk_ID, AssetChunk>(nhash);
stable var assets_stable_storage : [(Asset_ID, Asset)] = [];
private var chunk_id_count : Chunk_ID = 0;
private func compare(a : AssetChunk, b : AssetChunk) : Order.Order {
if (a.order < b.order) {
return #less;
};
if (a.order > b.order) {
return #greater;
};
return #equal;
};
public shared ({ caller }) func create_chunk(batch_id : Text, content : Blob, order : Nat) : async Nat {
chunk_id_count := chunk_id_count + 1;
let checksum = ofBlob(content);
let asset_chunk : AssetChunk = {
batch_id = batch_id;
checksum = checksum;
content = content;
created = Time.now();
filename = "";
id = chunk_id_count;
order = order;
owner = caller;
};
ignore Map.put(chunks, nhash, chunk_id_count, asset_chunk);
return chunk_id_count;
};
public shared ({ caller }) func commit_batch(batch_id : Text, chunk_ids : [Chunk_ID], asset_properties : AssetProperties) : async Result.Result<Asset_ID, Text> {
let ASSET_ID = Utils.generate_uuid();
let CANISTER_ID = Principal.toText(Principal.fromActor(this));
var chunks_to_commit = Buffer.Buffer<AssetChunk>(0);
var asset_content = Buffer.Buffer<Blob>(0);
var content_size = 0;
var asset_checksum : Nat32 = 0;
let modulo_value : Nat32 = 400_000_000;
for (chunk in Map.vals(chunks)) {
if (chunk.batch_id == batch_id) {
if (chunk.owner != caller) {
return #err("Not Owner of Chunk");
};
chunks_to_commit.add(chunk);
};
};
chunks_to_commit.sort(compare);
for (chunk in chunks_to_commit.vals()) {
asset_content.add(chunk.content);
asset_checksum := (asset_checksum + chunk.checksum) % modulo_value;
content_size := content_size + chunk.content.size();
};
if (Nat32.notEqual(asset_checksum, asset_properties.checksum)) {
return #err("Invalid Checksum: Chunk Missing");
};
for (chunk in chunks_to_commit.vals()) {
Map.delete(chunks, nhash, chunk.id);
};
let asset : Types.Asset = {
canister_id = CANISTER_ID;
chunks_size = asset_content.size();
content = Option.make(Buffer.toArray(asset_content));
content_encoding = asset_properties.content_encoding;
content_size = content_size;
content_type = asset_properties.content_type;
created = Time.now();
filename = asset_properties.filename;
id = ASSET_ID;
url = Utils.generate_asset_url({
asset_id = ASSET_ID;
canister_id = CANISTER_ID;
is_prod = IS_PROD;
});
owner = Principal.toText(caller);
};
ignore Map.put(assets, thash, ASSET_ID, asset);
return #ok(asset.id);
};
public shared ({ caller }) func delete_asset(id : Asset_ID) : async Result.Result<Text, Text> {
switch (Map.get(assets, thash, id)) {
case (?asset) {
if (asset.owner == Principal.toText(caller)) {
Map.delete(assets, thash, id);
return #ok("Asset deleted successfully.");
} else {
return #err("Permission denied: You are not the owner of this asset.");
};
};
case (_) {
return #err("Asset not found.");
};
};
};
func clear_expired_chunks() : async () {
let currentTime = Time.now();
let fiveMinutes = 5 * 60 * 1000000000; // Convert 5 minutes to nanoseconds
let filteredChunks = Map.mapFilter<Chunk_ID, AssetChunk, AssetChunk>(
chunks,
nhash,
func(key : Chunk_ID, assetChunk : AssetChunk) : ?AssetChunk {
let age = currentTime - assetChunk.created;
if (age <= fiveMinutes) {
return ?assetChunk;
} else {
return null;
};
},
);
chunks := filteredChunks;
};
public query func assets_list() : async Result.Result<[Asset], Text> {
var assets_list = Buffer.Buffer<Asset>(0);
for (asset in Map.vals(assets)) {
let asset_without_content : Asset = {
asset with content = null;
};
assets_list.add(asset_without_content);
};
return #ok(Buffer.toArray(assets_list));
};
public query func get(id : Asset_ID) : async Result.Result<Asset, Text> {
switch (Map.get(assets, thash, id)) {
case (?asset) {
let asset_without_content : Asset = {
asset with content = null;
};
return #ok(asset_without_content);
};
case (_) {
return #err("Asset Not Found");
};
};
};
public query func chunks_size() : async Nat {
return Map.size(chunks);
};
public query func is_full() : async Bool {
let MAX_SIZE_THRESHOLD_MB : Float = 1500;
let rts_memory_size : Nat = Prim.rts_memory_size();
let mem_size : Float = Float.fromInt(rts_memory_size);
let memory_in_megabytes = Float.abs(mem_size * 0.000001);
if (memory_in_megabytes > MAX_SIZE_THRESHOLD_MB) {
return true;
} else {
return false;
};
};
// ------------------------- Get Asset HTTP -------------------------
public shared query ({ caller }) func http_request(request : Types.HttpRequest) : async Types.HttpResponse {
let NOT_FOUND : [Nat8] = Blob.toArray(Text.encodeUtf8("Asset Not Found"));
let asset_id = Utils.get_asset_id(request.url);
switch (Map.get(assets, thash, asset_id)) {
case (?asset) {
let filename = Text.concat("attachment; filename=", asset.filename);
return {
body = Blob.toArray(Option.get(asset.content, [])[0]);
headers = [
("Content-Type", asset.content_type),
("accept-ranges", "bytes"),
("Content-Disposition", filename),
("cache-control", "private, max-age=0"),
];
status_code = 200;
streaming_strategy = create_strategy({
asset_id = asset_id;
chunk_index = 0;
data_chunks_size = asset.chunks_size;
});
};
};
case _ {
return {
body = NOT_FOUND;
headers = [];
status_code = 404;
streaming_strategy = null;
};
};
};
};
private func create_strategy(args : Types.CreateStrategyArgs) : ?Types.StreamingStrategy {
switch (create_token(args)) {
case (null) { null };
case (?token) {
let self = Principal.fromActor(this);
let canister_id : Text = Principal.toText(self);
let canister = actor (canister_id) : actor {
http_request_streaming_callback : shared () -> async ();
};
return ?#Callback({
token;
callback = canister.http_request_streaming_callback;
});
};
};
};
private func create_token(args : Types.CreateStrategyArgs) : ?Types.StreamingCallbackToken {
if (args.chunk_index + 1 >= args.data_chunks_size) {
return null;
} else {
let token = {
asset_id = args.asset_id;
chunk_index = args.chunk_index + 1;
content_encoding = "gzip";
};
return ?token;
};
};
public shared query ({ caller }) func http_request_streaming_callback(
st : Types.StreamingCallbackToken
) : async Types.StreamingCallbackHttpResponse {
switch (Map.get(assets, thash, st.asset_id)) {
case (null) throw Error.reject("asset_id not found: " # st.asset_id);
case (?asset) {
return {
token = create_token({
asset_id = st.asset_id;
chunk_index = st.chunk_index;
data_chunks_size = asset.chunks_size;
});
body = Option.get(asset.content, [])[st.chunk_index];
};
};
};
};
// ------------------------- Canister Management -------------------------
public query func version() : async Nat {
return VERSION;
};
public func start_clear_expired_chunks() : async Timer.TimerId {
if (timer_id == 0) {
timer_id := 1;
return Timer.recurringTimer(#seconds(300), clear_expired_chunks);
} else {
return timer_id;
};
};
public func stop_clear_expired_chunks() : async Timer.TimerId {
timer_id := 0;
Timer.cancelTimer(1);
return 0;
};
// ------------------------- System Methods -------------------------
system func preupgrade() {
assets_stable_storage := Iter.toArray(Map.entries(assets));
};
system func postupgrade() {
assets := Map.fromIter<Asset_ID, Asset>(assets_stable_storage.vals(), thash);
assets_stable_storage := [];
};
};