Turn webp rewriting into a feature flag.

This commit is contained in:
Kavin 2023-08-05 16:26:19 +01:00
parent 53329c43e8
commit 5212b4213c
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F
2 changed files with 10 additions and 5 deletions

View file

@ -8,7 +8,7 @@ version = "0.1.0"
[dependencies] [dependencies]
actix-web = "4.3.1" actix-web = "4.3.1"
image = "0.24.6" image = "0.24.6"
libwebp-sys = "0.9.2" libwebp-sys = { version = "0.9.2", optional = true }
mimalloc = "0.1.37" mimalloc = "0.1.37"
once_cell = "1.18.0" once_cell = "1.18.0"
qstring = "0.7.2" qstring = "0.7.2"
@ -19,4 +19,6 @@ reqwest = { version = "0.11.18", features = ["rustls-tls", "stream", "brotli", "
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }
[features] [features]
default = ["webp"]
avif = ["dep:ravif", "dep:rgb"] avif = ["dep:ravif", "dep:rgb"]
webp = ["dep:libwebp-sys"]

View file

@ -1,9 +1,8 @@
use std::env; use std::env;
use std::error::Error; use std::error::Error;
use actix_web::{App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, web};
use actix_web::http::Method; use actix_web::http::Method;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
use libwebp_sys::{WebPEncodeRGB, WebPFree};
use mimalloc::MiMalloc; use mimalloc::MiMalloc;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use qstring::QString; use qstring::QString;
@ -28,8 +27,8 @@ async fn main() -> std::io::Result<()> {
let bind = env::var("BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string()); let bind = env::var("BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string());
server.bind(bind)? server.bind(bind)?
} }
.run() .run()
.await .await
} }
static RE_DOMAIN: Lazy<Regex> = static RE_DOMAIN: Lazy<Regex> =
@ -221,7 +220,10 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
}; };
} }
#[cfg(feature = "webp")]
if content_type == "image/jpeg" { if content_type == "image/jpeg" {
use libwebp_sys::{WebPEncodeRGB, WebPFree};
let resp_bytes = resp.bytes().await.unwrap(); let resp_bytes = resp.bytes().await.unwrap();
let image = image::load_from_memory(&resp_bytes).unwrap(); let image = image::load_from_memory(&resp_bytes).unwrap();
@ -256,6 +258,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
response.content_type("image/jpeg"); response.content_type("image/jpeg");
return Ok(response.body(resp_bytes)); return Ok(response.body(resp_bytes));
} }
if content_type == "application/x-mpegurl" if content_type == "application/x-mpegurl"
|| content_type == "application/vnd.apple.mpegurl" || content_type == "application/vnd.apple.mpegurl"
{ {