Skip to content

Commit 2002161

Browse files
committed
return the next largest image for size query param
fixes #18
1 parent 8a52fd6 commit 2002161

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ You can give any optional query parameters to change what reply you receive:
2424
| placeholder | `true` / `false` | return a placeholder when user has no image | `false` |
2525
| size | `64` / `128` / `256` / `512` | square resolution of the image | `256` |
2626

27-
> [!WARNING]
28-
> The placeholder will always be returned as a 500x500 sized image
27+
> [!INFO]
28+
> `size` will return the next largest image if requested value is not available
2929
3030
# config
3131

src/image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ use axum::{
1111
response::{IntoResponse, Response},
1212
};
1313

14-
use rand::{Rng, SeedableRng, rngs::SmallRng, seq::IndexedRandom};
14+
use rand::{SeedableRng, rngs::SmallRng, seq::IndexedRandom};
1515
use reqwest::header::CONTENT_TYPE;
1616
use svg::{
1717
Document,
18-
node::element::{Definitions, Group, Mask, Polygon, Polyline, Rectangle, Use, tag::Rectangle},
18+
node::element::{Definitions, Group, Mask, Polygon, Polyline, Rectangle, Use},
1919
};
2020
use tokio::{fs::File, process::Command, task::JoinSet};
2121
use tokio_util::io::ReaderStream;
2222

23-
use crate::{PLACEHOLDER, error::AppError, format::SupportedFormat};
23+
use crate::{error::AppError, format::SupportedFormat};
2424

2525
static IMAGE_PATH: LazyLock<String> =
2626
LazyLock::new(|| env::var("IMAGE_PATH").expect("IMAGE_PATH not present"));

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ mod pages;
3232
static LOG_LEVEL: LazyLock<String> =
3333
LazyLock::new(|| env::var("LOG_LEVEL").unwrap_or("INFO".into()));
3434

35-
static PLACEHOLDER: &[u8] = include_bytes!("../templates/placeholder.jpg");
36-
3735
static SIZES: &[u32] = &[64, 128, 256, 512];
3836

3937
#[tokio::main]
@@ -102,18 +100,25 @@ pub async fn post_image(session: Session, mut multipart: Multipart) -> Result<Re
102100
}
103101

104102
#[derive(Deserialize)]
105-
pub struct PlaceholderQuery {
103+
pub struct GetImageQuery {
106104
placeholder: Option<bool>,
107105
size: Option<u32>,
108106
}
109107

110108
pub async fn get_image(
111-
Query(params): Query<PlaceholderQuery>,
109+
Query(params): Query<GetImageQuery>,
112110
Path(user_id): Path<u32>,
113111
if_none_match: Option<TypedHeader<IfNoneMatch>>,
114112
) -> Result<Response, AppError> {
115113
// default size
116-
let size = params.size.unwrap_or(256);
114+
let requested_size = params.size.unwrap_or(256);
115+
// get closest size that is bigger, or largest
116+
let size = *SIZES
117+
.iter()
118+
.filter(|x| **x > requested_size)
119+
.min()
120+
.unwrap_or(SIZES.iter().max().unwrap());
121+
dbg!(size);
117122
let profile = ProfileImage::new(user_id);
118123
let etag_opt = file_modified_etag(&profile.path(size)).await?;
119124

0 commit comments

Comments
 (0)