Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2e/bats/assetscanister.bash
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ teardown() {
assert_eq '(vec { 88; 87; 86; })'

assert_command_fail dfx canister call --query e2e_project_assets retrieve '("C")'
}

HOME=. assert_command_fail dfx canister call --update e2e_project_assets store '("index.js", vec { 1; 2; 3; })'
}
2 changes: 1 addition & 1 deletion nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"motoko": {
"ref": "master",
"repo": "ssh://git@github.com/dfinity-lab/motoko",
"rev": "719ea2b3f54de4c4c867bd34a8284e52b49999a7",
"rev": "ea4585c4b3606689e224c89ae809cf50ab07df30",
"type": "git"
},
"napalm": {
Expand Down
37 changes: 23 additions & 14 deletions src/distributed/assetstorage/assetstorage.mo
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import A "mo:base/AssocList";
import L "mo:base/List";
import AssocList "mo:base/AssocList";
import Debug "mo:base/Debug";
import List "mo:base/List";
import Prelude "mo:base/Prelude";
import Prim "mo:prim";

actor {

public type Path = Text;

public type Contents = Blob;

var stored: A.AssocList<Path, Contents> = L.nil<(Path, Contents)>();
private let initializer : Principal = Prim.caller();

private stable var db: AssocList.AssocList<Path, Contents> = List.nil();

func pathEq(a: Path, b: Path): Bool {
func eq(a: Path, b: Path): Bool {
return a == b;
};

public func store(path : Path, contents: Contents) : async () {
let (newStored, _) = A.replace<Path, Contents>(stored, path, pathEq, ?contents);
stored := newStored;
public shared { caller } func store(path : Path, contents : Contents) {
if (caller != initializer) {
Debug.print("not authorized");
Prelude.unreachable();
} else {
db := AssocList.replace<Path, Contents>(db, path, eq, ?contents).0;
};
};

public query func retrieve(path: Path): async Contents {
let result = A.find<Path, Contents>(stored, path, pathEq);
public query func retrieve(path : Path) : async Contents {
let result = AssocList.find<Path, Contents>(db, path, eq);
switch result {
case null {
// more than 8 chars treated as invalid UTF-8
// TODO: https://github.com/dfinity-lab/sdk/issues/701
throw Prim.error("notfound")
Debug.print("not found");
Prelude.unreachable();
};
case (?contents) { contents };
}
case (?contents) contents;
};
};
};