// A site's uploads kept in a content-addressed store: the same picture uploaded twice takes the
// room once, names point at contents, a draft's contents swept away once nothing names them, and
// every file checked against its hash.
//
//   quillo run blob_store.qwe      then open http://localhost:8080/store and http://localhost:8080/avatar

use "blob_store.qwer"

let ROOT = "data/blobs"

route GET /store {
    let first = blob_put_file(ROOT, "blob_store_sample.png", "avatars/ada.png")?
    let again = blob_put_file(ROOT, "blob_store_sample.png", "avatars/grace.png")?
    let note = blob_put_text(ROOT, "Hello from the store", "notes/hello.txt")?
    blob_put_text(ROOT, "a draft nobody keeps", "drafts/tmp.txt")?
    blob_unlink(ROOT, "drafts/tmp.txt")?
    {
        picture: { hash: first.hash, size: first.size, type: first.type },
        same_bytes_again: again.new,
        same_hash: first.hash == again.hash,
        note: blob_get_text(ROOT, note.hash)?,
        avatars: blob_list(ROOT, "avatars/", 10) | map((n) => n.name),
        swept: blob_gc(ROOT, 0)?.removed,
        check: blob_verify(ROOT)?,
    }
}

route GET /avatar {
    let a = blob_resolve(ROOT, "avatars/ada.png")? ?? reply 404 "no avatar yet: open /store first"
    file(a.path)
}
