Skip to content

Commit

Permalink
fix(Backend): 🐛 use multipart for imgur api to send image other than …
Browse files Browse the repository at this point in the history
…webp
  • Loading branch information
Eason0729 committed Feb 15, 2024
1 parent 2fec51a commit 6c55a98
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ features = ["serde"]
[dependencies.reqwest]
version = "0.11.22"
default-features = false
features = ["rustls-tls", "json"]
features = ["rustls-tls", "json","multipart"]

[dependencies.k256]
version = "0.13.2"
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controller/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ struct DupKey {
}

/// Request Duplication
///
///
/// It cache request result with fat pointer and provide safe interface to access data
///
///
/// Note that for effeciency, it uses Clock-Pro cache algorithm, expect occasional missing,
/// shouldn't be rely on in unstable connection
pub struct DupController {
Expand Down
23 changes: 17 additions & 6 deletions backend/src/controller/imgur.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::collections::HashMap;

use reqwest::Client;
use reqwest::{multipart, Client};
use serde::Serialize;
use tracing::instrument;

use crate::init::config;
use crate::{init::config, report_internal};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("reqwest error")]
#[error("reqwest error `{0}`")]
Reqwest(#[from] reqwest::Error),
#[error("old API version was used")]
OldApi,
#[error("Invaild image")]
InvaildImage,
}

impl From<Error> for tonic::Status {
fn from(value: Error) -> Self {
match value {
Error::Reqwest(x) => tonic::Status::internal(format!("reqwest error: {}", x)),
Error::OldApi => tonic::Status::internal("old API version was used"),
Error::InvaildImage => tonic::Status::failed_precondition("Invaild image"),
_ => report_internal!(error, value),
}
}
}
Expand Down Expand Up @@ -51,16 +53,25 @@ impl ImgurController {
/// upload image
#[instrument(skip_all, level = "debug")]
pub async fn upload(&self, image: Vec<u8>) -> Result<String, Error> {
// check max image size(10MB)
if image.len() >= 10 * 1000 * 1000 {
return Err(Error::InvaildImage);
}

let part = multipart::Part::bytes(image).file_name("upload.bin");
let form = multipart::Form::new().part("image", part);

let res = self
.client
.post("https://api.imgur.com/3/image")
.body(image)
.multipart(form)
.header(
"Authorization",
["Client-ID", &self.config.client_id].concat(),
)
.send()
.await?;

let payload: HashMap<String, String> = res.json().await?;

let link = payload.get("link").cloned().ok_or(Error::OldApi)?;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controller/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ macro_rules! impl_metrics {
}

/// collection of statful metrics
///
///
/// because metrics(opentelemetry) sdk is not yet GA,
/// stateful metrics is necessary in state of art.
/// stateful metrics is necessary in state of art.
pub struct MetricsController {
user: UpDownCounter<i64>,
submit: UpDownCounter<i64>,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! collection of controller
//!
//!
//! controller are stateful, independent conponment and should
//! include intergated test
Expand Down
8 changes: 8 additions & 0 deletions backend/src/macro_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ macro_rules! report_internal {
tracing::$level!($pattern);
tonic::Status::internal($pattern.to_string())
}};
($level:ident,$pattern:expr) => {{
tracing::$level!("{}", $pattern);
tonic::Status::internal($pattern.to_string())
}};
($level:ident,$pattern:literal, $error:expr) => {{
tracing::$level!($pattern, $error);
tonic::Status::internal($error.to_string())
Expand All @@ -18,6 +22,10 @@ macro_rules! report_internal {
tracing::$level!($pattern);
tonic::Status::unknown("unknown error")
}};
($level:ident,$pattern:expr) => {{
tracing::$level!("{}", $pattern);
tonic::Status::unknown("unknown error")
}};
($level:ident,$pattern:literal, $error:expr) => {{
tracing::$level!($pattern, $error);
tonic::Status::unknown("unknown error")
Expand Down
4 changes: 2 additions & 2 deletions judger/src/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ pub enum InitError {
pub enum Error {
#[error("Language not found")]
LangNotFound,
#[error("Internal Error: `{0}`")]
#[error("`{0}`")]
Sandbox(#[from] sandbox::Error),
}

impl From<Error> for tonic::Status {
fn from(value: Error) -> Self {
match value {
Error::LangNotFound => tonic::Status::failed_precondition("lang not found"),
Error::Sandbox(x) => tonic::Status::internal(format!("{}", x)),
_ => tonic::Status::internal(value.to_string()),
}
}
}
Expand Down

0 comments on commit 6c55a98

Please sign in to comment.