1 use actix_http::http::header::ACCEPT_ENCODING;
2 use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
5 use lemmy_db_queries::source::secret::SecretSingleton;
6 use lemmy_db_schema::source::secret::Secret;
7 use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings, LemmyError};
8 use serde::{Deserialize, Serialize};
9 use std::time::Duration;
11 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
12 let client = Client::builder()
13 .header("User-Agent", "pict-rs-frontend, v0.1.0")
14 .timeout(Duration::from_secs(30))
18 .app_data(Data::new(client))
20 web::resource("/pictrs/image")
21 .wrap(rate_limit.image())
22 .route(web::post().to(upload)),
24 // This has optional query params: /image/{filename}?format=jpg&thumbnail=256
25 .service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
26 .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
29 #[derive(Debug, Serialize, Deserialize)]
35 #[derive(Debug, Serialize, Deserialize)]
38 files: Option<Vec<Image>>,
41 #[derive(Deserialize)]
43 format: Option<String>,
44 thumbnail: Option<String>,
50 client: web::Data<Client>,
51 ) -> Result<HttpResponse, Error> {
52 // TODO: check rate limit here
55 .expect("No auth header for picture upload");
57 let jwt_secret = Secret::get().jwt_secret;
58 if Claims::decode(jwt.value(), &jwt_secret).is_err() {
59 return Ok(HttpResponse::Unauthorized().finish());
62 let mut client_req = client.request_from(format!("{}/image", pictrs_url()?), req.head());
63 // remove content-encoding header so that pictrs doesnt send gzipped response
64 client_req.headers_mut().remove(ACCEPT_ENCODING);
66 if let Some(addr) = req.head().peer_addr {
67 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
70 let mut res = client_req
73 .map_err(error::ErrorBadRequest)?;
75 let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?;
77 Ok(HttpResponse::build(res.status()).json(images))
81 filename: web::Path<String>,
82 web::Query(params): web::Query<PictrsParams>,
84 client: web::Data<Client>,
85 ) -> Result<HttpResponse, Error> {
86 let name = &filename.into_inner();
88 // If there are no query params, the URL is original
89 let url = if params.format.is_none() && params.thumbnail.is_none() {
90 format!("{}/image/original/{}", pictrs_url()?, name,)
92 // Use jpg as a default when none is given
93 let format = params.format.unwrap_or_else(|| "jpg".to_string());
95 let mut url = format!("{}/image/process.{}?src={}", pictrs_url()?, format, name,);
97 if let Some(size) = params.thumbnail {
98 url = format!("{}&thumbnail={}", url, size,);
103 image(url, req, client).await
109 client: web::Data<Client>,
110 ) -> Result<HttpResponse, Error> {
111 let mut client_req = client.request_from(url, req.head());
112 client_req.headers_mut().remove(ACCEPT_ENCODING);
114 if let Some(addr) = req.head().peer_addr {
115 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
122 .map_err(error::ErrorBadRequest)?;
124 if res.status() == StatusCode::NOT_FOUND {
125 return Ok(HttpResponse::NotFound().finish());
128 let mut client_res = HttpResponse::build(res.status());
130 for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
131 client_res.insert_header((name.clone(), value.clone()));
134 Ok(client_res.body(BodyStream::new(res)))
138 components: web::Path<(String, String)>,
140 client: web::Data<Client>,
141 ) -> Result<HttpResponse, Error> {
142 let (token, file) = components.into_inner();
144 let url = format!("{}/image/delete/{}/{}", pictrs_url()?, &token, &file);
146 let mut client_req = client.request_from(url, req.head());
147 client_req.headers_mut().remove(ACCEPT_ENCODING);
149 if let Some(addr) = req.head().peer_addr {
150 client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
157 .map_err(error::ErrorBadRequest)?;
159 Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
162 fn pictrs_url() -> Result<String, LemmyError> {
165 .ok_or_else(|| anyhow!("images_disabled").into())