Skip to content

Commit

Permalink
wip: webp
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Nov 16, 2024
1 parent b7dfa3b commit 28ed502
Show file tree
Hide file tree
Showing 20 changed files with 208 additions and 112 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ reqwest = { version = "0.12", features = [
"json",
"rustls-tls",
], default-features = false }
sysinfo = "0.32.0"
sysinfo = { version = "0.32.0" }
tempfile = { version = "3.14.0" }
image = { version = "0.25.5" }
webp = { version = "0.3.0", features = ["image"] }
hex = "0.4.3"

[build-dependencies]
chrono = { version = "0.4" }
Expand Down
4 changes: 3 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ where
}
}

pub async fn getdel<T>(key: impl Into<RedisKey> + Send + Display) -> Result<Option<T>, CacheError>
pub async fn get_del<T>(
key: impl Into<RedisKey> + Send + Display,
) -> Result<Option<T>, CacheError>
where
T: for<'de> Deserialize<'de>, {
let result = get_client().getdel::<Option<Value>, _>(key).await?;
Expand Down
20 changes: 9 additions & 11 deletions src/cluster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use kube::{
use tokio_util::codec::Framed;
use tracing::{error, info};

use crate::config;

static K8S_CLIENT: OnceLock<K8sClient> = OnceLock::new();

pub fn get_k8s_client() -> &'static K8sClient {
Expand Down Expand Up @@ -56,7 +54,7 @@ pub async fn create(

let pod_api: Api<Pod> = Api::namespaced(
client.clone(),
config::get_config().await.cluster.namespace.as_str(),
crate::env::get_env().cluster.namespace.as_str(),
);

let mut env_vars: Vec<EnvVar> = challenge
Expand All @@ -79,7 +77,7 @@ pub async fn create(
.ports
.iter()
.map(|port| ContainerPort {
container_port: *port as i32,
container_port: *port,
protocol: Some("TCP".to_string()),
..Default::default()
})
Expand All @@ -92,7 +90,7 @@ pub async fn create(
name: name.clone(),
image: challenge.image_name.clone(),
env: Some(env_vars),
ports: Some(match config::get_config().await.cluster.proxy.enabled {
ports: Some(match crate::env::get_env().cluster.proxy.enabled {
true => vec![],
false => container_ports,
}),
Expand All @@ -110,7 +108,7 @@ pub async fn create(

let service_api: Api<Service> = Api::namespaced(
client.clone(),
config::get_config().await.cluster.namespace.as_str(),
crate::env::get_env().cluster.namespace.as_str(),
);

let service = Service {
Expand All @@ -125,7 +123,7 @@ pub async fn create(
.ports
.iter()
.map(|port| ServicePort {
port: *port as i32,
port: *port,
target_port: None,
protocol: Some("TCP".to_string()),
..Default::default()
Expand All @@ -150,10 +148,10 @@ pub async fn create(
nats.push(crate::model::pod::Nat {
src: format!("{}", port.port),
dst: Some(format!("{}", node_port)),
proxy: config::get_config().await.cluster.proxy.enabled,
proxy: crate::env::get_env().cluster.proxy.enabled,
entry: Some(format!(
"{}:{}",
config::get_config().await.cluster.entry,
crate::config::get_config().await.cluster.entry,
node_port
)),
});
Expand All @@ -168,15 +166,15 @@ pub async fn create(
pub async fn delete(name: String) {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client().clone(),
config::get_config().await.cluster.namespace.as_str(),
crate::env::get_env().cluster.namespace.as_str(),
);
let _ = pod_api.delete(&name, &DeleteParams::default()).await;
}

pub async fn wsrx(name: String, port: u16, ws: WebSocket) -> Result<(), anyhow::Error> {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client().clone(),
config::get_config().await.cluster.namespace.as_str(),
crate::env::get_env().cluster.namespace.as_str(),
);
let mut pf = pod_api.portforward(&name, &[port]).await?;
let pfw = pf.take_stream(port);
Expand Down
2 changes: 0 additions & 2 deletions src/config/cluster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, FromJsonQueryResult, PartialEq, Eq, Default)]
pub struct Config {
pub entry: String,
pub namespace: String,
pub proxy: proxy::Config,
pub strategy: strategy::Config,
}
5 changes: 0 additions & 5 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ pub async fn init_config() {
},
}),
cluster: Set(crate::config::cluster::Config {
namespace: String::from("default"),
entry: String::from("127.0.0.1"),
proxy: crate::config::cluster::proxy::Config {
enabled: true,
traffic_capture: false,
},
strategy: crate::config::cluster::strategy::Config {
parallel_limit: 0,
request_limit: 0,
Expand Down
9 changes: 9 additions & 0 deletions src/env/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod proxy;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Env {
pub namespace: String,
pub proxy: proxy::Env,
}
7 changes: 7 additions & 0 deletions src/env/cluster/proxy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Env {
pub enabled: bool,
pub traffic_capture: bool,
}
2 changes: 2 additions & 0 deletions src/env/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod axum;
pub mod cache;
pub mod cluster;
pub mod consts;
pub mod db;
pub mod metric;
Expand All @@ -21,6 +22,7 @@ pub struct Env {
pub queue: queue::Env,
pub cache: cache::Env,
pub metric: metric::Env,
pub cluster: cluster::Env,
}

pub async fn init() {
Expand Down
28 changes: 21 additions & 7 deletions src/media/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
pub mod traits;
pub mod util;

use std::{error::Error, path::PathBuf};

use tokio::{
fs::{create_dir_all, metadata, read_dir, remove_dir_all, File},
fs::{create_dir_all, metadata, read_dir, remove_dir_all, remove_file, File},
io::{AsyncReadExt, AsyncWriteExt},
};

pub async fn get(path: String, filename: String) -> Result<Vec<u8>, Box<dyn Error>> {
use crate::media::traits::MediaError;

pub async fn get(path: String, filename: String) -> Result<Vec<u8>, MediaError> {
let filepath =
PathBuf::from(crate::env::consts::path::MEDIA).join(format!("{}/{}", path, filename));

match File::open(&filepath).await {
Ok(mut file) => {
let mut buffer = Vec::new();
if let Err(_) = file.read_to_end(&mut buffer).await {
return Err("internal_server_error".into());
return Err(MediaError::InternalServerError(String::new()));
}
Ok(buffer)
}
Err(_) => Err("not_found".into()),
Err(_) => Err(MediaError::NotFound(String::new())),
}
}

pub async fn scan_dir(path: String) -> Result<Vec<(String, u64)>, Box<dyn Error>> {
pub async fn scan_dir(path: String) -> Result<Vec<(String, u64)>, MediaError> {
let filepath = PathBuf::from(crate::env::consts::path::MEDIA).join(path);
let mut files = Vec::new();

Expand All @@ -43,7 +48,7 @@ pub async fn scan_dir(path: String) -> Result<Vec<(String, u64)>, Box<dyn Error>
Ok(files)
}

pub async fn save(path: String, filename: String, data: Vec<u8>) -> Result<(), Box<dyn Error>> {
pub async fn save(path: String, filename: String, data: Vec<u8>) -> Result<(), MediaError> {
let filepath =
PathBuf::from(crate::env::consts::path::MEDIA).join(format!("{}/{}", path, filename));
if let Some(parent) = filepath.parent() {
Expand All @@ -56,7 +61,16 @@ pub async fn save(path: String, filename: String, data: Vec<u8>) -> Result<(), B
Ok(())
}

pub async fn delete(path: String) -> Result<(), Box<dyn Error>> {
pub async fn delete(path: String, filename: String) -> Result<(), MediaError> {
let filepath =
PathBuf::from(crate::env::consts::path::MEDIA).join(format!("{}/{}", path, filename));
if metadata(&filepath).await.is_ok() {
remove_file(&filepath).await?;
}
Ok(())
}

pub async fn delete_dir(path: String) -> Result<(), Box<dyn Error>> {
let filepath = PathBuf::from(crate::env::consts::path::MEDIA).join(path);
if metadata(&filepath).await.is_ok() {
remove_dir_all(&filepath).await?;
Expand Down
15 changes: 15 additions & 0 deletions src/media/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::io;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum MediaError {
#[error("not found: {0}")]
NotFound(String),
#[error("internal server error: {0}")]
InternalServerError(String),
#[error("I/O error: {0}")]
IoError(#[from] io::Error),
#[error(transparent)]
OtherError(#[from] anyhow::Error),
}
16 changes: 16 additions & 0 deletions src/media/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ring::digest::{Context, SHA256};

pub fn hash(data: Vec<u8>) -> String {
let mut context = Context::new(&SHA256);
context.update(&data);
let digest = context.finish();
hex::encode(digest.as_ref())
}

pub async fn img_convert_to_webp(img: Vec<u8>) -> Result<Vec<u8>, anyhow::Error> {
let origin_image = image::load_from_memory(&*img)?.to_rgba8();
let webp_encoder =
webp::Encoder::from_rgba(&origin_image, origin_image.width(), origin_image.height());
let webp_image = webp_encoder.encode(85.0);
Ok(webp_image.to_vec())
}
1 change: 1 addition & 0 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod middleware;
pub mod model;
pub mod router;
pub mod traits;
pub mod util;

use std::sync::OnceLock;

Expand Down
4 changes: 2 additions & 2 deletions src/web/router/api/challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub async fn save_attachment(
}
}

crate::media::delete(path.clone()).await.unwrap();
crate::media::delete_dir(path.clone()).await.unwrap();

let _ = crate::media::save(path, filename, data)
.await
Expand All @@ -392,7 +392,7 @@ pub async fn delete_attachment(

let path = format!("challenges/{}/attachment", id);

let _ = crate::media::delete(path)
let _ = crate::media::delete_dir(path)
.await
.map_err(|_| WebError::InternalServerError(String::new()))?;

Expand Down
Loading

0 comments on commit 28ed502

Please sign in to comment.