package main // Exhibit 13. The zoo holds no storage keys. It saves files into a person's storage by showing // storage an assertion from account that says "zoo, for this person" (rc.app_headers("storage", // true)). Storage decides the folder (Apps / Zoo); the app cannot choose another. // // One file: postcard() below is the whole path, the one to copy into an app. Many files (an importer, // a photo library): storage's bulk calls app/begin, app/commit, app/urls and app/list (bulk.odin; // storage's README, "In bulk"), which the self-check uses for 50 postcards at once. import "core:encoding/json" import "core:fmt" import "core:strings" import "core:time" import "ratcloud:rc" // A postcard, 1 bit per pixel: the background is the person's own colour, the words white. postcard_png :: proc(uid: i64, line := "") -> []u8 { id := int(uid) im := image_new(160, 104, {{u8(40 + (id * 37) % 120), u8(90 + (id * 61) % 100), u8(120 + (id * 17) % 100)}, {255, 255, 250}}) frame_rect(&im, 5, 5, 154, 98, 1) text(&im, 14, 16, "Greetings from", 1) text(&im, 14, 38, "the zoo", 1, 2) text(&im, 14, 74, line if line != "" else cat(ymd_hm(now()), " UTC"), 1) return png_encode(&im) } Card :: struct { file: struct { id: i64 `json:"id"`, path: string `json:"path"`, } `json:"file"`, bytes: int `json:"bytes"`, matched: bool `json:"matched"`, image: string `json:"image,omitempty"`, } // One file: storage's /app/save, then /app/fetch by the id it answered. The save answers JSON: // // {"saved": {"id": 42, "name": "postcard.png", "kind": "file", "size": 812, "type": "image/png", // "updated": 1760000000, "app": "zoo", ...}, // "path": "Apps/Zoo/postcards/postcard.png"} // // so the new file's id is answer.saved.id (keep it to fetch or trash the file later). The fetch // answers the file's bytes themselves, not JSON. Saving at a path already taken replaces it. postcard :: proc(me: ^rc.User) -> (Card, string) { png := postcard_png(me.id) a := call_app("storage", "/app/save?path=postcards%2Fpostcard.png", true, "POST", string(png), "image/png", 30_000) ans := parse(a.body) id, has := jint(ans, "saved", "id") if a.status != 200 || !has do return {}, fmt.tprintf("storage would not save it (%d): %s%s", a.status, jstr(ans, "error"), a.err) back := call_app("storage", fmt.tprintf("/app/fetch?id=%d", id), true, timeout_ms = 30_000) if back.status != 200 do return {}, fmt.tprintf("saved as file %d, but fetching it back answered %d", id, back.status) c := Card{bytes = len(png), matched = back.body == string(png), image = cat("data:image/png;base64,", b64(transmute([]u8)back.body))} c.file.id, c.file.path = id, jstr(ans, "path") return c, "" } Cards :: struct { count: int `json:"count"`, matched: int `json:"matched"`, listed: int `json:"listed"`, path: string `json:"path"`, ids: [2]i64 `json:"ids"`, ms: i64 `json:"ms"`, steps_ms: map[string]i64 `json:"steps_ms"`, one: ^Card `json:"one,omitempty"`, } // The check's batch: 50 postcards in one begin, one commit and one urls call, then listed. postcards_50 :: proc(me: ^rc.User) -> (Cards, string) { t0 := time.tick_now() lap := time.tick_now() steps := make(map[string]i64) mark :: proc(steps: ^map[string]i64, k: string, lap: ^time.Tick) { steps[k] = i64(time.duration_milliseconds(time.tick_since(lap^))) lap^ = time.tick_now() } paths := make([]string, 50) cards := make([][]u8, 50) for i in 0 ..< 50 { paths[i] = fmt.tprintf("postcards/check/%02d.png", i + 1) cards[i] = postcard_png(me.id, fmt.tprintf("no. %02d of 50", i + 1)) } mark(&steps, "draw", &lap) fb := strings.builder_make() for p, i in paths do fmt.sbprintf(&fb, `%s{{"path":%s,"size":%d,"type":"image/png"}}`, "," if i > 0 else "", jq(p), len(cards[i])) s, r := storage_call("begin", fmt.tprintf(`{{"files":[%s]}}`, strings.to_string(fb))) beg := jarr(r, "files") urls := 0 for f in beg do if jstr(f, "url") != "" do urls += 1 if s != 200 || urls != 50 do return {}, fmt.tprintf("storage would not begin them (%d): %s", s, to_json(jget(r, "error")) if jhas(r, "error") else to_json(beg[0] if len(beg) > 0 else r)) mark(&steps, "begin", &lap) puts := make([]Put, 50) for f, i in beg do puts[i] = {jstr(f, "url"), jstr(f, "type"), cards[i]} codes := put_many(puts) mark(&steps, "put", &lap) for c in codes do if c != 200 do return {}, fmt.tprintf("B2 refused some PUTs: %v", codes) ib := strings.builder_make() for f, i in beg do fmt.sbprintf(&ib, "%s%d", "," if i > 0 else "", jint(f, "id")) s, r = storage_call("commit", fmt.tprintf(`{{"ids":[%s]}}`, strings.to_string(ib))) files := make([dynamic]json.Value) for f in jarr(r, "files") do if jhas(f, "file") do append(&files, jget(f, "file")) if s != 200 || len(files) != 50 do return {}, fmt.tprintf("storage would not commit them all (%d): %s", s, to_json(r)) mark(&steps, "commit", &lap) fids := strings.builder_make() lo, hi: i64 = max(i64), 0 for f, i in files { id := jint(f, "id") lo, hi = min(lo, id), max(hi, id) fmt.sbprintf(&fids, "%s%d", "," if i > 0 else "", id) } s, r = storage_call("urls", fmt.tprintf(`{{"ids":[%s]}}`, strings.to_string(fids))) mark(&steps, "urls", &lap) got := make([dynamic]string) for f in jarr(r, "files") do if jstr(f, "url") != "" do append(&got, jstr(f, "url")) if s != 200 || len(got) != 50 do return {}, fmt.tprintf("storage would not hand out their URLs (%d)", s) back := get_many(got[:]) mark(&steps, "get", &lap) matched := 0 for g, i in back do if g.status == 200 && eq_bytes(g.body, cards[i]) do matched += 1 listed := make(map[string]bool) after: json.Value = json.Integer(0) for page := 0; page < 20; page += 1 { _, l := storage_call("list", fmt.tprintf(`{{"after":%s,"limit":1000}}`, to_json(after))) for f in jarr(l, "files") do listed[jstr(f, "path")] = true after = jget(l, "next") if !jhas(l, "next") do break } mark(&steps, "list", &lap) n := 0 for p in paths do if listed[cat("Apps/Zoo/", p)] || listed[p] do n += 1 return {count = 50, matched = matched, listed = n, path = "Apps/Zoo/postcards/check/", ids = {lo, hi}, ms = i64(time.duration_milliseconds(time.tick_since(t0))), steps_ms = steps}, "" } e13 :: proc() -> Exhibit { return { n = 13, try_ = "save a postcard", wing = "Storage", title = "Apps can save files for you", promise = "An app can save a file into your storage, under its own folder, without ever holding storage's keys.", block = `rc.app_headers("storage", true) proves to storage which app calls and for whom; storage saves only under Apps / . One file: POST /app/save answers {saved: {id, ...}, path}. Many: app/begin gives presigned PUTs (200 a call), the bytes go straight to B2, app/commit checks them all at once, app/urls hands out downloads.`, src = {"e13_postcards.odin", #load("e13_postcards.odin", string)}, files = srcs({"bulk.odin", #load("bulk.odin", string)}, {"png.odin", #load("png.odin", string)}), script = {"web/e13.js", #load("web/e13.js", string)}, show = proc(me: ^rc.User) -> string { d, s, found := stored_data(13, Cards) last := "" if found && s.ok do last = fmt.tprintf(`

