feat: 🚧 add libravatar options

This commit is contained in:
May 2024-11-20 16:02:42 -08:00
parent 092c70da5b
commit c70db40ffa
Signed by: split
GPG key ID: C325C61F0BF517C0
2 changed files with 95 additions and 31 deletions

View file

@ -26,6 +26,19 @@ IMAGES__DEFAULT_RESOLUTION=512
# Comma-separated list of permitted image mimetypes # Comma-separated list of permitted image mimetypes
IMAGES__ALLOWED_INPUT_FORMATS=image/jpg,image/jpeg,image/png,image/webp,image/gif IMAGES__ALLOWED_INPUT_FORMATS=image/jpg,image/jpeg,image/png,image/webp,image/gif
# Whether or not to enable the libravatar API. Default false.
LIBRAVATAR__ENABLED=false
# Either jpeg or png. The type of image the libravatar API serves. Default png.
LIBRAVATAR__FORMAT=png
# What to do when libravatar requests a resolution that is not a valid output resolution. Default round.
# Modes:
# round - Round to the nearest image size.
# nocache - Render the image at the requested size without saving it to disk.
# cache - Render the image at the requested size and save it to disk.
LIBRAVATAR__GENERATION_MODE=round
# Prisma database URL # Prisma database URL
DATABASE_URL=file:../.data/data.db DATABASE_URL=file:../.data/data.db

View file

@ -1,32 +1,83 @@
import Sharp, { type FormatEnum } from 'sharp'; import Sharp, { type FormatEnum } from "sharp"
import { env } from '$env/dynamic/private'; import { env } from "$env/dynamic/private"
const stbool = (s: string) => {
switch (s.toLowerCase()) {
case "1":
case "y":
case "yes":
case "true":
return true
case "0":
case "n":
case "no":
case "false":
return false
default:
return undefined
}
}
const configuration = { const configuration = {
oauth2: { oauth2: {
endpoints: { endpoints: {
authenticate: env.OAUTH2__AUTHENTICATE!, authenticate: env.OAUTH2__AUTHENTICATE!,
logout: env.OAUTH2__LOGOUT, logout: env.OAUTH2__LOGOUT,
token: env.OAUTH2__GET_TOKEN! token: env.OAUTH2__GET_TOKEN!,
}, },
client: { client: {
id: env.OAUTH2__CLIENT_ID!, id: env.OAUTH2__CLIENT_ID!,
secret: env.OAUTH2__CLIENT_SECRET!, secret: env.OAUTH2__CLIENT_SECRET!,
scopes: env.OAUTH2__SCOPES! scopes: env.OAUTH2__SCOPES!,
} },
}, },
userinfo: { userinfo: {
route: env.USERINFO__ROUTE!, route: env.USERINFO__ROUTE!,
identifier: env.USERINFO__IDENTIFIER! identifier: env.USERINFO__IDENTIFIER!,
}, },
images: { images: {
permitted_input: (env.ALLOWED_TYPES || env.IMAGES__ALLOWED_INPUT_FORMATS)?.split(',') || [], permitted_input:
default_resolution: parseInt((env.IMAGES__DEFAULT_RESOLUTION || '').toString(), 10) || 512, (env.ALLOWED_TYPES || env.IMAGES__ALLOWED_INPUT_FORMATS)?.split(
extra_output_types: ","
(env.IMAGES__EXTRA_OUTPUT_FORMATS?.split(',').filter( ) || [],
(e) => e in Sharp.format default_resolution:
) as (keyof FormatEnum)[]) || [], parseInt((env.IMAGES__DEFAULT_RESOLUTION || "").toString(), 10) ||
output_resolutions: env.IMAGES__OUTPUT_RESOLUTIONS?.split(',').map((e) => parseInt(e, 10)) || [ 512,
1024, 512, 256, 128, 64, 32 extra_output_types:
] (env.IMAGES__EXTRA_OUTPUT_FORMATS?.split(",").filter(
} e => e in Sharp.format
}; ) as (keyof FormatEnum)[]) || [],
export default configuration; output_resolutions: env.IMAGES__OUTPUT_RESOLUTIONS?.split(",").map(e =>
parseInt(e, 10)
) || [1024, 512, 256, 128, 64, 32],
},
...(stbool(env.LIBRAVATAR__ENABLED) ?? false
? {
libravatar: {
output_format: (["png", "jpeg"].includes(
env.LIBRAVATAR__FORMAT
)
? env.LIBRAVATAR__FORMAT
: "jpeg") as keyof FormatEnum,
resize_mode: ["round", "nocache", "cache"].includes(
env.LIBRAVATAR__GENERATION_MODE
)
? env.LIBRAVATAR__GENERATION_MODE
: "round",
},
}
: {}),
}
// add extra output format if required
if (
configuration.libravatar &&
!configuration.images.extra_output_types.includes(
configuration.libravatar.output_format
)
)
configuration.images.extra_output_types.push(
configuration.libravatar.output_format
)
export default configuration