use actix_web::{body::BodyStream, http::StatusCode, web::Data, *};
use anyhow::anyhow;
use awc::Client;
-use lemmy_db_queries::source::secret::SecretSingleton;
-use lemmy_db_schema::source::secret::Secret;
-use lemmy_utils::{claims::Claims, rate_limit::RateLimit, settings::structs::Settings, LemmyError};
+use lemmy_utils::{claims::Claims, rate_limit::RateLimit, LemmyError};
+use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use std::time::Duration;
req: HttpRequest,
body: web::Payload,
client: web::Data<Client>,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
// TODO: check rate limit here
let jwt = req
.cookie("jwt")
.expect("No auth header for picture upload");
- let jwt_secret = Secret::get().jwt_secret;
- if Claims::decode(jwt.value(), &jwt_secret).is_err() {
+ if Claims::decode(jwt.value(), &context.secret().jwt_secret).is_err() {
return Ok(HttpResponse::Unauthorized().finish());
};
- let mut client_req = client.request_from(format!("{}/image", pictrs_url()?), req.head());
+ let mut client_req = client.request_from(
+ format!("{}/image", pictrs_url(context.settings().pictrs_url)?),
+ req.head(),
+ );
// remove content-encoding header so that pictrs doesnt send gzipped response
client_req.headers_mut().remove(ACCEPT_ENCODING);
web::Query(params): web::Query<PictrsParams>,
req: HttpRequest,
client: web::Data<Client>,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
let name = &filename.into_inner();
// If there are no query params, the URL is original
+ let pictrs_url_settings = context.settings().pictrs_url;
let url = if params.format.is_none() && params.thumbnail.is_none() {
- format!("{}/image/original/{}", pictrs_url()?, name,)
+ format!(
+ "{}/image/original/{}",
+ pictrs_url(pictrs_url_settings)?,
+ name,
+ )
} else {
// Use jpg as a default when none is given
let format = params.format.unwrap_or_else(|| "jpg".to_string());
- let mut url = format!("{}/image/process.{}?src={}", pictrs_url()?, format, name,);
+ let mut url = format!(
+ "{}/image/process.{}?src={}",
+ pictrs_url(pictrs_url_settings)?,
+ format,
+ name,
+ );
if let Some(size) = params.thumbnail {
url = format!("{}&thumbnail={}", url, size,);
components: web::Path<(String, String)>,
req: HttpRequest,
client: web::Data<Client>,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
let (token, file) = components.into_inner();
- let url = format!("{}/image/delete/{}/{}", pictrs_url()?, &token, &file);
+ let url = format!(
+ "{}/image/delete/{}/{}",
+ pictrs_url(context.settings().pictrs_url)?,
+ &token,
+ &file
+ );
let mut client_req = client.request_from(url, req.head());
client_req.headers_mut().remove(ACCEPT_ENCODING);
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res)))
}
-fn pictrs_url() -> Result<String, LemmyError> {
- Settings::get()
- .pictrs_url
- .ok_or_else(|| anyhow!("images_disabled").into())
+fn pictrs_url(pictrs_url: Option<String>) -> Result<String, LemmyError> {
+ pictrs_url.ok_or_else(|| anyhow!("images_disabled").into())
}