Skip to content

Commit

Permalink
feat: add more cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 6, 2024
1 parent 4aea9b8 commit a78b73d
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `ic-oss`
# `IC-OSS`

🗂 A decentralized Object Storage Service on the Internet Computer.

Expand Down
2 changes: 1 addition & 1 deletion src/ic_oss_bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dfx deploy ic_oss_bucket --argument "(opt variant {Init =
max_children = 1000;
visibility = 0;
max_custom_data_size = 4096;
enable_hash_index = false;
enable_hash_index = true;
}
})"

Expand Down
2 changes: 1 addition & 1 deletion src/ic_oss_bucket/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn get_file_chunks(
Err("permission denied".to_string())?;
}

Ok(store::fs::get_chunks(id, index, take.unwrap_or(10).min(8)))
Ok(store::fs::get_chunks(id, index, take.unwrap_or(8).min(8)))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ic_oss_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true

[dependencies]
bytes = { workspace = true }
candid = { workspace = true }
candid = { workspace = true, features = ["value", "printer"] }
serde = { workspace = true }
serde_bytes = { workspace = true }
tokio = { workspace = true }
Expand All @@ -21,6 +21,7 @@ futures = { workspace = true }
futures-util = { workspace = true }
crc32fast = { workspace = true }
sha3 = { workspace = true }
hex = { workspace = true }
ic-oss = { path = "../ic_oss", version = "0.7" }
ic-oss-types = { path = "../ic_oss_types", version = "0.7" }
ic-agent = "0.36"
Expand Down
33 changes: 31 additions & 2 deletions src/ic_oss_cli/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use chrono::prelude::*;
use ic_oss_types::{file::*, format_error};
use ic_oss_types::{file::*, format_error, ByteN};
use sha3::{Digest, Sha3_256};
use tokio::io::AsyncReadExt;
use tokio::{time, time::Duration};

pub async fn upload_file(
cli: &ic_oss::bucket::Client,
enable_hash_index: bool,
parent: u32,
file: &str,
retry: u8,
) -> Result<(), String> {
let start_ts: DateTime<Local> = Local::now();
let file_path = std::path::Path::new(file);
let metadata = std::fs::metadata(file_path).map_err(format_error)?;
if !metadata.is_file() {
Expand All @@ -25,12 +28,25 @@ pub async fn upload_file(
mime_db::lookup(file).unwrap_or("application/octet-stream")
};

let hash: Option<ByteN<32>> = if enable_hash_index {
let fs = tokio::fs::File::open(&file_path)
.await
.map_err(format_error)?;
Some(pre_sum_hash(fs).await?.into())
} else {
None
};

let start_ts: DateTime<Local> = Local::now();
let input = CreateFileInput {
parent,
name: file_path.file_name().unwrap().to_string_lossy().to_string(),
content_type: content_type.to_string(),
size: Some(file_size),
hash,
..Default::default()
};

let fs = tokio::fs::File::open(&file_path)
.await
.map_err(format_error)?;
Expand Down Expand Up @@ -94,3 +110,16 @@ pub async fn upload_file(
);
Ok(())
}

async fn pre_sum_hash(mut fs: tokio::fs::File) -> Result<[u8; 32], String> {
let mut hasher = Sha3_256::new();
let mut buf = vec![0u8; 1024 * 1024 * 2];
loop {
let n = fs.read(&mut buf).await.map_err(format_error)?;
if n == 0 {
break;
}
hasher.update(&buf[..n]);
}
Ok(hasher.finalize().into())
}
Loading

0 comments on commit a78b73d

Please sign in to comment.