pub struct FileTransferService { /* private fields */ }Expand description
A handle to the file transfer service.
Exposes methods to interact with the file transfer service, like for publishing and getting files.
§Example
use std::path::Path;
use hyveos_sdk::Connection;
let shared_dir = std::env::var(hyveos_core::BRIDGE_SHARED_DIR_ENV_VAR).unwrap();
let file_path = Path::new(&shared_dir).join("example.txt");
tokio::fs::write(&file_path, "Hello, world!").await.unwrap();
let connection = Connection::new().await.unwrap();
let mut file_transfer_service = connection.file_transfer();
let cid = file_transfer_service.publish_file(&file_path).await.unwrap();
println!("Content ID: {cid:?}");Implementations§
Source§impl Service
impl Service
Sourcepub async fn publish_file(&mut self, path: impl AsRef<Path>) -> Result<Cid>
pub async fn publish_file(&mut self, path: impl AsRef<Path>) -> Result<Cid>
Publishes a file in the mesh network and returns its content ID.
Before it’s published, the file is copied to the shared directory if it is not already
there. By default, the shared directory is defined by the HYVEOS_BRIDGE_SHARED_DIR
environment variable (hyveos_core::BRIDGE_SHARED_DIR_ENV_VAR). However, it can be
set to a custom path when using a custom connection (ConnectionBuilder::custom).
§Errors
Returns an error if the RPC call fails.
§Example
use std::path::Path;
use hyveos_sdk::Connection;
let shared_dir = std::env::var(hyveos_core::BRIDGE_SHARED_DIR_ENV_VAR).unwrap();
let file_path = Path::new(&shared_dir).join("example.txt");
tokio::fs::write(&file_path, "Hello, world!").await.unwrap();
let connection = Connection::new().await.unwrap();
let mut file_transfer_service = connection.file_transfer();
let cid = file_transfer_service.publish_file(&file_path).await.unwrap();
println!("Content ID: {cid:?}");Sourcepub async fn get_file(&mut self, cid: Cid) -> Result<PathBuf>
pub async fn get_file(&mut self, cid: Cid) -> Result<PathBuf>
Retrieves a file from the mesh network and returns its path.
When the local runtime doesn’t own a copy of this file yet, it downloads it from one of its peers.
Afterwards, or if it was already locally available, the file is copied
into the shared directory, which is defined by the HYVEOS_BRIDGE_SHARED_DIR environment
variable.
§Errors
Returns an error if the RPC call fails.
§Example
use hyveos_sdk::Connection;
let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let cid = dht_service.get_record_json("file", "example").await.unwrap();
if let Some(cid) = cid {
let mut file_transfer_service = connection.file_transfer();
let path = file_transfer_service.get_file(cid).await.unwrap();
println!("File path: {}", path.display());
let contents = tokio::fs::read_to_string(path).await.unwrap();
println!("File length: {}", contents.len());
} else {
println!("File not found");
}Sourcepub async fn get_file_with_progress(
&mut self,
cid: Cid,
) -> Result<impl Stream<Item = Result<DownloadEvent>>>
pub async fn get_file_with_progress( &mut self, cid: Cid, ) -> Result<impl Stream<Item = Result<DownloadEvent>>>
Retrieves a file from the mesh network and returns a stream of download events.
When the local runtime doesn’t own a copy of this file yet, it downloads it from one of its peers.
While downloading, it emits events with the download progress as a percentage.
Afterwards, or if it was already locally available, the file is copied
into the shared directory, which is defined by the HYVEOS_BRIDGE_SHARED_DIR environment
variable, and the path is emitted as the last event in the stream.
§Errors
Returns an error if the RPC call fails.
§Example
use futures::StreamExt as _;
use hyveos_sdk::{services::file_transfer::DownloadEvent, Connection};
use indicatif::ProgressBar;
let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let cid = dht_service.get_record_json("file", "example").await.unwrap();
if let Some(cid) = cid {
let mut file_transfer_service = connection.file_transfer();
let mut stream = file_transfer_service
.get_file_with_progress(cid)
.await
.unwrap();
let progress_bar = ProgressBar::new(100);
let path = loop {
match stream.next().await.unwrap().unwrap() {
DownloadEvent::Progress(progress) => {
progress_bar.set_position(progress);
}
DownloadEvent::Ready(path) => {
progress_bar.finish();
break path;
}
}
};
println!("File path: {}", path.display());
let contents = tokio::fs::read_to_string(path).await.unwrap();
println!("File length: {}", contents.len());
} else {
println!("File not found");
}Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Service
impl !RefUnwindSafe for Service
impl Send for Service
impl Sync for Service
impl Unpin for Service
impl !UnwindSafe for Service
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request