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

WIP(service/box): add box service support #2785

Closed

Conversation

A-Stupid-Sun
Copy link
Contributor

related issee: #2773

Comment on lines +240 to +280
pub async fn box_upload(
&self,
path: &str,
size: Option<usize>,
content_type: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let folder_path=self.truncate_filename(path.trim_end_matches('/'));
let file_name="test.txt";
let url: String = "https://upload.box.com/api/2.0/files/content".to_string();
let mut req = Request::post(&url);
req = req.header(CONTENT_TYPE, "multipart/form-data; boundary=my-boundary");
let mut req_body = BytesMut::with_capacity(100);
write!(
&mut req_body,
"--my-boundary\ncontent-disposition: form-data; name=\"attributes\"\n\n{{\"name\":\"{}\", \"parent\":{{\"id\":\"{}\"}}}}\n--my-boundary\n",
file_name,
folder_path// should be folder id
).unwrap();

write!(
&mut req_body,
"content-disposition: form-data; name=\"file\"; filename=\"{}\"\n",
file_name,
).unwrap();
if let Some(mime) = content_type {
write!(&mut req_body, "Content-Type: {}\n\n", mime).unwrap();
} else {
write!(&mut req_body, "Content-Type: application/octet-stream\n\n").unwrap();
}
if let AsyncBody::Bytes(bytes) = body {
req_body.extend_from_slice(&bytes);
}
write!(&mut req_body, "\n--my-boundary").unwrap();
let req_body = AsyncBody::Bytes(req_body.freeze());
let mut request = req.body(req_body).map_err(new_request_build_error)?;


self.sign(&mut request).await?;
self.client.send(request).await
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the upload api reference:
https://developer.box.com/guides/uploads/direct/file/
https://developer.box.com/reference/post-files-content/
the post message seems like
image
I wonder if it's correct to implement this api in this way

Copy link
Contributor Author

@A-Stupid-Sun A-Stupid-Sun Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use FormDataPart in http_util/multpart.rs, It seems can not fix the filename field in the post message, should we build the request body manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someone could provide me with some guidance, I would be very grateful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Xuanwo


write!(
&mut req_body,
"content-disposition: form-data; name=\"file\"; filename=\"{}\"\n",
Copy link
Member

@Xuanwo Xuanwo Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use FormDataPart's header for this.

For example:

let multipart =  Multipart::new()
  .part(FormDataPart::new("attributes")
     .content(attributes_content))
  .part(FormDataPart::new("file")
     .header(CONTENT_DISPOSITION, format!("form-data; name=\"{file}\"; filename=\"{filename}\"").parse().unwrap())
     .content(file_content));

let req: http::request::Builder = Request::post(url);
let req = multipart.apply(req)?;

NOTE: FormDataPart doesn't support filename yet, so we have to modify the CONTENT_DISPOSITION by hand for now. We will fix this later.

Copy link
Contributor Author

@A-Stupid-Sun A-Stupid-Sun Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use FormDataPart's header for this.

For example:

let multipart =  Multipart::new()
  .part(FormDataPart::new("attributes")
     .content(attributes_content))
  .part(FormDataPart::new("file")
     .header(CONTENT_DISPOSITION, format!("form-data; name=\"{file}\"; filename=\"{filename}\"").parse().unwrap())
     .content(file_content));

let req: http::request::Builder = Request::post(url);
let req = multipart.apply(req)?;

NOTE: FormDataPart doesn't support filename yet, so we have to modify the CONTENT_DISPOSITION by hand for now. We will fix this later.

thanks, I got you, I check the code before, but haven't thought about inserting header again manually, you are right, it's much elegant to implement it.

@A-Stupid-Sun
Copy link
Contributor Author

Very Sorry for the delay, I was exhausted from preparing for various intern job opportunities in the past few months, Now things have done, Let me try
to fix this pr🥺

@Xuanwo
Copy link
Member

Xuanwo commented Nov 23, 2023

Very Sorry for the delay, I was exhausted from preparing for various intern job opportunities in the past few months

There is no sorry needed ❤️

Now things have done, Let me try to fix this pr🥺

Our core changed a lot since your last update, it's better to update branch first.

@Xuanwo Xuanwo mentioned this pull request Jan 2, 2024
44 tasks
@Xuanwo
Copy link
Member

Xuanwo commented Apr 9, 2024

Hi, @A-Stupid-Sun, thanks for your contribution first. We've made numerous updates to our Rust core, rendering this PR quite outdated. I'll be closing it but feel free to open a new one. I'm here to offer any help you might need.

@Xuanwo Xuanwo closed this Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants