Skip to content

Commit

Permalink
syntax error fs.rs closure
Browse files Browse the repository at this point in the history
  • Loading branch information
rustchain64 committed Oct 25, 2023
1 parent 655b768 commit 947f8b0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
39 changes: 34 additions & 5 deletions src/backend/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use anyhow::{anyhow, Error, Result};
use axum::body::Bytes;
use axum::{body::Bytes, extract::BodyStream};

Check failure on line 10 in src/backend/fs.rs

View workflow job for this annotation

GitHub Actions / lint

error: unused import: `extract::BodyStream` --> src/backend/fs.rs:10:25 | 10 | use axum::{body::Bytes, extract::BodyStream}; | ^^^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings`
use bytes::BytesMut;
use carbonado::{constants::Format, file::Header, structs::Encoded};
use chrono::{NaiveDateTime, TimeZone, Utc};
Expand Down Expand Up @@ -49,13 +49,24 @@ pub async fn write_file<'a>(
&x_only_pk.serialize(),
)));

//let is_mime_type = Arc::new(Mutex::new("mim_type_is?"));

trace!("Iterate through file body stream");
let thread_file_hasher = file_hasher.clone();

let segment_hashes: Vec<BaoHash> = file_stream
.try_par_then(None, move |segment: Bytes| {
// let is_mime_type = is_mime_type.clone();

let segment_hashes: Vec<(BaoHash, String)> = file_stream
.try_par_then(None, move |(segment, (_, _, mime_type))| {

Check failure on line 60 in src/backend/fs.rs

View workflow job for this annotation

GitHub Actions / lint

error[E0308]: mismatched types --> src/backend/fs.rs:60:35 | 60 | .try_par_then(None, move |(segment, (_, _, mime_type))| { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | expected `Bytes`, found `(_, _)` | expected due to this | = note: expected struct `bytes::Bytes` found tuple `(_, _)`

Check failure on line 60 in src/backend/fs.rs

View workflow job for this annotation

GitHub Actions / lint

error[E0308]: mismatched types --> src/backend/fs.rs:60:35 | 60 | .try_par_then(None, move |(segment, (_, _, mime_type))| { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | expected `Bytes`, found `(_, _)` | expected due to this | = note: expected struct `bytes::Bytes` found tuple `(_, _)`
// If you need to pass additional information, create a new struct or tuple
//let additional_data = (body_stream.clone(), remainder.clone(), is_mime_type.clone());
let segment = segment.clone();
let mime_type = mime_type.clone();

trace!("Process segment");
let thread_file_hasher = thread_file_hasher.clone();
//let is_mime_type = is_mime_type.clone();

async move {
thread_file_hasher.lock().await.update(&segment);
trace!("Encoding segment");
Expand All @@ -64,12 +75,30 @@ pub async fn write_file<'a>(
let segment_hash = write_segment(&ss, &pk_bytes, &encoded_segment)?;
trace!("Segment hash: {segment_hash}");

Ok::<BaoHash, Error>(segment_hash)
// Extract the mime_type from the result here
// let mime_type = match result {
// Ok(Some((_, (_, _, mime_type)))) => mime_type,
// _ => "application/octet-stream".to_string(),
// };

Ok::<(BaoHash, String), Error>((segment_hash, mime_type))
}
})
.try_collect()
.await?;

// Extract mime_type values from segment_hashes
// let mime_types: Vec<String> = segment_hashes
// .iter()
// .map(|(_, mime_type)| mime_type.clone())
// .collect();

// Convert the reference to a slice of tuples into a reference to a slice of BaoHash
let seg_hashes: &[BaoHash] = &segment_hashes

Check failure on line 97 in src/backend/fs.rs

View workflow job for this annotation

GitHub Actions / lint

error[E0308]: mismatched types --> src/backend/fs.rs:97:34 | 97 | let seg_hashes: &[BaoHash] = &segment_hashes | _____________________----------___^ | | | | | expected due to this 98 | | .iter() 99 | | .map(|(bao_hash, _)| bao_hash) 100 | | .collect::<Vec<&BaoHash>>()[..]; | |_______________________________________^ expected `&[BaoHash]`, found `&[&BaoHash]` | = note: expected reference `&[structs::BaoHash]` found reference `&[&structs::BaoHash]`
.iter()
.map(|(bao_hash, _)| bao_hash)
.collect::<Vec<&BaoHash>>()[..];

Check failure on line 100 in src/backend/fs.rs

View workflow job for this annotation

GitHub Actions / lint

error[E0308]: mismatched types --> src/backend/fs.rs:97:34 | 97 | let seg_hashes: &[BaoHash] = &segment_hashes | _____________________----------___^ | | | | | expected due to this 98 | | .iter() 99 | | .map(|(bao_hash, _)| bao_hash) 100 | | .collect::<Vec<&BaoHash>>()[..]; | |_______________________________________^ expected `&[BaoHash]`, found `&[&BaoHash]` | = note: expected reference `&[structs::BaoHash]` found reference `&[&structs::BaoHash]`

let file_hash: Blake3Hash = Blake3Hash(file_hasher.lock().await.finalize());

trace!("Check if catalog already exists");
Expand All @@ -82,7 +111,7 @@ pub async fn write_file<'a>(
}

trace!("Append each hash to its catalog");
write_catalog(&write_pk_str, &file_hash, name, mime_type, &segment_hashes).await?;
write_catalog(&write_pk_str, &file_hash, name, "mime_type", seg_hashes).await?;

debug!("Finished write_file");
Ok(file_hash)
Expand Down
26 changes: 12 additions & 14 deletions src/frontend/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ use crate::{
prelude::*,
};

async fn write_file_handler(pk: &str, body: BodyStream, name: Option<String>) -> Result<String> {
pub async fn write_file_handler(
pk: &str,
body: BodyStream,
name: Option<String>,
) -> Result<String> {
let pk = &Secp256k1PubKey::try_from(pk)?;

let is_mime_type = "default_mime_type".to_string();

let file_stream: FileStream = stream::try_unfold(
(
body,
BytesMut::with_capacity(SEGMENT_SIZE * 2),
is_mime_type.clone(),
"default_mime_type".to_string(),
),
|(mut body_stream, mut remainder, mut is_mime_type)| async move {

Check failure on line 40 in src/frontend/http.rs

View workflow job for this annotation

GitHub Actions / lint

error: unused variable: `is_mime_type` --> src/frontend/http.rs:40:47 | 40 | |(mut body_stream, mut remainder, mut is_mime_type)| async move { | ^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_is_mime_type` | = note: `-D unused-variables` implied by `-D warnings`

