1 use actix::clock::Duration;
2 use actix_web::{body::BodyStream, http::StatusCode, *};
4 use lemmy_api::claims::Claims;
5 use lemmy_utils::{rate_limit::RateLimit, settings::Settings};
6 use serde::{Deserialize, Serialize};
8 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
9 let client = Client::builder()
10 .header("User-Agent", "pict-rs-frontend, v0.1.0")
11 .timeout(Duration::from_secs(30))
17 web::resource("/pictrs/image")
18 .wrap(rate_limit.image())
19 .route(web::post().to(upload)),
21 // This has optional query params: /image/{filename}?format=jpg&thumbnail=256
22 .service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
23 .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
26 #[derive(Debug, Serialize, Deserialize)]
32 #[derive(Debug, Serialize, Deserialize)]
35 files: Option<Vec<Image>>,
38 #[derive(Deserialize)]
40 format: Option<String>,
41 thumbnail: Option<String>,
47 client: web::Data<Client>,
48 ) -> Result<HttpResponse, Error> {
49 // TODO: check rate limit here
52 .expect("No auth header for picture upload");
54 if Claims::decode(jwt.value()).is_err() {
55 return Ok(HttpResponse::Unauthorized().finish());
59 client.request_from(format!("{}/image", Settings::get().pictrs_url), req.head());
61 if let Some(addr) = req.head().peer_addr {
62 client_req = client_req.header("X-Forwarded-For", addr.to_string())
65 let mut res = client_req.send_stream(body).await?;
67 let images = res.json::<Images>().await?;
69 Ok(HttpResponse::build(res.status()).json(images))
73 filename: web::Path<String>,
74 web::Query(params): web::Query<PictrsParams>,
76 client: web::Data<Client>,
77 ) -> Result<HttpResponse, Error> {
78 let name = &filename.into_inner();
80 // If there are no query params, the URL is original
81 let url = if params.format.is_none() && params.thumbnail.is_none() {
82 format!("{}/image/original/{}", Settings::get().pictrs_url, name,)
84 // Use jpg as a default when none is given
85 let format = params.format.unwrap_or_else(|| "jpg".to_string());
87 let mut url = format!(
88 "{}/image/process.{}?src={}",
89 Settings::get().pictrs_url,
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());
110 if let Some(addr) = req.head().peer_addr {
111 client_req = client_req.header("X-Forwarded-For", addr.to_string())
114 let res = client_req.no_decompress().send().await?;
116 if res.status() == StatusCode::NOT_FOUND {
117 return Ok(HttpResponse::NotFound().finish());
120 let mut client_res = HttpResponse::build(res.status());
122 for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
123 client_res.header(name.clone(), value.clone());
126 Ok(client_res.body(BodyStream::new(res)))
130 components: web::Path<(String, String)>,
132 client: web::Data<Client>,
133 ) -> Result<HttpResponse, Error> {
134 let (token, file) = components.into_inner();
137 "{}/image/delete/{}/{}",
138 Settings::get().pictrs_url,
143 let mut client_req = client.request_from(url, req.head());
145 if let Some(addr) = req.head().peer_addr {
146 client_req = client_req.header("X-Forwarded-For", addr.to_string())
149 let res = client_req.no_decompress().send().await?;
151 Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))