Skip to content

Commit

Permalink
add default format
Browse files Browse the repository at this point in the history
  • Loading branch information
reschandreas committed Sep 26, 2024
1 parent 5dbdae9 commit f272ec6
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ravatar"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ docker run -p 8080:8080 -v /path/to/images:/raw ghcr.io/reschandreas/ravatar:lat
| `IMAGES_PATH` | The path to the generated images | `/images` |
| `LOG_LEVEL` | The log level of the server | `info` |
| `OFFER_ORIGINAL_DIMENSIONS` | Offer the image with its original dimensions instead of resized to fill | `false` |
| `DEFAULT_FORMAT` | The default format of the image, "square" or "original" | `square` |

If you want to serve an image with another identifier than the filename, i.e. the email address, you can use
LDAP to match the filename to other identifiers from your active directory. The configuration relies
Expand Down
4 changes: 2 additions & 2 deletions helm/ravatar/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: ravatar
description: Ravatar is an implementation of the Libavatar service, which make it easy to host your own avatar service.
type: application
version: 0.1.4
appVersion: "v0.1.1"
version: 0.1.5
appVersion: "v0.1.2"
2 changes: 2 additions & 0 deletions helm/ravatar/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
value: {{ .Values.ravatar.imageExtension | default "png" | quote }}
- name: MM_EXTENSION
value: {{ .Values.ravatar.mmExtension | default "png" | quote }}
- name: DEFAULT_FORMAT
value: {{ .Values.ravatar.defaultFormat | default "square" | quote }}
- name: RAW_PATH
value: {{ .Values.ravatar.rawImagesPath | default "/raw"| quote }}
- name: IMAGES_PATH
Expand Down
1 change: 1 addition & 0 deletions helm/ravatar/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ravatar:
host: "0.0.0.0"
imageExtension: "png"
mmExtension: "png"
defaultFormat: "square"
port: 8080
rawImagesPath: "/raw"
generatedImagesPath: "/images"
Expand Down
14 changes: 13 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ pub(crate) fn read_config() -> Config {
let host = env::var("HOST").unwrap_or("0.0.0.0".into());
let port: u16 = env::var("PORT").unwrap_or("8080".into()).parse().unwrap();
let log_level = env::var("LOG_LEVEL").unwrap_or("info".into());
let offer_original_dimensions: bool = env::var("OFFER_ORIGINAL_DIMENSIONS")
let mut offer_original_dimensions: bool = env::var("OFFER_ORIGINAL_DIMENSIONS")
.unwrap_or("false".into())
.parse()
.unwrap();
let default_format = env::var("DEFAULT_FORMAT").unwrap_or("square".into());
if default_format.eq("square") {
log::info!("DEFAULT_FORMAT is set to square, this is the default behavior");
} else if default_format.eq("original") {
log::info!("DEFAULT_FORMAT is set to original, this will offer the original image per default, use original_dimensions=false to disable");
} else {
log::warn!("DEFAULT_FORMAT is set to an unknown value, defaulting to square");
}
if default_format.eq("original") {
offer_original_dimensions = true;
}
let mut ldap: Option<LdapConfig> = None;
if let Ok(ldap_url) = env::var("LDAP_URL") {
let ldap_bind_username = env::var("LDAP_BIND_USERNAME").unwrap_or("".into());
Expand Down Expand Up @@ -50,6 +61,7 @@ pub(crate) fn read_config() -> Config {
raw,
extension,
mm_extension,
default_original_dimensions: default_format.eq("original"),
log_level,
ldap,
offer_original_dimensions,
Expand Down
6 changes: 5 additions & 1 deletion src/image_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ pub fn resize_default(config: &Config) {
}
let sizes: Vec<u32> = vec![16, 24, 32, 48, 64, 80, 96, 128, 256, 512, 1024];
let image_path = config.images.clone();
for name in vec!["mm"] {
for name in ["mm", "default"] {
let source_binding = build_path(
vec!["default".to_string(), name.to_string()],
Some(extension.clone()),
);
if !source_binding.as_path().exists() {
log::warn!("source does not exist {}", source_binding.to_str().unwrap());
continue;
}
sizes.par_iter().for_each(|size| {
let directory = build_path(vec![image_path.clone(), size.to_string()], None);
if !directory.as_path().exists() {
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ async fn avatar(
let default: String = read_default(query.clone());
log::debug!("serving {mail_hash}, size {size}");
let mut path_parts = vec![cache_dir.clone()];
if query.original_dimensions.unwrap_or_default() {
let mut serve_original_size = config.default_original_dimensions;

if query.original_dimensions.is_some() && !query.original_dimensions.unwrap() {
serve_original_size = false;
}
if serve_original_size {
path_parts.push("original-dimensions".to_string());
}
path_parts.push(size.to_string());
Expand Down
1 change: 1 addition & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct Config {
pub raw: String,
pub extension: String,
pub mm_extension: String,
pub default_original_dimensions: bool,
pub log_level: String,
pub offer_original_dimensions: bool,
pub ldap: Option<LdapConfig>,
Expand Down

0 comments on commit f272ec6

Please sign in to comment.