Check failure on line 40 in src/frontend/http.rs

View workflow job for this annotation

GitHub Actions / lint

error: variable does not need to be mutable --> src/frontend/http.rs:40:43 | 40 | |(mut body_stream, mut remainder, mut is_mime_type)| async move { | ----^^^^^^^^^^^^ | | | help: remove this `mut` | = note: `-D unused-mut` implied by `-D warnings`
while remainder.len() < SEGMENT_SIZE {
Expand All @@ -43,17 +45,13 @@ async fn write_file_handler(pk: &str, body: BodyStream, name: Option<String>) ->
let mut buffer = Vec::new();
buffer.extend_from_slice(&bytes);

// is_mime_type = "test_mime_type".to_string();

let kind = infer::get(&buffer); // Use the extended buffer here
match kind {
Some(mime_type) => {
is_mime_type = mime_type.mime_type().to_string();
}
None => debug!("no mime_type found"),
let mime_type = match kind {
Some(mime_type) => mime_type.mime_type().to_string(),
None => "application/octet-stream".to_string(),
};

debug!(">>>>>>>>>>>> PASS TO TUPLE MIME_TYPE {}", &is_mime_type);
debug!(">>>>>>>>>>>> PASS TO TUPLE MIME_TYPE {}", &mime_type);

remainder.extend(bytes);

Expand All @@ -62,7 +60,7 @@ async fn write_file_handler(pk: &str, body: BodyStream, name: Option<String>) ->
trace!("Stream 1MB segment");
return Ok(Some((
segment.freeze(),
(body_stream, remainder, is_mime_type),
(body_stream, remainder, mime_type.clone()), // Pass mime_type here
)));
}
} else {
Expand All @@ -76,7 +74,7 @@ async fn write_file_handler(pk: &str, body: BodyStream, name: Option<String>) ->
)
.boxed();

let Blake3Hash(hash) = write_file(pk, file_stream, name, &is_mime_type).await?;
let Blake3Hash(hash) = write_file(pk, file_stream, name).await?;

Ok(hash.to_hex().to_string())
}
Expand Down
27 changes: 27 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{
str::FromStr,
};

