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::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
|
||||
use mimalloc::MiMalloc;
|
||||
|
@ -8,6 +5,10 @@ use once_cell::sync::Lazy;
|
|||
use qstring::QString;
|
||||
use regex::Regex;
|
||||
use reqwest::{Body, Client, Request, Url};
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::task::spawn_blocking;
|
||||
|
||||
#[global_allocator]
|
||||
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") {
|
||||
#[cfg(feature = "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 rgb::FromSlice;
|
||||
|
||||
let resp_bytes = resp.bytes().await.unwrap();
|
||||
|
||||
let image = image::load_from_memory(&resp_bytes).unwrap();
|
||||
|
||||
let width = image.width() as usize;
|
||||
|
@ -234,19 +236,22 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
|||
.encode_rgb(buffer);
|
||||
|
||||
return if let Ok(res) = res {
|
||||
response.content_type("image/avif");
|
||||
Ok(response.body(res.avif_file.to_vec()))
|
||||
tx.send((res.avif_file.to_vec(), "image/avif")).unwrap();
|
||||
} else {
|
||||
response.content_type("image/jpeg");
|
||||
Ok(response.body(resp_bytes))
|
||||
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
||||
};
|
||||
});
|
||||
let (body, content_type) = rx.await.unwrap();
|
||||
response.content_type(content_type);
|
||||
return Ok(response.body(body));
|
||||
}
|
||||
|
||||
#[cfg(feature = "webp")]
|
||||
if content_type == "image/jpeg" {
|
||||
use libwebp_sys::{WebPEncodeRGB, WebPFree};
|
||||
|
||||
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 width = image.width();
|
||||
|
@ -273,12 +278,15 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
|||
};
|
||||
|
||||
if bytes.len() < resp_bytes.len() {
|
||||
response.content_type("image/webp");
|
||||
return Ok(response.body(bytes));
|
||||
tx.send((bytes, "image/webp")).unwrap();
|
||||
return;
|
||||
}
|
||||
|
||||
response.content_type("image/jpeg");
|
||||
return Ok(response.body(resp_bytes));
|
||||
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
||||
});
|
||||
let (body, content_type) = rx.await.unwrap();
|
||||
response.content_type(content_type);
|
||||
return Ok(response.body(body));
|
||||
}
|
||||
|
||||
if content_type == "application/x-mpegurl"
|
||||
|
|
Loading…
Reference in a new issue