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, LemmyError};
6 use lemmy_websocket::LemmyContext;
7 use serde::{Deserialize, Serialize};
8 use std::time::Duration;
10 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
11 let client = Client::builder()
12 .header("User-Agent", "pict-rs-frontend, v0.1.0")
13 .timeout(Duration::from_secs(30))
17 .app_data(Data::new(client))
19 web::resource("/pictrs/image")
20 .wrap(rate_limit.image())
21 .route(web::post().to(upload)),
23 // This has optional query params: /image/{filename}?format=jpg&thumbnail=256
24 .service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
25 .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
28 #[derive(Debug, Serialize, Deserialize)]
34 #[derive(Debug, Serialize, Deserialize)]
37 files: Option<Vec<Image>>,
40 #[derive(Deserialize)]
42 format: Option<String>,
43 thumbnail: Option<String>,
49 client: web::Data<Client>,
50 context: web::Data<LemmyContext>,
51 ) -> Result<HttpResponse, Error> {
52 // TODO: check rate limit here
55 .expect("No auth header for picture upload");
57 if Claims::decode(jwt.value(), &context.secret().jwt_secret).is_err() {
58 return Ok(HttpResponse::Unauthorized().finish());
61 let mut client_req = client.request_from(
62 format!("{}/image", pictrs_url(context.settings().pictrs_url)?),
65 // remove content-encoding header so that pictrs doesnt send gzipped response
66 client_req.headers_mut().remove(ACCEPT_ENCODING);
68 if let Some(addr) = req.head().peer_addr {
69 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
72 let mut res = client_req
75 .map_err(error::ErrorBadRequest)?;
77 let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?;
79 Ok(HttpResponse::build(res.status()).json(images))
83 filename: web::Path<String>,
84 web::Query(params): web::Query<PictrsParams>,
86 client: web::Data<Client>,
87 context: web::Data<LemmyContext>,
88 ) -> Result<HttpResponse, Error> {
89 let name = &filename.into_inner();
91 // If there are no query params, the URL is original
92 let pictrs_url_settings = context.settings().pictrs_url;
93 let url = if params.format.is_none() && params.thumbnail.is_none() {
95 "{}/image/original/{}",
96 pictrs_url(pictrs_url_settings)?,
100 // Use jpg as a default when none is given
101 let format = params.format.unwrap_or_else(|| "jpg".to_string());
103 let mut url = format!(
104 "{}/image/process.{}?src={}",
105 pictrs_url(pictrs_url_settings)?,
110 if let Some(size) = params.thumbnail {
111 url = format!("{}&thumbnail={}", url, size,);
116 image(url, req, client).await
122 client: web::Data<Client>,
123 ) -> Result<HttpResponse, Error> {
124 let mut client_req = client.request_from(url, req.head());
125 client_req.headers_mut().remove(ACCEPT_ENCODING);
127 if let Some(addr) = req.head().peer_addr {
128 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
135 .map_err(error::ErrorBadRequest)?;
137 if res.status() == StatusCode::NOT_FOUND {
138 return Ok(HttpResponse::NotFound().finish());
141 let mut client_res = HttpResponse::build(res.status());
143 for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
144 client_res.insert_header((name.clone(), value.clone()));
147 Ok(client_res.body(BodyStream::new(res)))
151 components: web::Path<(String, String)>,
153 client: web::Data<Client>,
154 context: web::Data<LemmyContext>,
155 ) -> Result<HttpResponse, Error> {
156 let (token, file) = components.into_inner();
159 "{}/image/delete/{}/{}",
160 pictrs_url(context.settings().pictrs_url)?,
165 let mut client_req = client.request_from(url, req.head());
166 client_req.headers_mut().remove(ACCEPT_ENCODING);
168 if let Some(addr) = req.head().peer_addr {
169 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
176 .map_err(error::ErrorBadRequest)?;
178 Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
181 fn pictrs_url(pictrs_url: Option<String>) -> Result<String, LemmyError> {
182 pictrs_url.ok_or_else(|| anyhow!("images_disabled").into())