Enable webp thumbnails

This commit is contained in:
asonix 2020-12-03 12:48:52 -06:00
parent 14bea52fae
commit 1a1d280bac
6 changed files with 59 additions and 30 deletions

22
Cargo.lock generated
View file

@ -84,9 +84,9 @@ dependencies = [
[[package]]
name = "actix-macros"
version = "0.1.2"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a60f9ba7c4e6df97f3aacb14bb5c0cd7d98a49dcbaed0d7f292912ad9a6a3ed2"
checksum = "b4ca8ce00b267af8ccebbd647de0d61e0674b6e61185cc7a592ff88772bed655"
dependencies = [
"quote",
"syn",
@ -213,9 +213,9 @@ dependencies = [
[[package]]
name = "actix-web"
version = "3.3.1"
version = "3.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d6d0a6ae7ff7290372b3f636b9fc38b76dfbfc395187ce21e5b95471f7ccab9"
checksum = "e641d4a172e7faa0862241a20ff4f1f5ab0ab7c279f00c2d4587b77483477b86"
dependencies = [
"actix-codec",
"actix-http",
@ -433,9 +433,9 @@ checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
name = "bytecount"
version = "0.6.1"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c39a773ba75db12126d8d383f1bdbf7eb92ea47ec27dd0557aff1fedf172764c"
checksum = "72feb31ffc86498dacdbd0fcebb56138e7177a8cc5cea4516031d15ae85a742e"
[[package]]
name = "byteorder"
@ -1045,9 +1045,9 @@ dependencies = [
[[package]]
name = "mio"
version = "0.6.22"
version = "0.6.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4"
dependencies = [
"cfg-if 0.1.10",
"fuchsia-zircon",
@ -1221,7 +1221,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pict-rs-proxy"
version = "0.2.2"
version = "0.2.3"
dependencies = [
"actix-rt",
"actix-web",
@ -1533,9 +1533,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.59"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779"
dependencies = [
"itoa",
"ryu",

View file

@ -1,6 +1,6 @@
[package]
name = "pict-rs-proxy"
version = "0.2.2"
version = "0.2.3"
authors = ["asonix <asonix@asonix.dog>"]
license = "AGPL-3.0"
readme = "README.md"

View file

@ -67,9 +67,9 @@ impl Config {
url.to_string()
}
fn upstream_thumbnail_url(&self, size: u64, name: &str) -> String {
fn upstream_thumbnail_url(&self, size: u64, name: &str, filetype: FileType) -> String {
let mut url = self.upstream.clone();
url.set_path("image/process.jpg");
url.set_path(&format!("image/process.{}", filetype.as_str()));
url.set_query(Some(&format!("src={}&thumbnail={}", name, size)));
url.to_string()
@ -89,9 +89,9 @@ impl Config {
url.to_string()
}
fn thumbnail_url(&self, size: u64, name: &str) -> String {
fn thumbnail_url(&self, size: u64, name: &str, filetype: FileType) -> String {
let mut url = self.domain.clone();
url.set_path(&format!("thumb/{}/{}", size, name));
url.set_path(&format!("thumb/{}/{}/{}", size, filetype.as_str(), name));
url.to_string()
}
@ -123,6 +123,23 @@ impl Config {
static CONFIG: Lazy<Config> = Lazy::new(|| Config::from_args());
#[derive(serde::Deserialize)]
enum FileType {
#[serde(rename = "jpg")]
Jpg,
#[serde(rename = "webp")]
Webp,
}
impl FileType {
fn as_str(&self) -> &'static str {
match self {
Self::Jpg => "jpg",
Self::Webp => "webp",
}
}
}
#[derive(Debug, serde::Deserialize)]
pub struct Images {
msg: String,
@ -162,8 +179,8 @@ impl Image {
CONFIG.thumbnails_url(&self.file)
}
fn thumb(&self, size: u64) -> String {
CONFIG.thumbnail_url(size, &self.file)
fn thumb(&self, size: u64, filetype: FileType) -> String {
CONFIG.thumbnail_url(size, &self.file, filetype)
}
fn delete(&self) -> String {
@ -306,14 +323,14 @@ async fn image(
}
async fn thumbnail(
parts: web::Path<(u64, String)>,
parts: web::Path<(u64, FileType, String)>,
req: HttpRequest,
client: web::Data<Client>,
) -> Result<HttpResponse, Error> {
let (size, file) = parts.into_inner();
let (size, filetype, file) = parts.into_inner();
if valid_thumbnail_size(size) {
let url = CONFIG.upstream_thumbnail_url(size, &file);
let url = CONFIG.upstream_thumbnail_url(size, &file, filetype);
return image(url, req, client).await;
}
@ -443,7 +460,10 @@ async fn main() -> Result<(), anyhow::Error> {
.service(web::resource("/upload").route(web::post().to(upload)))
.service(web::resource("/image/{filename}").route(web::get().to(full_res)))
.service(web::resource("thumbnails").route(web::get().to(thumbs)))
.service(web::resource("/thumb/{size}/{filename}").route(web::get().to(thumbnail)))
.service(
web::resource("/thumb/{size}/{filetype}/{filename}")
.route(web::get().to(thumbnail)),
)
.service(web::resource("/static/{filename}").route(web::get().to(static_files)))
.service(web::resource("/delete").route(web::get().to(delete)))
.service(web::resource("/404").route(web::get().to(not_found)))

View file

@ -1,5 +1,5 @@
@use super::{layout_html, return_home_html, statics::images_css};
@use crate::Image;
@use crate::{Image, FileType};
@(image: &Image)
@ -13,7 +13,10 @@
</article>
<article>
<div class="imagebox">
<img src="@image.thumb(800)" alt="@image.filename()" title="@image.filename()" />
<picture>
<source type="image/webp" srcset="@image.thumb(800, FileType::Webp)" />
<img src="@image.thumb(800, FileType::Jpg)" alt="@image.filename()" title="@image.filename()" />
</picture>
</div>
<p>
<a href="@image.confirm_delete()">Delete @image.filename()</a>

View file

@ -1,5 +1,5 @@
@use super::{layout_html, return_home_html, statics::images_css};
@use crate::Images;
@use crate::{Images, FileType};
@(images: Images)
@ -24,7 +24,10 @@
<li>
<article>
<div class="imagebox">
<img src="@image.thumb(800)" alt="@image.filename()" title="@image.filename()" />
<picture>
<source type="image/webp" srcset="@image.thumb(800, FileType::Webp)" />
<img src="@image.thumb(800, FileType::Jpg)" alt="@image.filename()" title="@image.filename()" />
</picture>
</div>
<p>
Link:<br />

View file

@ -1,5 +1,5 @@
@use super::{layout_html, return_home_html, statics::images_css};
@use crate::Image;
@use crate::{Image, FileType};
@(image: Image, sizes: &[u64])
@ -20,15 +20,18 @@
@for size in sizes {
<li>
<article>
<img src="@image.thumb(*size)" alt="@image.filename()" title="@image.filename()" />
<picture>
<source type="image/webp" srcset="@image.thumb(*size, FileType::Webp)" />
<img src="@image.thumb(*size, FileType::Jpg)" alt="@image.filename()" title="@image.filename()" />
</picture>
<p>@size x @size</p>
<p>
<a
href="@image.thumb(*size)"
href="@image.thumb(*size, FileType::Jpg)"
target="_blank"
rel="noopener noreferrer"
>
@image.thumb(*size)
@image.thumb(*size, FileType::Jpg)
</a>
</p>
</article>