-
Notifications
You must be signed in to change notification settings - Fork 481
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
Conversation
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 | ||
} |
There was a problem hiding this comment.
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
I wonder if it's correct to implement this api in this way
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
FormDataPart
'sheader
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.
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 |
There is no sorry needed ❤️
Our core changed a lot since your last update, it's better to update branch first. |
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. |
related issee: #2773