diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 152e88e..200cf25 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,10 +1,7 @@ name: Rust on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] + - push env: CARGO_TERM_COLOR: always diff --git a/Cargo.lock b/Cargo.lock index 4546cfe..aaac117 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,9 +54,9 @@ dependencies = [ [[package]] name = "ahash" -version = "0.8.10" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b79b82693f705137f8fb9b37871d99e4f9a7df12b917eed79c3d3954830a60b" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if 1.0.0", "once_cell", @@ -477,9 +477,9 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "carbonado" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2aeb8cfda5485d8a2b81e4064effa691ee5d0dd6abafb98faf8bc64f636b422" +checksum = "3b6dec3eae07040c0b6d726f19414df8fb82a746b14cd7c78f814f59d4e605c2" dependencies = [ "bao", "bech32 0.11.0", @@ -981,8 +981,9 @@ dependencies = [ [[package]] name = "flexi_syslog" -version = "0.5.2" -source = "git+https://github.com/cryptoquick/flexi_syslog.git#66b28399488e5753d8c2c2e6ebe00ca202e4cdc8" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e5a97f0d9b6b91e0b7095a77d03e1f216693d8d34c3827bd7b623b29cbcdb88" dependencies = [ "chrono", "flexi_logger", @@ -1132,9 +1133,9 @@ dependencies = [ [[package]] name = "ghash" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" dependencies = [ "opaque-debug", "polyval", @@ -2131,9 +2132,9 @@ dependencies = [ [[package]] name = "polyval" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52cff9d1d4dee5fe6d03729099f4a310a41179e0a10dbf542039873f2e826fb" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" dependencies = [ "cfg-if 1.0.0", "cpufeatures", diff --git a/Cargo.toml b/Cargo.toml index 25e77c5..5aeec87 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,13 +18,13 @@ axum-macros = "0.4.0" bao = "0.12.1" blake3 = "1.5.0" bytes = "1.5.0" -carbonado = "0.4.0" +carbonado = "0.4.1" chrono = "0.4.31" clap = { version = "4.4.11", features = ["derive"] } directories = "5.0.1" file-format = { version = "0.24.0", features = ["reader-xml"] } -flexi_logger = { version = "0.27.3", features = ["colors"] } -flexi_syslog = { version = "0.5.2", git = "https://github.com/cryptoquick/flexi_syslog.git" } +flexi_logger = { version = "0.27.4", features = ["colors"] } +flexi_syslog = "0.5.3" futures-util = "0.3.29" hex = "0.4.3" http = "1.0.0" diff --git a/src/backend/fs.rs b/src/backend/fs.rs index f4cde89..225d2bd 100755 --- a/src/backend/fs.rs +++ b/src/backend/fs.rs @@ -37,7 +37,7 @@ pub type FileStream = Pin> + Send>>; pub async fn write_file<'a>( pk: &Secp256k1PubKey, file_stream: FileStream, - name: Option, + file_name: FileName, mime_type_receiver: watch::Receiver, ) -> Result { trace!("write_file, create a shared secret using ECDH"); @@ -50,9 +50,10 @@ pub async fn write_file<'a>( ensure_pk_dirs_exist(&write_pk_str).await?; trace!("Initialize Blake3 keyed hasher"); - let file_hasher = Arc::new(Mutex::new(blake3::Hasher::new_keyed( - &x_only_pk.serialize(), - ))); + let file_hasher = Arc::new(Mutex::new(match file_name { + FileName::PubKeyed => blake3::Hasher::new_keyed(&x_only_pk.serialize()), + _ => blake3::Hasher::new(), + })); trace!("Iterate through file body stream"); let thread_file_hasher = file_hasher.clone(); @@ -112,11 +113,17 @@ pub async fn write_file<'a>( let final_mime_type_str = current_mime_type.lock().await.clone(); debug!(">>>>>>>>>> Mime_Type has Changed {final_mime_type_str:?}"); + let write_pk_str = write_pk_str.to_owned(); + let file_name = match file_name { + FileName::Named(name) => name, + FileName::PubKeyed => file_hash.to_string(), + FileName::Hashed => file_hash.to_string(), + }; + trace!("Append each hash to its catalog"); write_catalog( - &write_pk_str, - &file_hash, - name, + write_pk_str, + file_name, &final_mime_type_str, &segment_hashes, ) @@ -174,9 +181,8 @@ pub fn write_segment(sk: &[u8], pk: &[u8], encoded: &Encoded) -> Result } pub async fn write_catalog( - write_pk_str: &str, - file_hash: &Blake3Hash, - name: Option, + write_pk_str: String, + file_name: String, mime_type: &str, segment_hashes: &[BaoHash], ) -> Result<()> { @@ -186,10 +192,6 @@ pub async fn write_catalog( .flat_map(|bao_hash| bao_hash.to_bytes()) .collect(); - let write_pk_str = write_pk_str.to_owned(); - let file_hash = file_hash.to_string(); - let name = name.unwrap_or(file_hash); - let date_utc = Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(61, 0).unwrap()); let date = date_utc.to_string(); let mime_type = mime_type.to_string(); @@ -198,7 +200,7 @@ pub async fn write_catalog( // HEADER METADATA let cat_data = CborData { - name: name.to_string(), + name: file_name.to_string(), date, mime_type, }; @@ -212,7 +214,7 @@ pub async fn write_catalog( let write_pk_str = write_pk_str.clone(); let contents = contents.clone(); - let name = name.clone(); + let name = file_name.clone(); let cbor_len = length as u8; let cbor_data = cbor_data.clone(); let metadata = Some(cbor_data.clone()); diff --git a/src/frontend/http.rs b/src/frontend/http.rs index 11b2c6c..2a6ef0e 100755 --- a/src/frontend/http.rs +++ b/src/frontend/http.rs @@ -25,16 +25,13 @@ use crate::{ prelude::*, }; -async fn write_file_handler(pk: &str, body: Body, name: Option) -> Result { +async fn write_file_handler(pk: &str, body: Body, file_name: FileName) -> Result { let pk = &Secp256k1PubKey::try_from(pk)?; - let name_clone = name.clone().unwrap_or("Not Named".to_string()); - let extension = name_clone.split('.').last().unwrap_or_default(); - if extension.is_empty() { - debug!(">>>>>> NO EXTENSION FROM NAME {}", extension); - } else { - debug!(">>>>>> EXTENSION FROM NAME {}", extension); - } + // let extension = match file_name { + // FileName::Named(name) => Some(name.split('.').last().unwrap_or_default().to_owned()), + // _ => None, + // }; // Create a watch channel for MIME type updates let (mime_type_sender, mime_type_receiver) = watch::channel("init_mime_type".to_string()); @@ -87,16 +84,31 @@ async fn write_file_handler(pk: &str, body: Body, name: Option) -> Resul .boxed(); // Call write_file with the receiver part of the channel - let Blake3Hash(hash) = write_file(pk, file_stream, name, mime_type_receiver).await?; + let Blake3Hash(hash) = write_file(pk, file_stream, file_name, mime_type_receiver).await?; Ok(hash.to_hex().to_string()) } #[axum_macros::debug_handler] -async fn post_file(Path(pk): Path, body: Body) -> Result { +async fn post_file_hashed( + Path(pk): Path, + body: Body, +) -> Result { + debug!("post_file called with {pk}"); + + let hash = write_file_handler(&pk, body, FileName::Hashed).await?; + + Ok((StatusCode::OK, hash)) +} + +#[axum_macros::debug_handler] +async fn post_file_pubkeyed( + Path(pk): Path, + body: Body, +) -> Result { debug!("post_file called with {pk}"); - let hash = write_file_handler(&pk, body, None).await?; + let hash = write_file_handler(&pk, body, FileName::PubKeyed).await?; Ok((StatusCode::OK, hash)) } @@ -115,7 +127,7 @@ async fn post_file_named( return Err(AppError(StatusCode::BAD_REQUEST, anyhow!("Provided file name contains characters that have not been encoded. It should be: {reencoded}"))); } - let hash = write_file_handler(&pk, body, Some(name)).await?; + let hash = write_file_handler(&pk, body, FileName::Named(name)).await?; Ok((StatusCode::OK, hash)) } @@ -174,10 +186,11 @@ async fn key(Path(pk): Path) -> Result { pub async fn start() -> Result<()> { let app = Router::new() .route("/remove/:pk/:blake3_hash", delete(remove_file)) - .route("/store/:pk", post(post_file)) - .route("/store_named/:pk/:name", post(post_file_named)) - .route("/retrieve/:pk/:blake3_hash", get(get_file)) - .route("/retrieve_named/:pk/:name", get(get_file_named)) + .route("/hashed/:pk", post(post_file_hashed)) + .route("/pubkeyed/:pk", post(post_file_pubkeyed)) + .route("/named/:pk/:name", post(post_file_named)) + .route("/hashed/:pk/:blake3_hash", get(get_file)) + .route("/named/:pk/:name", get(get_file_named)) .route("/key/:pk", get(key)) // .route("/catalog/:blake3_hash", get(get_catalog)) // .route("/raw/:bao_hash", get(get_raw)) diff --git a/src/structs.rs b/src/structs.rs index c5ca75f..34c3e60 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -96,3 +96,10 @@ pub struct CborData { pub mime_type: String, // pub metadata: Option>, } + +pub enum FileName { + Named(String), + PubKeyed, + Hashed, + // None, +} diff --git a/tests/file.rs b/tests/file.rs index d52ffd4..f9b8f0e 100755 --- a/tests/file.rs +++ b/tests/file.rs @@ -10,7 +10,7 @@ use carbonado_node::{ backend::fs::{read_file, write_file, FileStream}, config::node_shared_secret, prelude::SEGMENT_SIZE, - structs::{Hash, Lookup}, + structs::{FileName, Hash, Lookup}, }; use futures_util::{stream, StreamExt, TryStreamExt}; @@ -58,7 +58,7 @@ async fn write_read() -> Result<()> { let blake3_hash = write_file( &Secp256k1PubKey::new(pk), file_stream, - None, + FileName::PubKeyed, mime_type_receiver, ) .await?; @@ -125,7 +125,7 @@ async fn read_write_delete_file() -> Result<()> { let file_did_write = write_file( &Secp256k1PubKey::new(pk), file_stream, - None, + FileName::PubKeyed, mime_type_receiver.clone(), ) .await @@ -150,7 +150,7 @@ async fn read_write_delete_file() -> Result<()> { let blake3_hash = write_file( &Secp256k1PubKey::new(pk), file_stream, - None, + FileName::PubKeyed, mime_type_receiver, ) .await