-
Notifications
You must be signed in to change notification settings - Fork 25
/
file_variants.rs
63 lines (54 loc) · 2.03 KB
/
file_variants.rs
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
//! This example shows how to store multiple byte arrays per file, by storing
//! additional data in a file's metadata, which links out to externally encrypted data.
use anyhow::Result;
use chrono::Utc;
use rand_chacha::ChaCha12Rng;
use rand_core::SeedableRng;
use wnfs::private::{
forest::{hamt::HamtForest, traits::PrivateForest},
PrivateFile, PrivateForestContent,
};
use wnfs_common::MemoryBlockStore;
#[async_std::main]
async fn main() -> Result<()> {
// The usual in-memory testing setup for WNFS
let store = &MemoryBlockStore::default();
let rng = &mut ChaCha12Rng::from_entropy();
let forest = &mut HamtForest::new_rsa_2048(rng);
// Create a new file (detached from any directory)
let mut file = PrivateFile::with_content_rc(
&forest.empty_name(),
Utc::now(),
b"main content".to_vec(),
forest,
store,
rng,
)
.await?;
// Create some content that's stored encrypted in the private forest.
// The PrivateForestContent struct holds the keys and pointers to look it back up.
// We use the file's name as the "path" for this content. This means anyone
// who had write access to the file will have write access to the external content.
let content = PrivateForestContent::new(
file.header.get_name(),
b"secondary content".to_vec(),
forest,
store,
rng,
)
.await?;
// We store the content in the file metadata.
// This will update the `file: Arc<PrivateFile>` for us with a new reference.
file.get_metadata_mut_rc()?
.put("thumbnail", content.as_metadata_value()?);
// We store the new reference in the forest.
file.as_node().store(forest, store, rng).await?;
// When can look up the private forest content again.
let content_ipld = file.get_metadata().get("thumbnail").unwrap();
let content = PrivateForestContent::from_metadata_value(content_ipld)?;
assert_eq!(
content.get_content(forest, store).await?,
b"secondary content".to_vec()
);
Ok(())
}