Skip to content

Commit

Permalink
Merge pull request #16 from functionland/readstream
Browse files Browse the repository at this point in the history
added support for readstream from fula and write stream to local file
  • Loading branch information
ehsan6sha authored Dec 19, 2022
2 parents 4193878 + 72c3d21 commit d100517
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 13 deletions.
7 changes: 7 additions & 0 deletions appmock/src/androidTest/java/land/fx/app/WNFSTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class WNFSTest {
assert(readcontent contentEquals "Hello, World!".toByteArray())
Log.d("AppMock", "readFileFromPathOfReadTo. content="+String(readcontent))

val contentstreamfrompathtopath: String = readFilestreamToPath(client, config.cid, config.private_ref, "root/testfrompath.txt", pathString+"/teststream.txt")
Log.d("AppMock", "contentstreamfrompathtopath="+contentstreamfrompathtopath)
assertNotNull("contentstreamfrompathtopath should not be null", contentstreamfrompathtopath)
val readcontentstream: ByteArray = File(contentstreamfrompathtopath).readBytes()
assert(readcontentstream contentEquals "Hello, World!".toByteArray())
Log.d("AppMock", "readFileFromPathOfReadstreamTo. content="+String(readcontentstream))

config = rm(client, config.cid, config.private_ref, "root/testfrompath.txt")
val content2 = readFile(client, config.cid, config.private_ref, "root/testfrompath.txt")
Log.d("AppMock", "rm. content="+String(content2))
Expand Down
3 changes: 2 additions & 1 deletion dep/wnfsutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ serde_json = "1.0.89"
anyhow = "1.0.66"
async-trait = "0.1.58"
log = "0.4.14"
sha3 = "0.10"
sha3 = "0.10"
futures = "0.3"
63 changes: 63 additions & 0 deletions dep/wnfsutils/src/private_forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use wnfs::{
use anyhow::Result;
use log::{trace, Level};
use sha3::Sha3_256;
use futures::{Stream, StreamExt};


use crate::blockstore::FFIFriendlyBlockStore;
Expand Down Expand Up @@ -347,6 +348,62 @@ impl<'a> PrivateDirectoryHelper<'a> {
}
}

pub async fn read_filestream_to_path(&mut self, local_filename: &String, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], index: usize) -> Result<bool, String> {
//let mut stream_content: Vec<u8> = vec![];
let local_file = File::create(local_filename);
if local_file.is_ok() {
let mut local_file_handler = local_file.ok().unwrap();

let private_node_result = root_dir.get_node(
path_segments
, true
, forest
, &mut self.store
).await;
if private_node_result.is_ok() {
let PrivateOpResult {result, forest, ..} = private_node_result.ok().unwrap();
if result.is_some() {
let private_node = result.unwrap();
let is_file = private_node.is_file();
if is_file {
let file_res = private_node.as_file();
if file_res.is_ok() {
let file = file_res.ok().unwrap();
let mut stream = file.stream_content(
index
, &forest
, &mut self.store
);
while let Some(block) = stream.next().await {
let write_result = local_file_handler.write_all(&block.unwrap());
if write_result.is_err() {
trace!("wnfsError occured in read_filestream_to_path on write_result: {:?}", write_result.as_ref().err().unwrap().to_string());
}
//stream_content.extend_from_slice(&block.unwrap());
}
Ok(true)
} else {
trace!("wnfsError occured in read_filestream_to_path on file_res: {:?}", file_res.as_ref().err().unwrap().to_string());
Err(file_res.err().unwrap().to_string())
}
} else {
trace!("wnfsError occured in read_filestream_to_path on is_file");
Err("wnfsError occured in read_filestream_to_path on is_file".to_string())
}
} else {
trace!("wnfsError occured in read_filestream_to_path on result");
Err("wnfsError occured in read_filestream_to_path on result".to_string())
}
} else {
trace!("wnfsError occured in read_filestream_to_path on private_node_result: {:?}", private_node_result.as_ref().err().unwrap().to_string());
Err(private_node_result.err().unwrap().to_string())
}
} else {
trace!("wnfsError occured in read_filestream_to_path on local_file {:?}", local_file.as_ref().err().unwrap().to_string());
Err(local_file.err().unwrap().to_string())
}
}

pub async fn read_file_to_path(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], filename: &String) -> Result<String, String> {
let file_content_res = self.read_file(forest, root_dir, path_segments).await;
if file_content_res.is_ok() {
Expand Down Expand Up @@ -502,6 +559,12 @@ impl<'a> PrivateDirectoryHelper<'a> {
return runtime.block_on(self.read_file(forest, root_dir, path_segments));
}

pub fn synced_read_filestream_to_path(&mut self, local_filename: &String, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String], index: usize) -> Result<bool, String> {
let runtime =
tokio::runtime::Runtime::new().expect("Unable to create a runtime");
return runtime.block_on(self.read_filestream_to_path(local_filename, forest, root_dir, path_segments, index));
}

pub fn synced_mkdir(&mut self, forest: Rc<PrivateForest>, root_dir: Rc<PrivateDirectory>, path_segments: &[String]) -> Result<(Cid, PrivateRef), String>
{
let runtime =
Expand Down
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ before_install:
- git lfs pull
install:
- FILE="-Dfile=lib/build/outputs/aar/lib-release.aar"
- mvn install:install-file $FILE -DgroupId=com.group.module -DartifactId=wnfs-android -Dversion=1.3.9 -Dpackaging=aar -DgeneratePom=true
- mvn install:install-file $FILE -DgroupId=com.group.module -DartifactId=wnfs-android -Dversion=1.4.0 -Dpackaging=aar -DgeneratePom=true
17 changes: 17 additions & 0 deletions lib/src/main/java/land/fx/wnfslib/Fs.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public final class Fs {

private static native String readFileToPathNative(Datastore datastore, String cid, String privateRef, String path, String filename);

private static native String readFilestreamToPathNative(Datastore datastore, String cid, String privateRef, String path, String filename);

private static native byte[] readFileNative(Datastore datastore, String cid, String privateRef, String path);

@NonNull
Expand Down Expand Up @@ -185,6 +187,21 @@ public static String readFileToPath(Datastore datastore, String cid, String priv
}
}

@NonNull
public static String readFilestreamToPath(Datastore datastore, String cid, String privateRef, String path, String filename) throws Exception {
try{
String res = readFilestreamToPathNative(datastore, cid, privateRef, path, filename);
if(res != null && !res.isEmpty()) {
return res;
} else {
throw new Exception("An Error Occured in Fs.readFilestreamToPathNative Search logs for wnfsError to find out more.");
}
}
catch(Exception e) {
throw new Exception(e.getMessage());
}
}

public static byte[] readFile(Datastore datastore, String cid, String privateRef, String path) {
return readFileNative(datastore, cid, privateRef, path);
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.group.module</groupId>
<artifactId>wnfs-android</artifactId>
<version>1.3.9</version>
<version>1.4.0</version>
</project>
65 changes: 55 additions & 10 deletions wnfslib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,51 @@ pub mod android {

}

#[no_mangle]
pub extern "C" fn Java_land_fx_wnfslib_Fs_readFilestreamToPathNative(
env: JNIEnv,
_: JClass,
jni_fula_client: JObject,
jni_cid: JString,
jni_private_ref: JString,
jni_path_segments: JString,
jni_filename: JString,
) -> jstring {
trace!("wnfs11 **********************readFilestreamToPathNative started**************");
let store = JNIStore::new(env, jni_fula_client);
let block_store = FFIFriendlyBlockStore::new(Box::new(store));
let helper = &mut PrivateDirectoryHelper::new(block_store);

let cid = deserialize_cid(env, jni_cid);
let private_ref = deserialize_private_ref(env, jni_private_ref);

let forest = helper.synced_load_forest(cid).unwrap();
let root_dir = helper
.synced_get_root_dir(forest.to_owned(), private_ref)
.unwrap();
let path_segments = prepare_path_segments(env, jni_path_segments);
let filename: String = env
.get_string(jni_filename)
.expect("Failed to parse input path segments")
.into();
trace!("wnfs11 **********************readFilestreamToPathNative filename created**************");
let result = helper.synced_read_filestream_to_path(&filename, forest.to_owned(), root_dir, &path_segments, 0);
trace!("wnfs11 **********************readFilestreamToPathNative finished**************");
if result.is_ok() {
let res = result.ok().unwrap();
env
.new_string(filename)
.expect("Failed to serialize result")
.into_inner()
} else {
trace!("wnfsError occured in Java_land_fx_wnfslib_Fs_readFilestreamToPathNative on result: {:?}", result.err().unwrap());
env
.new_string("".to_string())
.expect("Failed to serialize result")
.into_inner()
}
}

#[no_mangle]
pub extern "C" fn Java_land_fx_wnfslib_Fs_readFileToPathNative(
env: JNIEnv,
Expand Down Expand Up @@ -370,10 +415,10 @@ pub mod android {
trace!("**********************readFileNative finished**************");
let result = helper.synced_read_file(forest.to_owned(), root_dir, &path_segments);
if result.is_err() {
let emptyVec: Vec<u8> = Vec::new();
let empty_vec: Vec<u8> = Vec::new();
return vec_to_jbyte_array(
env,
emptyVec,
empty_vec,
);
}
vec_to_jbyte_array(
Expand Down Expand Up @@ -507,34 +552,34 @@ pub mod android {
);
} else {
trace!("wnfsError occured in Java_land_fx_wnfslib_Fs_lsNative output: {:?}", output.err().unwrap().to_string());
let emptyBytes: Vec<u8> = vec![0];
let empty_bytes: Vec<u8> = vec![0];
return vec_to_jbyte_array(
env,
emptyBytes
empty_bytes
);
}
} else {
trace!("wnfsError occured in Java_land_fx_wnfslib_Fs_lsNative ls_res: {:?}", ls_res.err().unwrap().to_string());
let emptyBytes: Vec<u8> = vec![0];
let empty_bytes: Vec<u8> = vec![0];
return vec_to_jbyte_array(
env,
emptyBytes
empty_bytes
);
}
} else {
trace!("wnfsError occured in Java_land_fx_wnfslib_Fs_lsNative root_dir_res: {:?}", root_dir_res.err().unwrap().to_string());
let emptyBytes: Vec<u8> = vec![0];
let empty_bytes: Vec<u8> = vec![0];
return vec_to_jbyte_array(
env,
emptyBytes
empty_bytes
);
}
} else {
trace!("wnfsError occured in Java_land_fx_wnfslib_Fs_lsNative forest_res: {:?}", forest_res.err().unwrap().to_string());
let emptyBytes: Vec<u8> = vec![0];
let empty_bytes: Vec<u8> = vec![0];
return vec_to_jbyte_array(
env,
emptyBytes
empty_bytes
);
}
}
Expand Down

0 comments on commit d100517

Please sign in to comment.