]> Untitled Git - lemmy.git/blob - crates/routes/src/images.rs
Merge pull request 'Explicitly mark posts and comments as public (ref #1220)' (#167...
[lemmy.git] / crates / routes / src / images.rs
1 use actix::clock::Duration;
2 use actix_web::{body::BodyStream, http::StatusCode, *};
3 use awc::Client;
4 use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::Settings};
5 use serde::{Deserialize, Serialize};
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     .data(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 =
58     client.request_from(format!("{}/image", Settings::get().pictrs_url), req.head());
59
60   if let Some(addr) = req.head().peer_addr {
61     client_req = client_req.header("X-Forwarded-For", addr.to_string())
62   };
63
64   let mut res = client_req.send_stream(body).await?;
65
66   let images = res.json::<Images>().await?;
67
68   Ok(HttpResponse::build(res.status()).json(images))
69 }
70
71 async fn full_res(
72   filename: web::Path<String>,
73   web::Query(params): web::Query<PictrsParams>,
74   req: HttpRequest,
75   client: web::Data<Client>,
76 ) -> Result<HttpResponse, Error> {
77   let name = &filename.into_inner();
78
79   // If there are no query params, the URL is original
80   let url = if params.format.is_none() && params.thumbnail.is_none() {
81     format!("{}/image/original/{}", Settings::get().pictrs_url, name,)
82   } else {
83     // Use jpg as a default when none is given
84     let format = params.format.unwrap_or_else(|| "jpg".to_string());
85
86     let mut url = format!(
87       "{}/image/process.{}?src={}",
88       Settings::get().pictrs_url,
89       format,
90       name,
91     );
92
93     if let Some(size) = params.thumbnail {
94       url = format!("{}&thumbnail={}", url, size,);
95     }
96     url
97   };
98
99   image(url, req, client).await
100 }
101
102 async fn image(
103   url: String,
104   req: HttpRequest,
105   client: web::Data<Client>,
106 ) -> Result<HttpResponse, Error> {
107   let mut client_req = client.request_from(url, req.head());
108
109   if let Some(addr) = req.head().peer_addr {
110     client_req = client_req.header("X-Forwarded-For", addr.to_string())
111   };
112
113   let res = client_req.no_decompress().send().await?;
114
115   if res.status() == StatusCode::NOT_FOUND {
116     return Ok(HttpResponse::NotFound().finish());
117   }
118
119   let mut client_res = HttpResponse::build(res.status());
120
121   for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
122     client_res.header(name.clone(), value.clone());
123   }
124
125   Ok(client_res.body(BodyStream::new(res)))
126 }
127
128 async fn delete(
129   components: web::Path<(String, String)>,
130   req: HttpRequest,
131   client: web::Data<Client>,
132 ) -> Result<HttpResponse, Error> {
133   let (token, file) = components.into_inner();
134
135   let url = format!(
136     "{}/image/delete/{}/{}",
137     Settings::get().pictrs_url,
138     &token,
139     &file
140   );
141
142   let mut client_req = client.request_from(url, req.head());
143
144   if let Some(addr) = req.head().peer_addr {
145     client_req = client_req.header("X-Forwarded-For", addr.to_string())
146   };
147
148   let res = client_req.no_decompress().send().await?;
149
150   Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
151 }