mirror of
https://github.com/fluencelabs/examples
synced 2025-06-24 23:31:33 +00:00
Refactor Aqua & save file size to IPFS
This commit is contained in:
@ -13,6 +13,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
marine-rs-sdk = { version="0.6.11", features=["logger"] }
|
||||
log = "0.4.14"
|
||||
rand = "0.8.4"
|
||||
|
||||
[dev-dependencies]
|
||||
marine-rs-sdk-test = "0.1.11"
|
||||
|
@ -5,4 +5,4 @@ mkdir -p artifacts
|
||||
rm -f artifacts/*.wasm
|
||||
marine build --release
|
||||
cp target/wasm32-wasi/release/process_files.wasm artifacts/
|
||||
marine aqua artifacts/process_files.wasm >../aqua/process_files.aqua
|
||||
marine aqua artifacts/process_files.wasm >../aqua/src/process_files.aqua
|
||||
|
@ -14,7 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use marine_rs_sdk::{marine, module_manifest};
|
||||
use marine_rs_sdk::{marine, module_manifest, get_call_parameters};
|
||||
use std::path::{PathBuf, Path};
|
||||
use rand::Rng;
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
||||
module_manifest!();
|
||||
|
||||
@ -35,3 +38,32 @@ pub fn file_size(file_path: String) -> SizeResult {
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub struct WriteResult {
|
||||
pub path: String,
|
||||
pub success: bool,
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn write_file_size(size: u32) -> WriteResult {
|
||||
let name: String = rand::thread_rng()
|
||||
.sample_iter(Alphanumeric)
|
||||
.take(16)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
|
||||
let file = vault_dir().join(&name);
|
||||
let file_str = file.to_string_lossy().to_string();
|
||||
match std::fs::write(&file, size.to_string()) {
|
||||
Ok(_) => WriteResult { path: file_str, success: true, error: String::new() },
|
||||
Err(err) => WriteResult { path: String::new(), success: false, error: err.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
fn vault_dir() -> PathBuf {
|
||||
let particle_id = get_call_parameters().particle_id;
|
||||
let vault = Path::new("/tmp").join("vault").join(particle_id);
|
||||
|
||||
vault
|
||||
}
|
Reference in New Issue
Block a user