Skip to content

Commit

Permalink
fix: refactor badge.ts to use volume system for image loading (#251)
Browse files Browse the repository at this point in the history
* fix: refactor badge.ts to use volume system for image loading

* fix: lint

* fix: update env

---------

Co-authored-by: Antoine D <[email protected]>
  • Loading branch information
Suboyyy and Antoine D authored Oct 10, 2024
1 parent 02531a5 commit 5ca1e21
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 44 deletions.
10 changes: 0 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,3 @@ DISCORD_WEBHOOK_CONTACT=
# Some more random variables
API_RATE_LIMIT=12
API_CART_LIFESPAN=3600

# Base 64 encoded image for the badge template
BADGE_RESTRICTED="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_RESTRICTED_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_FULLACCESS="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_FULLACCESS_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_ORGAPRICE="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_ORGAPRICE_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_INVITE="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_INVITE_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
9 changes: 0 additions & 9 deletions .env.test.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ DATABASE_URL=mysql://dev:dev@localhost/arena_test

LOG_LEVEL=silly
LOG_IN_TEST=false

BADGE_RESTRICTED="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_RESTRICTED_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_FULLACCESS="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_FULLACCESS_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_ORGAPRICE="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_ORGAPRICE_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_INVITE="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
BADGE_INVITE_BACK="UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoFAAUAA8BiJZwAAp1sr3oAAPv++hpsEWcij+2s5Q/cZFuOAAA="
Binary file added assets/badges/blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions src/utils/badge.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import axios, { AxiosResponse } from 'axios';
import { readFileSync } from 'fs';
import PDFkit from 'pdfkit';
import sharp from 'sharp';
import { Badge } from '../types';
import env from './env';

const imageBase = async (base64: string) => {
// convert webp base64 to png base64
const Image = sharp(Buffer.from(base64, 'base64'));
return `data:image/png;base64,${(await Image.toFormat('png').toBuffer()).toString('base64')}`;
const getImage = (filename: string) => {
try {
return `data:image/png;base64,${readFileSync(`assets/badges/${filename}`, 'base64')}`;
} catch {
return `data:image/png;base64,${readFileSync(`assets/badges/blank.png`, 'base64')}`;
}
};

const loadImageBadgeRestricted = () => imageBase(env.badge.badge_restricted);
const loadImageBadgeOrgaPrice = () => imageBase(env.badge.badge_orgaprice);
const loadImageBadgeFullAccess = () => imageBase(env.badge.badge_fullaccess);
const loadImageBadgeInvite = () => imageBase(env.badge.badge_invite);
const loadImageBadgeRestricted = () => getImage('badge-restricted.png');
const loadImageBadgeOrgaPrice = () => getImage('badge-orgaprice.png');
const loadImageBadgeFullAccess = () => getImage('badge-fullaccess.png');
const loadImageBadgeInvite = () => getImage('badge-invite.png');

const loadBackRestricted = () => imageBase(env.badge.badge_restricted_back);
const loadBackOrgaPrice = () => imageBase(env.badge.badge_orgaprice_back);
const loadBackFullAccess = () => imageBase(env.badge.badge_fullaccess_back);
const loadBackInvite = () => imageBase(env.badge.badge_invite_back);
const loadBackRestricted = () => getImage('back-restricted.png');
const loadBackOrgaPrice = () => getImage('back-orgaprice.png');
const loadBackFullAccess = () => getImage('back-fullaccess.png');
const loadBackInvite = () => getImage('back-invite.png');

type BadgePermission = 'restricted' | 'orgaprice' | 'fullaccess' | 'invite';

const getBack = (permission: BadgePermission): Promise<string> => {
const getBack = (permission: BadgePermission): string => {
switch (permission) {
case 'restricted': {
return loadBackRestricted();
Expand All @@ -46,7 +48,7 @@ const getBack = (permission: BadgePermission): Promise<string> => {
}
};

const getBadge = (permission: BadgePermission): Promise<string> => {
const getBadge = (permission: BadgePermission): string => {
switch (permission) {
case 'restricted': {
return loadImageBadgeRestricted();
Expand Down
10 changes: 0 additions & 10 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,6 @@ const env = {
enabledInTest: loadEnv('LOG_IN_TEST') === 'true',
sentryDsn: loadEnv('LOG_SENTRY_DSN'),
},
badge: {
badge_restricted: loadEnv('BADGE_RESTRICTED'),
badge_orgaprice: loadEnv('BADGE_ORGAPRICE'),
badge_fullaccess: loadEnv('BADGE_FULLACCESS'),
badge_invite: loadEnv('BADGE_INVITE'),
badge_restricted_back: loadEnv('BADGE_RESTRICTED_BACK'),
badge_orgaprice_back: loadEnv('BADGE_ORGAPRICE_BACK'),
badge_fullaccess_back: loadEnv('BADGE_FULLACCESS_BACK'),
badge_invite_back: loadEnv('BADGE_INVITE_BACK'),
},
};

// Create a warn log array to use it after winsotn initialization
Expand Down

0 comments on commit 5ca1e21

Please sign in to comment.