|
1 | 1 | //! The bindgen API for PublicFile.
|
2 | 2 |
|
| 3 | +use crate::{fs::metadata::JsMetadata, value}; |
3 | 4 | use chrono::{DateTime, Utc};
|
4 |
| -use js_sys::{Error, Uint8Array}; |
5 |
| -use wasm_bindgen::prelude::wasm_bindgen; |
6 |
| -use wnfs::{ipld::Cid, public::PublicFile as WnfsPublicFile, Id}; |
| 5 | +use js_sys::{Error, Promise, Uint8Array}; |
| 6 | +use std::rc::Rc; |
| 7 | +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; |
| 8 | +use wasm_bindgen_futures::future_to_promise; |
| 9 | +use wnfs::{ipld::Cid, public::PublicFile as WnfsPublicFile, BlockStore as WnfsBlockStore, Id}; |
7 | 10 |
|
8 |
| -use crate::fs::JsResult; |
| 11 | +use crate::fs::{BlockStore, ForeignBlockStore, JsResult}; |
9 | 12 |
|
10 | 13 | /// A file in a WNFS public file system.
|
11 | 14 | #[wasm_bindgen]
|
12 |
| -pub struct PublicFile(WnfsPublicFile); |
| 15 | +pub struct PublicFile(pub(crate) Rc<WnfsPublicFile>); |
13 | 16 |
|
14 | 17 | #[wasm_bindgen]
|
15 | 18 | impl PublicFile {
|
16 | 19 | /// Creates a new file in a WNFS public file system.
|
17 | 20 | #[wasm_bindgen(constructor)]
|
18 |
| - pub fn new(time: &js_sys::Date, cid: Uint8Array) -> JsResult<PublicFile> { |
| 21 | + pub fn new(time: &js_sys::Date, cid: Vec<u8>) -> JsResult<PublicFile> { |
19 | 22 | let time = DateTime::<Utc>::from(time);
|
20 | 23 |
|
21 |
| - let cid = Cid::try_from(&cid.to_vec()[..]) |
22 |
| - .map_err(|e| Error::new(&format!("Invalid CID: {e}")))?; |
| 24 | + let cid = Cid::try_from(&cid[..]).map_err(|e| Error::new(&format!("Invalid CID: {e}")))?; |
23 | 25 |
|
24 |
| - Ok(PublicFile(WnfsPublicFile::new(time, cid))) |
| 26 | + Ok(PublicFile(Rc::new(WnfsPublicFile::new(time, cid)))) |
25 | 27 | }
|
26 | 28 |
|
27 | 29 | /// Gets a unique id for node.
|
28 | 30 | #[wasm_bindgen(js_name = "getId")]
|
29 | 31 | pub fn get_id(&self) -> String {
|
30 | 32 | self.0.get_id()
|
31 | 33 | }
|
| 34 | + |
| 35 | + /// Stores a file in provided block store. |
| 36 | + pub fn store(&self, store: BlockStore) -> JsResult<Promise> { |
| 37 | + let file = Rc::clone(&self.0); |
| 38 | + let mut store = ForeignBlockStore(store); |
| 39 | + |
| 40 | + Ok(future_to_promise(async move { |
| 41 | + let cid = file |
| 42 | + .store(&mut store) |
| 43 | + .await |
| 44 | + .map_err(|e| Error::new(&format!("Cannot add to store: {e}")))?; |
| 45 | + |
| 46 | + let cid_u8array = Uint8Array::from(&cid.to_bytes()[..]); |
| 47 | + |
| 48 | + Ok(value!(cid_u8array)) |
| 49 | + })) |
| 50 | + } |
| 51 | + |
| 52 | + /// Loads a file given its CID from the block store. |
| 53 | + pub fn load(cid: Vec<u8>, store: BlockStore) -> JsResult<Promise> { |
| 54 | + let store = ForeignBlockStore(store); |
| 55 | + let cid = Cid::try_from(cid).map_err(|e| Error::new(&format!("Cannot parse cid: {e}")))?; |
| 56 | + Ok(future_to_promise(async move { |
| 57 | + let file: WnfsPublicFile = store |
| 58 | + .get_deserializable(&cid) |
| 59 | + .await |
| 60 | + .map_err(|e| Error::new(&format!("Couldn't deserialize directory: {e}")))?; |
| 61 | + |
| 62 | + Ok(value!(PublicFile(Rc::new(file)))) |
| 63 | + })) |
| 64 | + } |
| 65 | + |
| 66 | + /// Gets the previous cid of the file or null if the file doesn't have a previous cid. |
| 67 | + #[wasm_bindgen(js_name = "previousCid")] |
| 68 | + pub fn previous_cid(&self) -> JsValue { |
| 69 | + match self.0.get_previous() { |
| 70 | + Some(cid) => { |
| 71 | + let cid_u8array = Uint8Array::from(&cid.to_bytes()[..]); |
| 72 | + value!(cid_u8array) |
| 73 | + } |
| 74 | + None => JsValue::NULL, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Gets the metadata of this file. |
| 79 | + pub fn metadata(&self) -> JsResult<JsValue> { |
| 80 | + JsMetadata(self.0.get_metadata()).try_into() |
| 81 | + } |
| 82 | + |
| 83 | + /// Gets the content cid of the file. |
| 84 | + #[wasm_bindgen(js_name = "contentCid")] |
| 85 | + pub fn content_cid(&self) -> Vec<u8> { |
| 86 | + self.0.get_content_cid().to_bytes() |
| 87 | + } |
32 | 88 | }
|
0 commit comments