Files
marine/examples/ipfs-node/effector/src/main.rs

83 lines
2.3 KiB
Rust
Raw Normal View History

2020-05-30 01:55:39 +03:00
/*
* 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.
*/
2020-06-05 23:12:02 +03:00
mod path;
2020-05-30 01:55:39 +03:00
use crate::path::to_full_path;
2020-05-30 01:55:39 +03:00
2020-07-11 23:04:55 +03:00
use fluence::fce;
2020-12-08 18:15:18 +03:00
use fluence::WasmLoggerBuilder;
2020-07-11 23:04:55 +03:00
const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file";
2020-06-30 14:45:04 +03:00
const IPFS_ADDR_ENV_NAME: &str = "IPFS_ADDR";
const TIMEOUT_ENV_NAME: &str = "timeout";
2020-06-11 19:11:34 +03:00
pub fn main() {
2020-12-08 18:15:18 +03:00
WasmLoggerBuilder::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();
2020-06-11 19:11:34 +03:00
}
2020-07-11 23:04:55 +03:00
/// 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);
2020-06-01 02:45:04 +03:00
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);
2020-06-01 02:45:04 +03:00
2020-08-23 04:04:11 +03:00
unsafe { ipfs(cmd) }
2020-07-11 23:04:55 +03:00
}
2020-05-30 01:55:39 +03:00
2020-07-11 23:04:55 +03:00
/// 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);
2020-06-05 23:12:02 +03:00
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
);
2020-08-23 04:04:11 +03:00
unsafe { ipfs(cmd) };
2020-07-11 23:04:55 +03:00
RESULT_FILE_PATH.to_string()
2020-05-30 01:55:39 +03:00
}
2020-07-11 23:04:55 +03:00
#[fce]
pub fn get_address() -> String {
match std::env::var(IPFS_ADDR_ENV_NAME) {
2020-06-30 14:45:04 +03:00
Ok(addr) => addr,
Err(e) => format!(
"getting {} env variable failed with error {:?}",
IPFS_ADDR_ENV_NAME, e
),
2020-07-11 23:04:55 +03:00
}
}
2020-06-12 16:54:21 +03:00
2020-07-18 18:05:07 +03:00
#[fce]
2020-06-06 21:34:13 +03:00
#[link(wasm_import_module = "host")]
2020-05-30 01:55:39 +03:00
extern "C" {
2020-07-18 18:05:07 +03:00
/// Execute provided cmd as a parameters of ipfs cli, return result.
pub fn ipfs(cmd: String) -> String;
2020-05-30 01:55:39 +03:00
}