mirror of
https://github.com/fluencelabs/marine.git
synced 2025-06-18 17:31:25 +00:00
update examples
This commit is contained in:
13
examples/ipfs-node/effector/Cargo.toml
Normal file
13
examples/ipfs-node/effector/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "ipfs-effector"
|
||||
version = "0.1.0"
|
||||
authors = ["Fluence Labs"]
|
||||
edition = "2018"
|
||||
|
||||
[[bin]]
|
||||
name = "ipfs_node"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
|
||||
log = "0.4.11"
|
79
examples/ipfs-node/effector/src/main.rs
Normal file
79
examples/ipfs-node/effector/src/main.rs
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2020 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
mod path;
|
||||
|
||||
use crate::path::to_full_path;
|
||||
|
||||
use fluence::fce;
|
||||
use fluence::WasmLogger;
|
||||
|
||||
const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file";
|
||||
const IPFS_ADDR_ENV_NAME: &str = "IPFS_ADDR";
|
||||
const TIMEOUT_ENV_NAME: &str = "timeout";
|
||||
|
||||
pub fn main() {
|
||||
WasmLogger::init_with_level(log::Level::Info).unwrap();
|
||||
}
|
||||
|
||||
/// Put file from specified path to IPFS and return its hash.
|
||||
#[fce]
|
||||
pub fn put(file_path: String) -> String {
|
||||
log::info!("put called with file path {}", file_path);
|
||||
|
||||
let file_path = to_full_path(file_path);
|
||||
|
||||
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
|
||||
let cmd = format!("add --timeout {} -Q {}", timeout, file_path);
|
||||
|
||||
ipfs(cmd)
|
||||
}
|
||||
|
||||
/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it.
|
||||
#[fce]
|
||||
pub fn get(hash: String) -> String {
|
||||
log::info!("get called with hash {}", hash);
|
||||
|
||||
let result_file_path = to_full_path(RESULT_FILE_PATH);
|
||||
|
||||
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
|
||||
let cmd = format!(
|
||||
"get --timeout {} -o {} {}",
|
||||
timeout, result_file_path, hash
|
||||
);
|
||||
|
||||
ipfs(cmd);
|
||||
|
||||
RESULT_FILE_PATH.to_string()
|
||||
}
|
||||
|
||||
#[fce]
|
||||
pub fn get_address() -> String {
|
||||
match std::env::var(IPFS_ADDR_ENV_NAME) {
|
||||
Ok(addr) => addr,
|
||||
Err(e) => format!(
|
||||
"getting {} env variable failed with error {:?}",
|
||||
IPFS_ADDR_ENV_NAME, e
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[fce]
|
||||
#[link(wasm_import_module = "host")]
|
||||
extern "C" {
|
||||
/// Execute provided cmd as a parameters of ipfs cli, return result.
|
||||
pub fn ipfs(cmd: String) -> String;
|
||||
}
|
52
examples/ipfs-node/effector/src/path.rs
Normal file
52
examples/ipfs-node/effector/src/path.rs
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2020 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
pub(super) fn to_full_path<S>(cmd: S) -> String
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
use std::path::Path;
|
||||
use std::path::Component;
|
||||
|
||||
let cmd = cmd.into();
|
||||
let path = Path::new(&cmd);
|
||||
|
||||
let mut components = path.components();
|
||||
let is_absolute = components.next() == Some(Component::RootDir);
|
||||
|
||||
if !is_absolute {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
let parent = match components.next() {
|
||||
Some(Component::Normal(path)) => path.to_str().unwrap(),
|
||||
_ => return cmd,
|
||||
};
|
||||
|
||||
match std::env::var(parent) {
|
||||
Ok(to_dir) => {
|
||||
let mut full_path = std::path::PathBuf::from(to_dir);
|
||||
|
||||
// TODO: optimize this
|
||||
#[allow(clippy::while_let_on_iterator)]
|
||||
while let Some(component) = components.next() {
|
||||
full_path.push(component);
|
||||
}
|
||||
full_path.to_string_lossy().into_owned()
|
||||
}
|
||||
Err(_) => cmd,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user