Skip to content

Commit

Permalink
feat: add get_raw_content_cid (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset authored Dec 18, 2023
1 parent 0964ac4 commit db4295e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions wnfs-wasm/src/fs/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ impl PublicFile {
JsMetadata(self.0.get_metadata()).try_into()
}

/// Gets the content cid of the file.
#[wasm_bindgen(js_name = "getRawContentCid")]
pub fn get_raw_content_cid(&self, store: BlockStore) -> JsResult<Promise> {
let mut file = Rc::clone(&self.0);
let store = ForeignBlockStore(store);

Ok(future_to_promise(async move {
let content_cid: Cid = file.get_raw_content_cid(&store).await;
Ok(value!(Uint8Array::from(&content_cid.to_bytes()[..])))
}))
}

/// Gets the content of the file at given offset & with an optional byte limit.
#[wasm_bindgen(js_name = "readAt")]
pub fn read_at(
Expand Down
22 changes: 22 additions & 0 deletions wnfs-wasm/tests/public.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,26 @@ test.describe("PublicDirectory", () => {

expect(result).toEqual(longString);
});

test("A PublicFile has a content CID", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
wnfs: { PublicFile },
mock: { CID, MemoryBlockStore },
} = await window.setup();

const store = new MemoryBlockStore();
const time = new Date();
const file = new PublicFile(time);

const content = new TextEncoder().encode("hello");
const file2 = await file.setContent(time, content, store);

const cid_bytes = await file2.getRawContentCid(store);
return cid_bytes ? CID.decode(cid_bytes).toV1().toString() : undefined;
});

expect(result).not.toBeUndefined();
expect(result).toEqual("bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq");
});
});
6 changes: 6 additions & 0 deletions wnfs/src/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ impl PublicFile {
Ok(())
}

/// Gets the content cid of the file.
pub async fn get_raw_content_cid(&self, store: &impl BlockStore) -> Cid {
let content_cid: Result<Cid> = self.userland.resolve_cid(store).await;
content_cid.unwrap()
}

/// Gets the previous value of the file.
///
/// # Examples
Expand Down

0 comments on commit db4295e

Please sign in to comment.