2 header::{HeaderName, HOST},
3 http::header::ACCEPT_ENCODING,
5 use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
7 use futures::stream::{Stream, StreamExt};
8 use lemmy_utils::{claims::Claims, rate_limit::RateLimit, LemmyError};
9 use lemmy_websocket::LemmyContext;
11 use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
12 use serde::{Deserialize, Serialize};
13 use std::time::Duration;
15 pub fn config(cfg: &mut web::ServiceConfig, client: ClientWithMiddleware, rate_limit: &RateLimit) {
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>,
47 request: &HttpRequest,
48 client: &ClientWithMiddleware,
51 // remove accept-encoding header so that pictrs doesnt compress the response
52 const INVALID_HEADERS: &[HeaderName] = &[ACCEPT_ENCODING, HOST];
54 let client_request = client
55 .request(request.method().clone(), url)
56 .timeout(Duration::from_secs(30));
61 .fold(client_request, |client_req, (key, value)| {
62 if INVALID_HEADERS.contains(key) {
65 client_req.header(key, value)
73 client: web::Data<ClientWithMiddleware>,
74 context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse, Error> {
76 // TODO: check rate limit here
79 .expect("No auth header for picture upload");
81 if Claims::decode(jwt.value(), &context.secret().jwt_secret).is_err() {
82 return Ok(HttpResponse::Unauthorized().finish());
85 let image_url = format!("{}/image", pictrs_url(context.settings().pictrs_url)?);
87 let mut client_req = adapt_request(&req, &client, image_url);
89 if let Some(addr) = req.head().peer_addr {
90 client_req = client_req.header("X-Forwarded-For", addr.to_string())
94 .body(Body::wrap_stream(make_send(body)))
97 .map_err(error::ErrorBadRequest)?;
99 let status = res.status();
100 let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?;
102 Ok(HttpResponse::build(status).json(images))
106 filename: web::Path<String>,
107 web::Query(params): web::Query<PictrsParams>,
109 client: web::Data<ClientWithMiddleware>,
110 context: web::Data<LemmyContext>,
111 ) -> Result<HttpResponse, Error> {
112 let name = &filename.into_inner();
114 // If there are no query params, the URL is original
115 let pictrs_url_settings = context.settings().pictrs_url;
116 let url = if params.format.is_none() && params.thumbnail.is_none() {
118 "{}/image/original/{}",
119 pictrs_url(pictrs_url_settings)?,
123 // Use jpg as a default when none is given
124 let format = params.format.unwrap_or_else(|| "jpg".to_string());
126 let mut url = format!(
127 "{}/image/process.{}?src={}",
128 pictrs_url(pictrs_url_settings)?,
133 if let Some(size) = params.thumbnail {
134 url = format!("{}&thumbnail={}", url, size,);
139 image(url, req, client).await
145 client: web::Data<ClientWithMiddleware>,
146 ) -> Result<HttpResponse, Error> {
147 let mut client_req = adapt_request(&req, &client, url);
149 if let Some(addr) = req.head().peer_addr {
150 client_req = client_req.header("X-Forwarded-For", addr.to_string());
153 if let Some(addr) = req.head().peer_addr {
154 client_req = client_req.header("X-Forwarded-For", addr.to_string());
157 let res = client_req.send().await.map_err(error::ErrorBadRequest)?;
159 if res.status() == StatusCode::NOT_FOUND {
160 return Ok(HttpResponse::NotFound().finish());
163 let mut client_res = HttpResponse::build(res.status());
165 for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
166 client_res.insert_header((name.clone(), value.clone()));
169 Ok(client_res.body(BodyStream::new(res.bytes_stream())))
173 components: web::Path<(String, String)>,
175 client: web::Data<ClientWithMiddleware>,
176 context: web::Data<LemmyContext>,
177 ) -> Result<HttpResponse, Error> {
178 let (token, file) = components.into_inner();
181 "{}/image/delete/{}/{}",
182 pictrs_url(context.settings().pictrs_url)?,
187 let mut client_req = adapt_request(&req, &client, url);
189 if let Some(addr) = req.head().peer_addr {
190 client_req = client_req.header("X-Forwarded-For", addr.to_string());
193 let res = client_req.send().await.map_err(error::ErrorBadRequest)?;
195 Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
198 fn pictrs_url(pictrs_url: Option<String>) -> Result<String, LemmyError> {
199 pictrs_url.ok_or_else(|| anyhow!("images_disabled").into())
202 fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static
204 S: Stream + Unpin + 'static,
207 // NOTE: the 8 here is arbitrary
208 let (tx, rx) = tokio::sync::mpsc::channel(8);
210 // NOTE: spawning stream into a new task can potentially hit this bug:
211 // - https://github.com/actix/actix-web/issues/1679
213 // Since 4.0.0-beta.2 this issue is incredibly less frequent. I have not personally reproduced it.
214 // That said, it is still technically possible to encounter.
215 actix_web::rt::spawn(async move {
216 while let Some(res) = stream.next().await {
217 if tx.send(res).await.is_err() {
226 struct SendStream<T> {
227 rx: tokio::sync::mpsc::Receiver<T>,
230 impl<T> Stream for SendStream<T>
237 mut self: std::pin::Pin<&mut Self>,
238 cx: &mut std::task::Context<'_>,
239 ) -> std::task::Poll<Option<Self::Item>> {
240 std::pin::Pin::new(&mut self.rx).poll_recv(cx)