Merge pull request #1202 from LemmyNet/pict-rs-v2-dess

Pict rs v2 dess
pull/1209/head
Dessalines 2020-10-14 15:46:02 -04:00 committed by GitHub
commit 23793fe096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 26 deletions

View File

@ -37,7 +37,7 @@ services:
restart: always restart: always
pictrs: pictrs:
image: asonix/pictrs:amd64-v0.1.0-r9 image: asonix/pictrs:v0.2.2-r0
user: 991:991 user: 991:991
ports: ports:
- "127.0.0.1:8537:8080" - "127.0.0.1:8537:8080"

View File

@ -40,7 +40,7 @@ services:
restart: always restart: always
pictrs: pictrs:
image: asonix/pictrs:v0.1.13-r0 image: asonix/pictrs:v0.2.2-r0
ports: ports:
- "8537:8080" - "8537:8080"
user: 991:991 user: 991:991

View File

@ -23,7 +23,7 @@ services:
pictrs: pictrs:
restart: always restart: always
image: asonix/pictrs:v0.1.13-r0 image: asonix/pictrs:v0.2.2-r0
user: 991:991 user: 991:991
volumes: volumes:
- ./volumes/pictrs_alpha:/mnt - ./volumes/pictrs_alpha:/mnt

View File

@ -37,7 +37,7 @@ services:
- lemmy - lemmy
pictrs: pictrs:
image: asonix/pictrs:v0.1.13-r0 image: asonix/pictrs:v0.2.2-r0
ports: ports:
- "127.0.0.1:8537:8080" - "127.0.0.1:8537:8080"
user: 991:991 user: 991:991

View File

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