The self-check saves 50 postcards for the robot each time: one begin, 50 PUTs straight to B2, one commit, one call for their 50 URLs, %d ms in all, %s.

`, d.ms, ago(s.at)) if me == nil do return cat(signin_line("to have the zoo save a postcard for you."), last) return cat(`

It appears in your storage, under Apps / Zoo.

`, last) }, api = proc(a: ^Api) -> bool { if (a.do_ != "postcard" && a.do_ != "postcards") || !a.post do return false if a.me == nil {reply_err(a, 401, "sign in first"); return true} if a.do_ == "postcard" { c, err := postcard(a.me) if err != "" {reply_err(a, 502, err); return true} reply(a, c) } else { c, err := postcards_50(a.me) if err != "" {reply_err(a, 502, err); return true} reply(a, c) } return true }, // The robot has the zoo save one postcard the simple way (the button's path), then 50 in one // batch, read them all back and find them in the listing. check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) r := post(b, self_url("/api/13/postcard")) one: Card if r.status != 200 || !json_into(r.body, &one) do return fail(fmt.tprintf("saving one postcard failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120))) if !strings.has_prefix(one.file.path, "Apps/Zoo/") do return fail(cat("one postcard was saved at ", one.file.path, ", not under Apps/Zoo"), one) one.image = "" if !one.matched do return fail(fmt.tprintf("one postcard, saved as file %d, came back different", one.file.id), one) b.timeout_ms = 40_000 // 50 PUTs, 50 GETs and four storage calls behind one request r = post(b, self_url("/api/13/postcards")) b.timeout_ms = 15_000 j: Cards if r.status != 200 || !json_into(r.body, &j) do return fail(fmt.tprintf("saving 50 postcards failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120))) j.one = &one if j.matched != 50 do return fail(fmt.tprintf("only %d of 50 postcards came back unchanged", j.matched), j) if j.listed != 50 do return fail(fmt.tprintf("only %d of 50 postcards are in the zoo's listing", j.listed), j) return pass(fmt.tprintf("saved one postcard at %s (file %d) and fetched it back by its id unchanged; then 50 under %s (one begin, PUTs straight to B2, one commit, one call for 50 URLs), all 50 back unchanged and listed, in %d ms", one.file.path, one.file.id, j.path, j.ms), j) }, } }