Put avif behind a feature flag.

This commit is contained in:
Kavin 2023-07-13 11:46:17 +01:00
parent 477c412875
commit 5c9311f654
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F
2 changed files with 16 additions and 14 deletions

View file

@ -12,8 +12,11 @@ libwebp-sys = "0.9.2"
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"
ravif = "0.11.2" ravif = { version = "0.11.2", optional = true }
rgb = "0.8.36" rgb = { version = "0.8.36", optional = true }
regex = "1.9.1" regex = "1.9.1"
reqwest = {version = "0.11.18", features = ["rustls-tls", "stream", "brotli", "gzip"], default-features = false} reqwest = { version = "0.11.18", features = ["rustls-tls", "stream", "brotli", "gzip"], default-features = false }
tokio = {version = "1.29.1", features = ["full"]} tokio = { version = "1.29.1", features = ["full"] }
[features]
avif = ["dep:ravif", "dep:rgb"]

View file

@ -1,16 +1,14 @@
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 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;
use ravif::{Encoder, Img};
use regex::Regex; use regex::Regex;
use reqwest::{Body, Client, Request, Url}; use reqwest::{Body, Client, Request, Url};
use rgb::FromSlice;
#[global_allocator] #[global_allocator]
static GLOBAL: MiMalloc = MiMalloc; static GLOBAL: MiMalloc = MiMalloc;
@ -30,8 +28,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> =
@ -119,6 +117,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
} }
}; };
#[cfg(feature = "avif")]
let avif = { let avif = {
if let Some(avif) = query.get("avif") { if let Some(avif) = query.get("avif") {
avif == "true" avif == "true"
@ -210,7 +209,11 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
if rewrite { if rewrite {
if let Some(content_type) = resp.headers().get("content-type") { if let Some(content_type) = resp.headers().get("content-type") {
#[cfg(feature = "avif")]
if content_type == "image/webp" || content_type == "image/jpeg" && avif { if content_type == "image/webp" || content_type == "image/jpeg" && avif {
use ravif::{Encoder, Img};
use rgb::FromSlice;
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();
@ -221,11 +224,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
let buf = image.into_rgb8(); let buf = image.into_rgb8();
let buf = buf.as_raw().as_rgb(); let buf = buf.as_raw().as_rgb();
let buffer = Img::new( let buffer = Img::new(buf, width, height);
buf,
width,
height,
);
let res = Encoder::new() let res = Encoder::new()
.with_quality(80f32) .with_quality(80f32)