use axum::extract::BodyStream;

Check failure on line 6 in src/structs.rs

View workflow job for this annotation

GitHub Actions / lint

error: unused import: `axum::extract::BodyStream` --> src/structs.rs:6:5 | 6 | use axum::extract::BodyStream; | ^^^^^^^^^^^^^^^^^^^^^^^^^
use bytes::BytesMut;

Check failure on line 7 in src/structs.rs

View workflow job for this annotation

GitHub Actions / lint

error: unused import: `bytes::BytesMut` --> src/structs.rs:7:5 | 7 | use bytes::BytesMut; | ^^^^^^^^^^^^^^^
use serde::{Deserialize, Serialize};

use anyhow::{Error, Result};
Expand All @@ -18,6 +20,31 @@ impl fmt::Display for Blake3Hash {
}
}

// ub struct BaoHashExt {
// pub hash: bao::Hash,
// pub other_data: (BodyStream, BytesMut, String), // Tuple containing body, BytesMut, and mime_type
// }

// impl BaoHashExt {
// // Extract the bao::Hash
// pub fn get_hash(&self) -> &bao::Hash {
// &self.hash
// }

// // Extract the String field from the tuple
// pub fn get_mime_type(&self) -> &str {
// &self.other_data.2
// }
// }

// impl fmt::Display for BaoHashExt {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// let Self(hash) = self;

// f.write_str(&hash.to_string())
// }
// }p

#[derive(Clone, Debug)]
pub struct BaoHash(pub bao::Hash);

Expand Down
9 changes: 4 additions & 5 deletions tests/file.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::fs;
use std::pin::Pin;
//use tokio::stream::StreamExt;

use anyhow::{Error, Result};
use anyhow::Result;
use axum::body::Bytes;
use bytes::BytesMut;
use carbonado_node::{
backend::fs::{read_catalog, read_file, write_file, FileStream},
backend::fs::{read_file, write_file, FileStream},
config::node_shared_secret,
prelude::SEGMENT_SIZE,
structs::{Hash, Lookup, Secp256k1PubKey},
Expand Down Expand Up @@ -47,7 +46,7 @@ async fn write_read() -> Result<()> {
.map(|chunk| Ok(Bytes::from(chunk)))
.boxed();

let blake3_hash = write_file(&Secp256k1PubKey(pk), file_stream, None, "mime_type_test").await?;
let blake3_hash = write_file(&Secp256k1PubKey(pk), file_stream, None).await?;

info!("Reading file, {}", blake3_hash);

Expand Down Expand Up @@ -98,7 +97,7 @@ async fn read_write_delete_file() -> Result<()> {

let (sk, pk) = generate_keypair(&mut thread_rng());

let file_did_write = write_file(&Secp256k1PubKey(pk), file_stream, None, "mime_type_test")
let file_did_write = write_file(&Secp256k1PubKey(pk), file_stream, None)
.await
.is_ok();

Expand Down

0 comments on commit 947f8b0

Please sign in to comment.