Crates.io | wnfs-unixfs-file |
lib.rs | wnfs-unixfs-file |
version | 0.2.0 |
source | src |
created_at | 2023-12-06 15:15:35.486326 |
updated_at | 2024-02-15 19:07:10.917017 |
description | IPLD UnixFS File implementation for Webnative Filesystem |
homepage | https://fission.codes |
repository | https://github.com/wnfs-wg/rs-wnfs/tree/main/wnfs-unixfs-file |
max_upload_size | |
id | 1060133 |
size | 117,909 |
This Rust crate provides an implementation of UnixFs files. WNFS uses the UnixFs file encoding purely to chunk big byte arrays into multiple blocks and produce a single CID for them to link to from WNFS structures.
This crate is a fork from beetle (previously "iroh")'s iroh-unixfs crate.
Major changes relative to that implementation include:
BlockStore
traituse wnfs_unixfs_file::builder::FileBuilder;
use wnfs_common::MemoryBlockStore;
use tokio::io::AsyncReadExt;
// Where data is stored
let store = &MemoryBlockStore::new();
// Encoding byte arrays, getting a CID
let data = vec![1u8; 1_000_000]; // 1MiB of ones
let root_cid = FileBuilder::new()
.content_bytes(data)
.build()?
.store(store)
.await?;
// Taking a CID, reading back a byte array:
let file = UnixFsFile::load(&root_cid, store).await?;
println!("filesize: {}", file.filesize());
let mut buffer = Vec::new();
let mut reader = file.into_content_reader(store, None)?;
reader.read_to_end(&mut buffer).await?;
// buffer now has 1 million ones
// You can also seek
use tokio::io::AsyncSeekExt;
use std::io::SeekFrom;
let mut reader = file.into_content_reader(store, None)?;
reader.seek(SeekFrom::Start(10_000)).await?;
let mut slice = [0u8; 10_000];
reader.read_exact(&mut slice).await?;
// slice now has 10_000 ones