FileTransferService

Struct FileTransferService 

Source
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

Source

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:?}");
Source

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");
}
Source

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§

Source§

impl Clone for Service

Source§

fn clone(&self) -> Service

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Service

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,