|
|
|
@ -2,6 +2,7 @@ use actix_web::{
|
|
|
|
|
body::BodyStream,
|
|
|
|
|
http::{
|
|
|
|
|
header::{HeaderName, ACCEPT_ENCODING, HOST},
|
|
|
|
|
Method,
|
|
|
|
|
StatusCode,
|
|
|
|
|
},
|
|
|
|
|
web,
|
|
|
|
@ -10,6 +11,7 @@ use actix_web::{
|
|
|
|
|
HttpResponse,
|
|
|
|
|
};
|
|
|
|
|
use futures::stream::{Stream, StreamExt};
|
|
|
|
|
use http::HeaderValue;
|
|
|
|
|
use lemmy_api_common::{context::LemmyContext, request::PictrsResponse};
|
|
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
|
images::{LocalImage, LocalImageForm, RemoteImage},
|
|
|
|
@ -109,7 +111,7 @@ fn adapt_request(
|
|
|
|
|
const INVALID_HEADERS: &[HeaderName] = &[ACCEPT_ENCODING, HOST];
|
|
|
|
|
|
|
|
|
|
let client_request = client
|
|
|
|
|
.request(request.method().clone(), url)
|
|
|
|
|
.request(convert_method(request.method()), url)
|
|
|
|
|
.timeout(REQWEST_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
request
|
|
|
|
@ -119,7 +121,8 @@ fn adapt_request(
|
|
|
|
|
if INVALID_HEADERS.contains(key) {
|
|
|
|
|
client_req
|
|
|
|
|
} else {
|
|
|
|
|
client_req.header(key, value)
|
|
|
|
|
// TODO: remove as_str and as_bytes conversions after actix-web upgrades to http 1.0
|
|
|
|
|
client_req.header(key.as_str(), value.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
@ -166,7 +169,7 @@ async fn upload(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::build(status).json(images))
|
|
|
|
|
Ok(HttpResponse::build(convert_status(status)).json(images))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn full_res(
|
|
|
|
@ -209,14 +212,14 @@ async fn image(
|
|
|
|
|
|
|
|
|
|
let res = client_req.send().await?;
|
|
|
|
|
|
|
|
|
|
if res.status() == StatusCode::NOT_FOUND {
|
|
|
|
|
if res.status() == http::StatusCode::NOT_FOUND {
|
|
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut client_res = HttpResponse::build(res.status());
|
|
|
|
|
let mut client_res = HttpResponse::build(StatusCode::from_u16(res.status().as_u16())?);
|
|
|
|
|
|
|
|
|
|
for (name, value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
|
|
|
|
|
client_res.insert_header((name.clone(), value.clone()));
|
|
|
|
|
client_res.insert_header(convert_header(name, value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(client_res.body(BodyStream::new(res.bytes_stream())))
|
|
|
|
@ -245,7 +248,7 @@ async fn delete(
|
|
|
|
|
|
|
|
|
|
LocalImage::delete_by_alias(&mut context.pool(), &file).await?;
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
|
|
|
|
|
Ok(HttpResponse::build(convert_status(res.status())).body(BodyStream::new(res.bytes_stream())))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn image_proxy(
|
|
|
|
@ -308,3 +311,14 @@ where
|
|
|
|
|
std::pin::Pin::new(&mut self.rx).poll_recv(cx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove these conversions after actix-web upgrades to http 1.0
|
|
|
|
|
fn convert_status(status: http::StatusCode) -> StatusCode {
|
|
|
|
|
StatusCode::from_u16(status.as_u16()).expect("status can be converted")
|
|
|
|
|
}
|
|
|
|
|
fn convert_method(method: &Method) -> http::Method {
|
|
|
|
|
http::Method::from_bytes(method.as_str().as_bytes()).expect("method can be converted")
|
|
|
|
|
}
|
|
|
|
|
fn convert_header<'a>(name: &'a http::HeaderName, value: &'a HeaderValue) -> (&'a str, &'a [u8]) {
|
|
|
|
|
(name.as_str(), value.as_bytes())
|
|
|
|
|
}
|
|
|
|
|