Skip to content

Commit

Permalink
handle non-text client files
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeragamba committed Jul 14, 2024
1 parent 17d868b commit 05c58f6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
27 changes: 27 additions & 0 deletions packages/og-injector-rust/Cargo.lock

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

2 changes: 2 additions & 0 deletions packages/og-injector-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ url = "1.7.2"
uuid = { version = "1.10.0", features = ["v4", "serde"] }
iso8601 = "0.6.1"
derive_builder = "0.20.0"
mime_guess = "2.0.5"
mime = "0.3.17"

[profile.release]
panic = "abort"
10 changes: 8 additions & 2 deletions packages/og-injector-rust/src/client/client_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub struct ClientFiles;

impl ClientFiles {
pub async fn read(file: &str) -> AppResult<String> {
let raw = Self::read_raw(file).await?;
let contents = std::str::from_utf8(&raw)?;
Ok(contents.to_string())
}

pub async fn read_raw(file: &str) -> AppResult<Vec<u8>> {
let config = ClientConfig::new();
let file_path = config.dir.join(file);

Expand All @@ -16,10 +22,10 @@ impl ClientFiles {
return Err(AppError::NotFoundError(path.to_string()));
}

let mut contents = String::new();
let mut contents = vec![];

let mut file = File::open(file_path)?;
file.read_to_string(&mut contents)?;
file.read_to_end(&mut contents)?;

Ok(contents)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/og-injector-rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::error::Error;
use std::fmt;
use std::str::Utf8Error;

use axum::body::Body;
use axum::http::StatusCode;
Expand Down Expand Up @@ -69,3 +70,9 @@ impl From<OpenGraphDataBuilderError> for AppError {
AppError::new(error.to_string())
}
}

impl From<Utf8Error> for AppError {
fn from(error: Utf8Error) -> Self {
AppError::new(error.to_string())
}
}
9 changes: 7 additions & 2 deletions packages/og-injector-rust/src/injector/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::extract::Path;
use axum::http::Request;
use axum::Router;
use axum::routing::get;
use mime_guess::mime;

use crate::client::ClientFiles;
use crate::injector::{inject_default_meta, inject_post_meta, inject_tag_meta};
Expand Down Expand Up @@ -67,6 +68,10 @@ async fn get_fallback(req: Request<Body>) -> Result {

let file_path = uri.path().strip_prefix('/').unwrap_or("");

let body = ClientFiles::read(file_path).await?;
Ok(RouterResponse::Html(body))
let body = ClientFiles::read_raw(file_path).await?;
let mime_type = mime_guess::from_path(uri.to_string())
.first()
.unwrap_or(mime::APPLICATION_OCTET_STREAM);

Ok(RouterResponse::Raw { body, mime_type })
}
8 changes: 8 additions & 0 deletions packages/og-injector-rust/src/injector/router_response.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use axum::body::Body;
use axum::http::{header, StatusCode};
use axum::response::{IntoResponse, Response};
use mime::Mime;

pub enum RouterResponse {
Html(String),
Raw { body: Vec<u8>, mime_type: Mime },
}

impl IntoResponse for RouterResponse {
Expand All @@ -15,6 +17,12 @@ impl IntoResponse for RouterResponse {
.header("x-generated-by", "rust")
.body(Body::from(body))
.unwrap(),
RouterResponse::Raw { body, mime_type } => Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime_type.to_string())
.header("x-generated-by", "rust")
.body(Body::from(body))
.unwrap(),
}
}
}
8 changes: 0 additions & 8 deletions packages/og-injector-rust/src/server_api/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ impl ServerApi {
let res = self.client.get(url).send().await?;
let body = res.text().await?;

println!("{}", body);

let res: GetPostRes = serde_json::from_str(&body)?;
Ok(res.post)
}
Expand All @@ -55,8 +53,6 @@ impl ServerApi {
let res = self.client.get(url).send().await?;
let body = res.text().await?;

println!("{}", body);

let res: GetPostRes = serde_json::from_str(&body)?;
Ok(res.post)
}
Expand All @@ -68,8 +64,6 @@ impl ServerApi {
let res = self.client.get(url).send().await?;
let body = res.text().await?;

println!("{}", body);

let res: GetTagRes = serde_json::from_str(&body)?;
Ok(res.tag)
}
Expand All @@ -81,8 +75,6 @@ impl ServerApi {
let res = self.client.get(url).send().await?;
let body = res.text().await?;

println!("{}", body);

let res: GetTaggedPostsRes = serde_json::from_str(&body)?;
Ok(res.posts)
}
Expand Down

0 comments on commit 05c58f6

Please sign in to comment.