1 use actix_http::http::header::ACCEPT_ENCODING;
2 use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
5 use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings, LemmyError};
6 use serde::{Deserialize, Serialize};
7 use std::time::Duration;
9 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
10 let client = Client::builder()
11 .header("User-Agent", "pict-rs-frontend, v0.1.0")
12 .timeout(Duration::from_secs(30))
16 .app_data(Data::new(client))
18 web::resource("/pictrs/image")
19 .wrap(rate_limit.image())
20 .route(web::post().to(upload)),
22 // This has optional query params: /image/{filename}?format=jpg&thumbnail=256
23 .service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
24 .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
27 #[derive(Debug, Serialize, Deserialize)]
33 #[derive(Debug, Serialize, Deserialize)]
36 files: Option<Vec<Image>>,
39 #[derive(Deserialize)]
41 format: Option<String>,
42 thumbnail: Option<String>,
48 client: web::Data<Client>,
49 ) -> Result<HttpResponse, Error> {
50 // TODO: check rate limit here
53 .expect("No auth header for picture upload");
55 if Claims::decode(jwt.value()).is_err() {
56 return Ok(HttpResponse::Unauthorized().finish());
59 let mut client_req = client.request_from(format!("{}/image", pictrs_url()?), req.head());
60 // remove content-encoding header so that pictrs doesnt send gzipped response
61 client_req.headers_mut().remove(ACCEPT_ENCODING);
63 if let Some(addr) = req.head().peer_addr {
64 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
67 let mut res = client_req
70 .map_err(error::ErrorBadRequest)?;
72 let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?;
74 Ok(HttpResponse::build(res.status()).json(images))
78 filename: web::Path<String>,
79 web::Query(params): web::Query<PictrsParams>,
81 client: web::Data<Client>,
82 ) -> Result<HttpResponse, Error> {
83 let name = &filename.into_inner();
85 // If there are no query params, the URL is original
86 let url = if params.format.is_none() && params.thumbnail.is_none() {
87 format!("{}/image/original/{}", pictrs_url()?, name,)
89 // Use jpg as a default when none is given
90 let format = params.format.unwrap_or_else(|| "jpg".to_string());
92 let mut url = format!("{}/image/process.{}?src={}", pictrs_url()?, format, name,);
94 if let Some(size) = params.thumbnail {
95 url = format!("{}&thumbnail={}", url, size,);
100 image(url, req, client).await
106 client: web::Data<Client>,
107 ) -> Result<HttpResponse, Error> {
108 let mut client_req = client.request_from(url, req.head());
109 client_req.headers_mut().remove(ACCEPT_ENCODING);
111 if let Some(addr) = req.head().peer_addr {
112 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
119 .map_err(error::ErrorBadRequest)?;
121 if res.status() == StatusCode::NOT_FOUND {
122 return Ok(HttpResponse::NotFound().finish());
125 let mut client_res = HttpResponse::build(res.status());
127 for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
128 client_res.insert_header((name.clone(), value.clone()));
131 Ok(client_res.body(BodyStream::new(res)))
135 components: web::Path<(String, String)>,
137 client: web::Data<Client>,
138 ) -> Result<HttpResponse, Error> {
139 let (token, file) = components.into_inner();
141 let url = format!("{}/image/delete/{}/{}", pictrs_url()?, &token, &file);
143 let mut client_req = client.request_from(url, req.head());
144 client_req.headers_mut().remove(ACCEPT_ENCODING);
146 if let Some(addr) = req.head().peer_addr {
147 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
154 .map_err(error::ErrorBadRequest)?;
156 Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
159 fn pictrs_url() -> Result<String, LemmyError> {
162 .ok_or_else(|| anyhow!("images_disabled").into())