Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block_put_post handle multipart bytes #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kubo-rpc-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To see how to make this your own, look here:
[README]((https://openapi-generator.tech))

- API version: 0.9.0
- Build date: 2023-11-13T20:39:18.296854378Z[Etc/UTC]
- Build date: 2023-11-15T16:13:27.108848Z[Etc/UTC]



Expand Down
10 changes: 9 additions & 1 deletion kubo-rpc-server/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ paths:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/_dag_put_post_request'
$ref: '#/components/schemas/_block_put_post_request'
responses:
"200":
content:
Expand Down Expand Up @@ -558,6 +558,14 @@ components:
- Cid
- RemPath
type: object
_block_put_post_request:
properties:
file:
format: binary
type: string
required:
- file
type: object
_block_put_post_200_response:
example:
Size: 0.8008281904610115
Expand Down
2 changes: 1 addition & 1 deletion kubo-rpc-server/examples/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn main() {
}
Some("BlockPutPost") => {
let result = rt.block_on(client.block_put_post(
swagger::ByteArray(Vec::from("BYTE_ARRAY_DATA_HERE")),
swagger::ByteArray(Vec::from("BINARY_DATA_HERE")),
None,
None,
None,
Expand Down
19 changes: 12 additions & 7 deletions kubo-rpc-server/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,17 +560,22 @@ where

// For each parameter, encode as appropriate and add to the multipart body as a stream.

let file_vec = param_file.to_vec();

let file_mime = match mime_0_2::Mime::from_str("application/octet-stream") {
Ok(mime) => mime,
Err(err) => return Err(ApiError(format!("Unable to get mime type: {:?}", err))),
let file_str = match serde_json::to_string(&param_file) {
Ok(str) => str,
Err(e) => {
return Err(ApiError(format!(
"Unable to serialize file to string: {}",
e
)))
}
};

let file_vec = file_str.as_bytes().to_vec();
let file_mime =
mime_0_2::Mime::from_str("application/json").expect("impossible to fail to parse");
let file_cursor = Cursor::new(file_vec);

let filename = None as Option<&str>;
multipart.add_stream("file", file_cursor, filename, Some(file_mime));
multipart.add_stream("file", file_cursor, None as Option<&str>, Some(file_mime));

let mut fields = match multipart.prepare() {
Ok(fields) => fields,
Expand Down
16 changes: 13 additions & 3 deletions kubo-rpc-server/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,19 @@ where
let param_file = match field_file {
Some(field) => {
let mut reader = field[0].data.readable().expect("Unable to read field for file");
let mut data = vec![];
reader.read_to_end(&mut data).expect("Reading saved binary data should never fail");
swagger::ByteArray(data)
let mut data = String::new();
reader.read_to_string(&mut data).expect("Reading saved String should never fail");
let file_model: swagger::ByteArray = match serde_json::from_str(&data) {
Ok(model) => model,
Err(e) => {
return Ok(
Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("file data does not match API definition : {}", e)))
.expect("Unable to create Bad Request due to missing required form parameter file"))
}
};
file_model
},
None => {
return Ok(
Expand Down
2 changes: 1 addition & 1 deletion kubo-rpc/kubo-rpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ paths:
properties:
file:
type: string
format: byte
format: binary
responses:
'200':
description: success
Expand Down
Loading