From fa7b449cb280b5725d4d66816102788f6e31dffd Mon Sep 17 00:00:00 2001 From: C0D3 M4513R <28912031+C0D3-M4513R@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:39:27 +0100 Subject: [PATCH] Use async file io and don't `unwrap` --- Cargo.toml | 1 + generate.sh | 10 ++++- src/apis/files_api.rs | 90 +++++++++++++++++++++---------------------- src/apis/mod.rs | 3 ++ 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0d5be42..c4922f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ license = "MIT" edition = "2021" [dependencies] +async-std = "1" serde = { version = "^1.0", features = ["derive"] } serde_with = { version = "^3.8", default-features = false, features = ["base64", "std", "macros"] } serde_json = "^1.0" diff --git a/generate.sh b/generate.sh index 86ccf3d..db31f51 100755 --- a/generate.sh +++ b/generate.sh @@ -42,7 +42,15 @@ sed -i 's/Result>/Result\s\+{/\0\nAsyncStdIo(::async_std::io::Error),/" src/apis/mod.rs +sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\("reqwest", e\.to_string\(\)\),/Error::AsyncStdIo(e) => ("async_std", e.to_string()),\0/' src/apis/mod.rs #Fix Debug +sed -E -i 's/Error::Reqwest\(e\)\s+=>\s+\e,/Error::AsyncStdIo(e) => e,\n\0/' src/apis/mod.rs #Fix Error +sed -i "s/impl From for Error/impl From<::async_std::io::Error> for Error {\nfn from(val: ::async_std::io::Error) -> Self {\n Error::AsyncStdIo(val)\n}\n\}\n\0/" src/apis/mod.rs #Add From impl +#Use the async file io +sed -i 's|// TODO: support file upload for '\''file'\'' parameter|let part = {let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(\&file).await?);\nif let Some(filename) = file.file_name() { \npart = part.file_name(filename.to_string_lossy().to_string()).mime_str(if filename.to_string_lossy().ends_with("png") { "image/png" } else { "application/octet-stream" })?;} \n else { part = part.mime_str("application/octet-stream")?; }\n part };\n\tlocal_var_form = local_var_form.part("file", part);|' src/apis/files_api.rs cargo fmt cargo build diff --git a/src/apis/files_api.rs b/src/apis/files_api.rs index 8574ae4..704a7a0 100644 --- a/src/apis/files_api.rs +++ b/src/apis/files_api.rs @@ -585,21 +585,21 @@ pub async fn upload_gallery_image( local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone()); } let mut local_var_form = reqwest::multipart::Form::new(); - let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap()) - .file_name(file.file_name().unwrap().to_string_lossy().to_string()) - .mime_str( - if file - .file_name() - .unwrap() - .to_string_lossy() - .to_string() - .ends_with("png") - { - "image/png" - } else { - "application/octet-stream" - }, - )?; + let part = { + let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?); + if let Some(filename) = file.file_name() { + part = part + .file_name(filename.to_string_lossy().to_string()) + .mime_str(if filename.to_string_lossy().ends_with("png") { + "image/png" + } else { + "application/octet-stream" + })?; + } else { + part = part.mime_str("application/octet-stream")?; + } + part + }; local_var_form = local_var_form.part("file", part); local_var_req_builder = local_var_req_builder.multipart(local_var_form); @@ -641,21 +641,21 @@ pub async fn upload_icon( local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone()); } let mut local_var_form = reqwest::multipart::Form::new(); - let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap()) - .file_name(file.file_name().unwrap().to_string_lossy().to_string()) - .mime_str( - if file - .file_name() - .unwrap() - .to_string_lossy() - .to_string() - .ends_with("png") - { - "image/png" - } else { - "application/octet-stream" - }, - )?; + let part = { + let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?); + if let Some(filename) = file.file_name() { + part = part + .file_name(filename.to_string_lossy().to_string()) + .mime_str(if filename.to_string_lossy().ends_with("png") { + "image/png" + } else { + "application/octet-stream" + })?; + } else { + part = part.mime_str("application/octet-stream")?; + } + part + }; local_var_form = local_var_form.part("file", part); local_var_req_builder = local_var_req_builder.multipart(local_var_form); @@ -700,21 +700,21 @@ pub async fn upload_image( local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone()); } let mut local_var_form = reqwest::multipart::Form::new(); - let part = reqwest::multipart::Part::bytes(std::fs::read(&file).unwrap()) - .file_name(file.file_name().unwrap().to_string_lossy().to_string()) - .mime_str( - if file - .file_name() - .unwrap() - .to_string_lossy() - .to_string() - .ends_with("png") - { - "image/png" - } else { - "application/octet-stream" - }, - )?; + let part = { + let mut part = reqwest::multipart::Part::bytes(::async_std::fs::read(&file).await?); + if let Some(filename) = file.file_name() { + part = part + .file_name(filename.to_string_lossy().to_string()) + .mime_str(if filename.to_string_lossy().ends_with("png") { + "image/png" + } else { + "application/octet-stream" + })?; + } else { + part = part.mime_str("application/octet-stream")?; + } + part + }; local_var_form = local_var_form.part("file", part); local_var_form = local_var_form.text("tag", tag.to_string()); if let Some(local_var_param_value) = animation_style { diff --git a/src/apis/mod.rs b/src/apis/mod.rs index b87bc7d..4e32afe 100644 --- a/src/apis/mod.rs +++ b/src/apis/mod.rs @@ -10,6 +10,7 @@ pub struct ResponseContent { #[derive(Debug)] pub enum Error { + AsyncStdIo(::async_std::io::Error), Reqwest(reqwest::Error), Serde(serde_json::Error), Io(std::io::Error), @@ -19,6 +20,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (module, e) = match self { + Error::AsyncStdIo(e) => ("async_std", e.to_string()), Error::Reqwest(e) => ("reqwest", e.to_string()), Error::Serde(e) => ("serde", e.to_string()), Error::Io(e) => ("IO", e.to_string()), @@ -31,6 +33,7 @@ impl fmt::Display for Error { impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { Some(match self { + Error::AsyncStdIo(e) => e, Error::Reqwest(e) => e, Error::Serde(e) => e, Error::Io(e) => e,