mirror of
https://github.com/TeamPiped/piped-proxy.git
synced 2024-08-14 23:50:45 +00:00
Perform webp and avif conversion on blocking tasks.
This commit is contained in:
parent
e642b968a8
commit
0d4eda637d
1 changed files with 59 additions and 51 deletions
38
src/main.rs
38
src/main.rs
|
@ -1,6 +1,3 @@
|
||||||
use std::env;
|
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use actix_web::http::Method;
|
use actix_web::http::Method;
|
||||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
|
use actix_web::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
|
||||||
use mimalloc::MiMalloc;
|
use mimalloc::MiMalloc;
|
||||||
|
@ -8,6 +5,10 @@ use once_cell::sync::Lazy;
|
||||||
use qstring::QString;
|
use qstring::QString;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::{Body, Client, Request, Url};
|
use reqwest::{Body, Client, Request, Url};
|
||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
use tokio::sync::oneshot;
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL: MiMalloc = MiMalloc;
|
static GLOBAL: MiMalloc = MiMalloc;
|
||||||
|
@ -213,11 +214,12 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
if let Some(content_type) = resp.headers().get("content-type") {
|
if let Some(content_type) = resp.headers().get("content-type") {
|
||||||
#[cfg(feature = "avif")]
|
#[cfg(feature = "avif")]
|
||||||
if content_type == "image/webp" || content_type == "image/jpeg" && avif {
|
if content_type == "image/webp" || content_type == "image/jpeg" && avif {
|
||||||
|
let resp_bytes = resp.bytes().await.unwrap();
|
||||||
|
let (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
|
||||||
|
spawn_blocking(|| {
|
||||||
use ravif::{Encoder, Img};
|
use ravif::{Encoder, Img};
|
||||||
use rgb::FromSlice;
|
use rgb::FromSlice;
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
let width = image.width() as usize;
|
let width = image.width() as usize;
|
||||||
|
@ -234,19 +236,22 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
.encode_rgb(buffer);
|
.encode_rgb(buffer);
|
||||||
|
|
||||||
return if let Ok(res) = res {
|
return if let Ok(res) = res {
|
||||||
response.content_type("image/avif");
|
tx.send((res.avif_file.to_vec(), "image/avif")).unwrap();
|
||||||
Ok(response.body(res.avif_file.to_vec()))
|
|
||||||
} else {
|
} else {
|
||||||
response.content_type("image/jpeg");
|
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
||||||
Ok(response.body(resp_bytes))
|
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
let (body, content_type) = rx.await.unwrap();
|
||||||
|
response.content_type(content_type);
|
||||||
|
return Ok(response.body(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "webp")]
|
#[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 (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
|
||||||
|
spawn_blocking(|| {
|
||||||
|
use libwebp_sys::{WebPEncodeRGB, WebPFree};
|
||||||
|
|
||||||
let image = image::load_from_memory(&resp_bytes).unwrap();
|
let image = image::load_from_memory(&resp_bytes).unwrap();
|
||||||
let width = image.width();
|
let width = image.width();
|
||||||
|
@ -273,12 +278,15 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if bytes.len() < resp_bytes.len() {
|
if bytes.len() < resp_bytes.len() {
|
||||||
response.content_type("image/webp");
|
tx.send((bytes, "image/webp")).unwrap();
|
||||||
return Ok(response.body(bytes));
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.content_type("image/jpeg");
|
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
||||||
return Ok(response.body(resp_bytes));
|
});
|
||||||
|
let (body, content_type) = rx.await.unwrap();
|
||||||
|
response.content_type(content_type);
|
||||||
|
return Ok(response.body(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
if content_type == "application/x-mpegurl"
|
if content_type == "application/x-mpegurl"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue