Browse Source

upgrade backend to use pictrsv2

feature/pictrs-v2.5
ryexandrite 10 months ago
parent
commit
b6137c2cdd
  1. 2
      ansible/templates/docker-compose.yml
  2. 2
      docker/dev/docker-compose.yml
  3. 2
      docker/federation/docker-compose.yml
  4. 4
      docker/prod/docker-compose.yml
  5. 113
      server/src/routes/images.rs

2
ansible/templates/docker-compose.yml

@ -26,7 +26,7 @@ services:
restart: always
pictrs:
image: asonix/pictrs:amd64-v0.1.0-r9
image: asonix/pictrs:v0.2.5-r0
user: 991:991
ports:
- "127.0.0.1:8537:8080"

2
docker/dev/docker-compose.yml

@ -30,7 +30,7 @@ services:
restart: always
pictrs:
image: asonix/pictrs:v0.1.13-r0
image: asonix/pictrs:v0.2.5-r0
ports:
- "8537:8080"
user: 991:991

2
docker/federation/docker-compose.yml

@ -23,7 +23,7 @@ services:
pictrs:
restart: always
image: asonix/pictrs:v0.1.13-r0
image: asonix/pictrs:v0.2.5-r0
user: 991:991
volumes:
- ./volumes/pictrs_alpha:/mnt

4
docker/prod/docker-compose.yml

@ -26,8 +26,8 @@ services:
- iframely
pictrs:
image: asonix/pictrs:v0.1.13-r0
ports:
image: asonix/pictrs:v0.2.5-r0
ports:
- "127.0.0.1:8537:8080"
user: 991:991
volumes:

113
server/src/routes/images.rs

@ -1,6 +1,7 @@
use actix::clock::Duration;
use actix_web::{body::BodyStream, http::StatusCode, *};
use awc::Client;
use crate::{api::claims::Claims};
use lemmy_rate_limit::RateLimit;
use lemmy_utils::settings::Settings;
use serde::{Deserialize, Serialize};
@ -18,39 +19,51 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.wrap(rate_limit.image())
.route(web::post().to(upload)),
)
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
.service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
.service(
web::resource("/pictrs/image/thumbnail{size}/{filename}").route(web::get().to(thumbnail)),
)
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Image {
struct Image {
file: String,
delete_token: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Images {
struct Images {
msg: String,
files: Option<Vec<Image>>,
}
#[derive(Deserialize)]
struct PictrsParams {
format: Option<String>,
thumbnail: Option<String>,
}
async fn upload(
req: HttpRequest,
body: web::Payload,
client: web::Data<Client>,
) -> Result<HttpResponse, Error> {
// TODO: check auth and rate limit here
// TODO: check rate limit here
let jwt = req
.cookie("jwt")
.expect("No auth header for picture upload");
if Claims::decode(jwt.value()).is_err() {
return Ok(HttpResponse::Unauthorized().finish());
};
let mut client_req =
client.request_from(format!("{}/image", Settings::get().pictrs_url), req.head());
if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string())
};
let mut res = client
.request_from(format!("{}/image", Settings::get().pictrs_url), req.head())
.if_some(req.head().peer_addr, |addr, req| {
req.header("X-Forwarded-For", addr.to_string())
})
.send_stream(body)
.await?;
let mut res = client_req.send_stream(body).await?;
let images = res.json::<Images>().await?;
@ -59,30 +72,31 @@ async fn upload(
async fn full_res(
filename: web::Path<String>,
web::Query(params): web::Query<PictrsParams>,
req: HttpRequest,
client: web::Data<Client>,
) -> Result<HttpResponse, Error> {
let url = format!(
"{}/image/{}",
Settings::get().pictrs_url,
&filename.into_inner()
);
image(url, req, client).await
}
async fn thumbnail(
parts: web::Path<(u64, String)>,
req: HttpRequest,
client: web::Data<Client>,
) -> Result<HttpResponse, Error> {
let (size, file) = parts.into_inner();
let url = format!(
"{}/image/thumbnail{}/{}",
Settings::get().pictrs_url,
size,
&file
);
let name = &filename.into_inner();
// If there are no query params, the URL is original
let url = if params.format.is_none() && params.thumbnail.is_none() {
format!("{}/image/original/{}", Settings::get().pictrs_url, name,)
} else {
// Use jpg as a default when none is given
let format = params.format.unwrap_or_else(|| "jpg".to_string());
let mut url = format!(
"{}/image/process.{}?src={}",
Settings::get().pictrs_url,
format,
name,
);
if let Some(size) = params.thumbnail {
url = format!("{}&thumbnail={}", url, size,);
}
url
};
image(url, req, client).await
}
@ -92,14 +106,13 @@ async fn image(
req: HttpRequest,
client: web::Data<Client>,
) -> Result<HttpResponse, Error> {
let res = client
.request_from(url, req.head())
.if_some(req.head().peer_addr, |addr, req| {
req.header("X-Forwarded-For", addr.to_string())
})
.no_decompress()
.send()
.await?;
let mut client_req = client.request_from(url, req.head());
if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string())
};
let res = client_req.no_decompress().send().await?;
if res.status() == StatusCode::NOT_FOUND {
return Ok(HttpResponse::NotFound().finish());
@ -127,14 +140,14 @@ async fn delete(
&token,
&file
);
let res = client
.request_from(url, req.head())
.if_some(req.head().peer_addr, |addr, req| {
req.header("X-Forwarded-For", addr.to_string())
})
.no_decompress()
.send()
.await?;
let mut client_req = client.request_from(url, req.head());
if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string())
};
let res = client_req.no_decompress().send().await?;
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
}
Loading…
Cancel
Save