如何将缓冲区转换为BSON格式?

发布于 2025-02-01 00:25:53 字数 1378 浏览 4 评论 0原文

我正在尝试将一个文件(我打开并读取为缓冲区)将文件转换为有效的BSON格式。

我编写了客户端,以提出一个需要两个字段的请求。

  1. 文件
  2. 文件的名称(缓冲区)

这里的问题是我似乎无法在这里成功进行转换。

另一个问题是,在进行此转换后,是否有可能将此BSON请求转换为缓冲区,因为这是卷曲(易于)板条板的类型(易于)进行请求(即来自终端的请求,而不是表单的浏览器)

这是我提出此请求的代码

// It takes in a file path.
fn send_file_post(file_from_arg: &str) -> tide::Result {

    // initialise the connection to the server
    let mut easy = Easy::new();
    easy.url("http://0.0.0.0:8080/hi").unwrap();

    // Opens and reads the file path
    let mut file = File::open(file_from_arg)?;
    let mut buf = [0; 1096];

    // reads file into buffer
    loop {
        let n = file.read(&mut buf)?;

        if n == 0 {
            // reached end of file
            break;
        }

        // easy.write_all(&buf[..n])?;
    }


// attempted bson format
    let bson_data: Bson = bson!({
    "name": file_from_arg,
    "file": buf
});

// sending the request, this is only sending the file for now, I want to send a bson format that is buffered (in a buffer/bytes format) 
    easy.post_fields_copy(&buf).unwrap();
    easy.write_function(|data| {
        stdout().write_all(data).unwrap();
        Ok(data.len())
    })
    .unwrap();

    println!(" oh hi{:?}", easy.perform().unwrap());
    Ok(format!("okay sent!").into())
}

I'm trying to convert a file (that I opened and read into a buffer), into a valid BSON format.

I writing the client-side for making a request that takes two fields;

  1. Name of the file
  2. File(buffer)

The problem here is I can't seem to make a successful conversion here.

Another question is, after making this conversion, is it possible to convert this BSON request into a buffer, because that's the type curl(Easy) crate takes for making its requests (i.e requests that are from the terminal, not the browser of forms)

this is my code for making this request

// It takes in a file path.
fn send_file_post(file_from_arg: &str) -> tide::Result {

    // initialise the connection to the server
    let mut easy = Easy::new();
    easy.url("http://0.0.0.0:8080/hi").unwrap();

    // Opens and reads the file path
    let mut file = File::open(file_from_arg)?;
    let mut buf = [0; 1096];

    // reads file into buffer
    loop {
        let n = file.read(&mut buf)?;

        if n == 0 {
            // reached end of file
            break;
        }

        // easy.write_all(&buf[..n])?;
    }


// attempted bson format
    let bson_data: Bson = bson!({
    "name": file_from_arg,
    "file": buf
});

// sending the request, this is only sending the file for now, I want to send a bson format that is buffered (in a buffer/bytes format) 
    easy.post_fields_copy(&buf).unwrap();
    easy.write_function(|data| {
        stdout().write_all(data).unwrap();
        Ok(data.len())
    })
    .unwrap();

    println!(" oh hi{:?}", easy.perform().unwrap());
    Ok(format!("okay sent!").into())
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2025-02-08 00:25:53

我意识到Serde_json有一个转换为VEC!方法,如果不相同,则可以比喻为字节。因此,我将文件转换为字节并将其作为缓冲区发送。这就是下面的功能。


// It takes in a file path.
fn send_file_post(file_from_arg: &str, port_addr: &str) -> tide::Result {
    // initialise
    let mut easy = Easy::new();
    let port = format!("{}/hi", port_addr);
    easy.url(&port).unwrap();
    
// reads file path
    let file = std::fs::read(file_from_arg)?;


    //extracts name of the file from file path
    let (.., file_name) = file_from_arg
        .rsplit_once(std::path::MAIN_SEPARATOR)
        .unwrap();

// creates the necessary type to send the file in bytes
    let new_post = FileSearch {
        file_name: file_name.to_string(),
        file_bytes: file,
    };

// Unwrap into a vector, which can be likened to bytes
    let send_file_body_req = serde_json::to_vec(&new_post).unwrap();

    // make and send request
    easy.post(true).unwrap();
    easy.post_field_size(send_file_body_req.len() as u64).unwrap();

    let mut transfer = easy.transfer();
    transfer
        .read_function(|buf| Ok(send_file_body_req.as_slice().read(buf).unwrap_or(0)))
        .unwrap();
    transfer.perform().unwrap();

    Ok(format!("okay sent!").into())
}


I realised that serde_json has a convert to vec! method, which can be likened to bytes if not the same. So I converted the file into bytes and sent it as a buffer. This is what the function looks like below.


// It takes in a file path.
fn send_file_post(file_from_arg: &str, port_addr: &str) -> tide::Result {
    // initialise
    let mut easy = Easy::new();
    let port = format!("{}/hi", port_addr);
    easy.url(&port).unwrap();
    
// reads file path
    let file = std::fs::read(file_from_arg)?;


    //extracts name of the file from file path
    let (.., file_name) = file_from_arg
        .rsplit_once(std::path::MAIN_SEPARATOR)
        .unwrap();

// creates the necessary type to send the file in bytes
    let new_post = FileSearch {
        file_name: file_name.to_string(),
        file_bytes: file,
    };

// Unwrap into a vector, which can be likened to bytes
    let send_file_body_req = serde_json::to_vec(&new_post).unwrap();

    // make and send request
    easy.post(true).unwrap();
    easy.post_field_size(send_file_body_req.len() as u64).unwrap();

    let mut transfer = easy.transfer();
    transfer
        .read_function(|buf| Ok(send_file_body_req.as_slice().read(buf).unwrap_or(0)))
        .unwrap();
    transfer.perform().unwrap();

    Ok(format!("okay sent!").into())
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文