|
|
|
@ -18,10 +18,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
|
|
|
|
|
.wrap(rate_limit.image())
|
|
|
|
|
.route(web::post().to(upload)),
|
|
|
|
|
)
|
|
|
|
|
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
|
|
|
|
|
.service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
|
|
|
|
|
.service(
|
|
|
|
|
web::resource("/pictrs/image/thumbnail{size}/{filename}").route(web::get().to(thumbnail)),
|
|
|
|
|
)
|
|
|
|
|
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -37,6 +35,12 @@ pub struct Images {
|
|
|
|
|
files: Option<Vec<Image>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct PictrsParams {
|
|
|
|
|
format: Option<String>,
|
|
|
|
|
thumbnail: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn upload(
|
|
|
|
|
req: HttpRequest,
|
|
|
|
|
body: web::Payload,
|
|
|
|
@ -59,30 +63,31 @@ async fn upload(
|
|
|
|
|
|
|
|
|
|
async fn full_res(
|
|
|
|
|
filename: web::Path<String>,
|
|
|
|
|
web::Query(params): web::Query<PictrsParams>,
|
|
|
|
|
req: HttpRequest,
|
|
|
|
|
client: web::Data<Client>,
|
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
|
let url = format!(
|
|
|
|
|
"{}/image/{}",
|
|
|
|
|
Settings::get().pictrs_url,
|
|
|
|
|
&filename.into_inner()
|
|
|
|
|
);
|
|
|
|
|
image(url, req, client).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn thumbnail(
|
|
|
|
|
parts: web::Path<(u64, String)>,
|
|
|
|
|
req: HttpRequest,
|
|
|
|
|
client: web::Data<Client>,
|
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
|
let (size, file) = parts.into_inner();
|
|
|
|
|
|
|
|
|
|
let url = format!(
|
|
|
|
|
"{}/image/thumbnail{}/{}",
|
|
|
|
|
Settings::get().pictrs_url,
|
|
|
|
|
size,
|
|
|
|
|
&file
|
|
|
|
|
);
|
|
|
|
|
let name = &filename.into_inner();
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
let format = params.format.unwrap_or("jpg".to_string());
|
|
|
|
|
|
|
|
|
|
let mut url = format!(
|
|
|
|
|
"{}/image/process.{}?src={}",
|
|
|
|
|
Settings::get().pictrs_url,
|
|
|
|
|
format,
|
|
|
|
|
name,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if let Some(size) = params.thumbnail {
|
|
|
|
|
url = format!("{}&thumbnail={}", url, size,);
|
|
|
|
|
}
|
|
|
|
|
url
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
image(url, req, client).await
|
|
|
|
|
}
|
|
|
|
|