Skip to content

Commit

Permalink
fix(api): Fix maidsafe#2434 and make dir_upload() methods more granular
Browse files Browse the repository at this point in the history
  • Loading branch information
happybeing committed Nov 18, 2024
1 parent 69694c7 commit fa9056e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion autonomi-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> Result<(
let local_addr;
let archive = if public {
let xor_name = client
.dir_upload(dir_path, &wallet)
.dir_and_archive_upload(dir_path, &wallet)
.await
.wrap_err("Failed to upload file")?;
local_addr = addr_to_str(xor_name);
Expand Down
31 changes: 25 additions & 6 deletions autonomi/src/client/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,24 @@ impl Client {

/// Upload a directory to the network. The directory is recursively walked.
/// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns ArchiveAddr (pointing to the archive)
pub async fn dir_upload(
pub async fn dir_and_archive_upload(
&self,
dir_path: PathBuf,
wallet: &EvmWallet,
) -> Result<ArchiveAddr, UploadError> {
match self.dir_upload(dir_path.clone(), wallet).await {
Ok(archive) => Ok(self.archive_upload(&archive, wallet).await?),
Err(e) => Err(e),
}
}

/// Upload a directory to the network. The directory is recursively walked.
/// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns Archive but does not upload that to the network.
pub async fn dir_upload(
&self,
dir_path: PathBuf,
wallet: &EvmWallet,
) -> Result<Archive, UploadError> {
info!("Uploading directory: {dir_path:?}");
let start = tokio::time::Instant::now();

Expand Down Expand Up @@ -152,14 +165,20 @@ impl Client {
}
}

// upload archive
let archive_serialized = archive.into_bytes()?;
let arch_addr = self.data_put(archive_serialized, wallet).await?;

info!("Complete archive upload completed in {:?}", start.elapsed());
#[cfg(feature = "loud")]
println!("Upload completed in {:?}", start.elapsed());
Ok(arch_addr)
Ok(archive)
}

/// Upload a directory Archive to the network
pub async fn archive_upload(
&self,
archive: &Archive,
wallet: &EvmWallet,
) -> Result<ArchiveAddr, UploadError> {
let archive_serialized = archive.into_bytes()?;
Ok(self.data_put(archive_serialized, wallet).await?)
}

/// Upload a file to the network.
Expand Down
4 changes: 2 additions & 2 deletions autonomi/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn dir_upload_download() -> Result<()> {
let wallet = get_funded_wallet();

let addr = client
.dir_upload("tests/file/test_dir".into(), &wallet)
.dir_and_archive_upload("tests/file/test_dir".into(), &wallet)
.await?;

sleep(Duration::from_secs(10)).await;
Expand Down Expand Up @@ -86,7 +86,7 @@ async fn file_into_vault() -> Result<()> {
let client_sk = bls::SecretKey::random();

let addr = client
.dir_upload("tests/file/test_dir".into(), &wallet)
.dir_and_archive_upload("tests/file/test_dir".into(), &wallet)
.await?;
sleep(Duration::from_secs(2)).await;

Expand Down

0 comments on commit fa9056e

Please sign in to comment.