mirror of
https://github.com/TeamPiped/piped-proxy.git
synced 2024-08-14 23:50:45 +00:00
Put avif behind a feature flag.
This commit is contained in:
parent
477c412875
commit
5c9311f654
2 changed files with 16 additions and 14 deletions
11
Cargo.toml
11
Cargo.toml
|
@ -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"]
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue