]> Untitled Git - lemmy.git/blob - crates/routes/src/images.rs
Upgrading deps, running clippy fix on nightly 1.55.0 (#1638)
[lemmy.git] / crates / routes / src / images.rs
1 use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
2 use awc::Client;
3 use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings};
4 use serde::{Deserialize, Serialize};
5 use std::time::Duration;
6
7 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
8   let client = Client::builder()
9     .header("User-Agent", "pict-rs-frontend, v0.1.0")
10     .timeout(Duration::from_secs(30))
11     .finish();
12
13   cfg
14     .app_data(Data::new(client))
15     .service(
16       web::resource("/pictrs/image")
17         .wrap(rate_limit.image())
18         .route(web::post().to(upload)),
19     )
20     // This has optional query params: /image/{filename}?format=jpg&thumbnail=256
21     .service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
22     .service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
23 }
24
25 #[derive(Debug, Serialize, Deserialize)]
26 struct Image {
27   file: String,
28   delete_token: String,
29 }
30
31 #[derive(Debug, Serialize, Deserialize)]
32 struct Images {
33   msg: String,
34   files: Option<Vec<Image>>,
35 }
36
37 #[derive(Deserialize)]
38 struct PictrsParams {
39   format: Option<String>,
40   thumbnail: Option<String>,
41 }
42
43 async fn upload(
44   req: HttpRequest,
45   body: web::Payload,
46   client: web::Data<Client>,
47 ) -> Result<HttpResponse, Error> {
48   // TODO: check rate limit here
49   let jwt = req
50     .cookie("jwt")
51     .expect("No auth header for picture upload");
52
53   if Claims::decode(jwt.value()).is_err() {
54     return Ok(HttpResponse::Unauthorized().finish());
55   };
56
57   let mut client_req = client.request_from(
58     format!("{}/image", Settings::get().pictrs_url()),
59     req.head(),
60   );
61
62   if let Some(addr) = req.head().peer_addr {
63     client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
64   };
65
66   let mut res = client_req
67     .send_stream(body)
68     .await
69     .map_err(error::ErrorBadRequest)?;
70
71   let images = res.json::<Images>().await.map_err(error::ErrorBadRequest)?;
72
73   Ok(HttpResponse::build(res.status()).json(images))
74 }
75
76 async fn full_res(
77   filename: web::Path<String>,
78   web::Query(params): web::Query<PictrsParams>,
79   req: HttpRequest,
80   client: web::Data<Client>,
81 ) -> Result<HttpResponse, Error> {
82   let name = &filename.into_inner();
83
84   // If there are no query params, the URL is original
85   let url = if params.format.is_none() && params.thumbnail.is_none() {
86     format!("{}/image/original/{}", Settings::get().pictrs_url(), name,)
87   } else {
88     // Use jpg as a default when none is given
89     let format = params.format.unwrap_or_else(|| "jpg".to_string());
90
91     let mut url = format!(
92       "{}/image/process.{}?src={}",
93       Settings::get().pictrs_url(),
94       format,
95       name,
96     );
97
98     if let Some(size) = params.thumbnail {
99       url = format!("{}&thumbnail={}", url, size,);
100     }
101     url
102   };
103
104   image(url, req, client).await
105 }
106
107 async fn image(
108   url: String,
109   req: HttpRequest,
110   client: web::Data<Client>,
111 ) -> Result<HttpResponse, Error> {
112   let mut client_req = client.request_from(url, req.head());
113
114   if let Some(addr) = req.head().peer_addr {
115     client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
116   };
117
118   let res = client_req
119     .no_decompress()
120     .send()
121     .await
122     .map_err(error::ErrorBadRequest)?;
123
124   if res.status() == StatusCode::NOT_FOUND {
125     return Ok(HttpResponse::NotFound().finish());
126   }
127
128   let mut client_res = HttpResponse::build(res.status());
129
130   for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
131     client_res.insert_header((name.clone(), value.clone()));
132   }
133
134   Ok(client_res.body(BodyStream::new(res)))
135 }
136
137 async fn delete(
138   components: web::Path<(String, String)>,
139   req: HttpRequest,
140   client: web::Data<Client>,
141 ) -> Result<HttpResponse, Error> {
142   let (token, file) = components.into_inner();
143
144   let url = format!(
145     "{}/image/delete/{}/{}",
146     Settings::get().pictrs_url(),
147     &token,
148     &file
149   );
150
151   let mut client_req = client.request_from(url, req.head());
152
153   if let Some(addr) = req.head().peer_addr {
154     client_req = client_req.insert_header(("X-Forwarded-For", addr.to_string()))
155   };
156
157   let res = client_req
158     .no_decompress()
159     .send()
160     .await
161     .map_err(error::ErrorBadRequest)?;
162
163   Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
164 }