Perform webp and avif conversion on blocking tasks.

This commit is contained in:
Kavin 2023-09-09 05:13:30 +01:00
parent e642b968a8
commit 0d4eda637d
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F

View file

@ -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,72 +214,79 @@ 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 {
use ravif::{Encoder, Img};
use rgb::FromSlice;
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 ravif::{Encoder, Img};
use rgb::FromSlice;
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;
let height = image.height() as usize; let height = image.height() as usize;
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(buf, width, height); let buffer = Img::new(buf, width, height);
let res = Encoder::new() let res = Encoder::new()
.with_quality(80f32) .with_quality(80f32)
.with_speed(7) .with_speed(7)
.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 { tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
response.content_type("image/jpeg"); };
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();
let height = image.height(); let height = image.height();
let quality = 85; let quality = 85;
let data = image.as_rgb8().unwrap().as_raw(); let data = image.as_rgb8().unwrap().as_raw();
let bytes: Vec<u8> = unsafe { let bytes: Vec<u8> = unsafe {
let mut out_buf = std::ptr::null_mut(); let mut out_buf = std::ptr::null_mut();
let stride = width as i32 * 3; let stride = width as i32 * 3;
let len: usize = WebPEncodeRGB( let len: usize = WebPEncodeRGB(
data.as_ptr(), data.as_ptr(),
width as i32, width as i32,
height as i32, height as i32,
stride, stride,
quality as f32, quality as f32,
&mut out_buf, &mut out_buf,
); );
let vec = std::slice::from_raw_parts(out_buf, len).into(); let vec = std::slice::from_raw_parts(out_buf, len).into();
WebPFree(out_buf as *mut _); WebPFree(out_buf as *mut _);
vec vec
}; };
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"