2020-08-05 16:00:00 +00:00
|
|
|
use actix::clock::Duration;
|
|
|
|
use actix_web::{body::BodyStream, http::StatusCode, *};
|
|
|
|
use awc::Client;
|
2020-12-01 17:48:39 +00:00
|
|
|
use lemmy_api::claims::Claims;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_utils::{rate_limit::RateLimit, settings::Settings};
|
2020-08-05 16:00:00 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
|
2020-09-12 01:37:25 +00:00
|
|
|
let client = Client::builder()
|
2020-08-05 16:00:00 +00:00
|
|
|
.header("User-Agent", "pict-rs-frontend, v0.1.0")
|
|
|
|
.timeout(Duration::from_secs(30))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
cfg
|
|
|
|
.data(client)
|
|
|
|
.service(
|
|
|
|
web::resource("/pictrs/image")
|
|
|
|
.wrap(rate_limit.image())
|
|
|
|
.route(web::post().to(upload)),
|
|
|
|
)
|
2020-10-14 16:48:10 +00:00
|
|
|
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
|
2020-08-05 16:00:00 +00:00
|
|
|
.service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
|
|
|
|
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-11-16 15:44:04 +00:00
|
|
|
struct Image {
|
2020-08-05 16:00:00 +00:00
|
|
|
file: String,
|
|
|
|
delete_token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-11-16 15:44:04 +00:00
|
|
|
struct Images {
|
2020-08-05 16:00:00 +00:00
|
|
|
msg: String,
|
|
|
|
files: Option<Vec<Image>>,
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:48:10 +00:00
|
|
|
#[derive(Deserialize)]
|
2020-11-16 15:44:04 +00:00
|
|
|
struct PictrsParams {
|
2020-10-14 16:48:10 +00:00
|
|
|
format: Option<String>,
|
|
|
|
thumbnail: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:00:00 +00:00
|
|
|
async fn upload(
|
|
|
|
req: HttpRequest,
|
|
|
|
body: web::Payload,
|
|
|
|
client: web::Data<Client>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-12-01 17:48:39 +00:00
|
|
|
// TODO: check rate limit here
|
|
|
|
let jwt = req
|
|
|
|
.cookie("jwt")
|
|
|
|
.expect("No auth header for picture upload");
|
|
|
|
|
|
|
|
if Claims::decode(jwt.value()).is_err() {
|
|
|
|
return Ok(HttpResponse::Unauthorized().finish());
|
|
|
|
};
|
2020-08-05 16:00:00 +00:00
|
|
|
|
2020-12-04 14:00:15 +00:00
|
|
|
let mut client_req =
|
|
|
|
client.request_from(format!("{}/image", Settings::get().pictrs_url), req.head());
|
|
|
|
|
|
|
|
if let Some(addr) = req.head().peer_addr {
|
|
|
|
client_req = client_req.header("X-Forwarded-For", addr.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = client_req.send_stream(body).await?;
|
2020-08-05 16:00:00 +00:00
|
|
|
|
|
|
|
let images = res.json::<Images>().await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::build(res.status()).json(images))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn full_res(
|
|
|
|
filename: web::Path<String>,
|
2020-10-14 16:48:10 +00:00
|
|
|
web::Query(params): web::Query<PictrsParams>,
|
2020-08-05 16:00:00 +00:00
|
|
|
req: HttpRequest,
|
|
|
|
client: web::Data<Client>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-10-14 16:48:10 +00:00
|
|
|
let name = &filename.into_inner();
|
2020-08-05 16:00:00 +00:00
|
|
|
|
2020-10-14 16:48:10 +00:00
|
|
|
// If there are no query params, the URL is original
|
|
|
|
let url = if params.format.is_none() && params.thumbnail.is_none() {
|
|
|
|
format!("{}/image/original/{}", Settings::get().pictrs_url, name,)
|
|
|
|
} else {
|
|
|
|
// Use jpg as a default when none is given
|
2020-10-16 14:09:37 +00:00
|
|
|
let format = params.format.unwrap_or_else(|| "jpg".to_string());
|
2020-10-14 16:48:10 +00:00
|
|
|
|
|
|
|
let mut url = format!(
|
|
|
|
"{}/image/process.{}?src={}",
|
|
|
|
Settings::get().pictrs_url,
|
|
|
|
format,
|
|
|
|
name,
|
|
|
|
);
|
2020-08-05 16:00:00 +00:00
|
|
|
|
2020-10-14 16:48:10 +00:00
|
|
|
if let Some(size) = params.thumbnail {
|
|
|
|
url = format!("{}&thumbnail={}", url, size,);
|
|
|
|
}
|
|
|
|
url
|
|
|
|
};
|
2020-08-05 16:00:00 +00:00
|
|
|
|
2020-08-05 17:53:59 +00:00
|
|
|
image(url, req, client).await
|
2020-08-05 16:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn image(
|
|
|
|
url: String,
|
|
|
|
req: HttpRequest,
|
|
|
|
client: web::Data<Client>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-12-04 14:00:15 +00:00
|
|
|
let mut client_req = client.request_from(url, req.head());
|
|
|
|
|
|
|
|
if let Some(addr) = req.head().peer_addr {
|
|
|
|
client_req = client_req.header("X-Forwarded-For", addr.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = client_req.no_decompress().send().await?;
|
2020-08-05 16:00:00 +00:00
|
|
|
|
|
|
|
if res.status() == StatusCode::NOT_FOUND {
|
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut client_res = HttpResponse::build(res.status());
|
|
|
|
|
|
|
|
for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
|
|
|
|
client_res.header(name.clone(), value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(client_res.body(BodyStream::new(res)))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(
|
|
|
|
components: web::Path<(String, String)>,
|
|
|
|
req: HttpRequest,
|
|
|
|
client: web::Data<Client>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let (token, file) = components.into_inner();
|
|
|
|
|
|
|
|
let url = format!(
|
|
|
|
"{}/image/delete/{}/{}",
|
|
|
|
Settings::get().pictrs_url,
|
|
|
|
&token,
|
|
|
|
&file
|
|
|
|
);
|
2020-12-04 14:00:15 +00:00
|
|
|
|
|
|
|
let mut client_req = client.request_from(url, req.head());
|
|
|
|
|
|
|
|
if let Some(addr) = req.head().peer_addr {
|
|
|
|
client_req = client_req.header("X-Forwarded-For", addr.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = client_req.no_decompress().send().await?;
|
2020-08-05 16:00:00 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
|
|
|
|
}
|