From cf9e7ca77029c20daa325cd3943f6e964c507da4 Mon Sep 17 00:00:00 2001 From: skinmaker1345 Date: Mon, 30 Dec 2024 01:56:29 +0900 Subject: [PATCH] refactor: include hash for discord cdn routing --- components/BotCard.tsx | 1 + components/DiscordAvatar.tsx | 3 +++ components/Search.tsx | 4 ++-- components/ServerCard.tsx | 2 +- components/ServerIcon.tsx | 14 +++----------- pages/api/v2/bots/[id]/index.ts | 3 ++- pages/bots/[id]/edit.tsx | 3 ++- pages/bots/[id]/index.tsx | 2 +- pages/bots/[id]/vote.tsx | 1 + pages/developers/applications/bots/[id].tsx | 2 +- pages/developers/applications/servers/[id].tsx | 2 +- pages/security.tsx | 2 +- pages/servers/[id]/edit.tsx | 6 +++++- pages/servers/[id]/index.tsx | 2 +- pages/servers/[id]/vote.tsx | 2 +- pages/users/[id]/index.tsx | 2 +- types/index.ts | 1 + utils/Constants.ts | 14 +++++++++++--- 18 files changed, 39 insertions(+), 27 deletions(-) diff --git a/components/BotCard.tsx b/components/BotCard.tsx index d78f493..b864ac8 100644 --- a/components/BotCard.tsx +++ b/components/BotCard.tsx @@ -34,6 +34,7 @@ const BotCard: React.FC = ({ manage = false, bot }) => { diff --git a/components/DiscordAvatar.tsx b/components/DiscordAvatar.tsx index a6cbb48..47d1981 100644 --- a/components/DiscordAvatar.tsx +++ b/components/DiscordAvatar.tsx @@ -11,10 +11,12 @@ const DiscordAvatar: React.FC = (props) => { src={KoreanbotsEndPoints.CDN.avatar(props.userID, { format: 'webp', size: props.size ?? 256, + hash: props.hash, })} fallbackSrc={KoreanbotsEndPoints.CDN.avatar(props.userID, { format: 'png', size: props.size ?? 256, + hash: props.hash, })} /> ) @@ -25,6 +27,7 @@ interface DiscordAvatarProps { userID: string className?: string size?: 128 | 256 | 512 + hash?: string } interface ImageEvent extends Event { diff --git a/components/Search.tsx b/components/Search.tsx index 2e0e4f7..d477ae2 100644 --- a/components/Search.tsx +++ b/components/Search.tsx @@ -138,7 +138,7 @@ const Search: React.FC = () => { data.data.bots.map((el) => (
  • - +

    {el.name}

    {el.intro}

    @@ -156,7 +156,7 @@ const Search: React.FC = () => { data.data.servers.map((el) => (
  • - +

    {el.name}

    {el.intro}

    diff --git a/components/ServerCard.tsx b/components/ServerCard.tsx index 7557e33..5ac69bf 100644 --- a/components/ServerCard.tsx +++ b/components/ServerCard.tsx @@ -43,7 +43,7 @@ const ServerCard: React.FC = ({ type, server }) => { diff --git a/components/ServerIcon.tsx b/components/ServerIcon.tsx index 33f3f69..737dd26 100644 --- a/components/ServerIcon.tsx +++ b/components/ServerIcon.tsx @@ -1,6 +1,6 @@ import { SyntheticEvent } from 'react' import dynamic from 'next/dynamic' -import { DiscordEnpoints, KoreanbotsEndPoints } from '@utils/Constants' +import { KoreanbotsEndPoints } from '@utils/Constants' const Image = dynamic(() => import('@components/Image')) @@ -9,16 +9,8 @@ const ServerIcon: React.FC = ({ id, size, className, alt, hash {alt} ) } diff --git a/pages/api/v2/bots/[id]/index.ts b/pages/api/v2/bots/[id]/index.ts index 74a9e5d..8e430af 100644 --- a/pages/api/v2/bots/[id]/index.ts +++ b/pages/api/v2/bots/[id]/index.ts @@ -25,6 +25,7 @@ import { } from '@utils/Tools' import { discordLog, getMainGuild, webhookClients } from '@utils/DiscordBot' import { KoreanbotsEndPoints } from '@utils/Constants' +import { userInfo } from 'os' const patchLimiter = rateLimit({ windowMs: 2 * 60 * 1000, @@ -127,7 +128,7 @@ const Bots = RequestHandler() : `${userinfo.username}#${userinfo.tag}`, iconURL: KoreanbotsEndPoints.URL.root + - KoreanbotsEndPoints.CDN.avatar(userinfo.id, { format: 'png', size: 256 }), + KoreanbotsEndPoints.CDN.avatar(userinfo.id, { format: 'png', size: 256, hash: userinfo.avatar }), url: KoreanbotsEndPoints.URL.user(userinfo.id), }) .setTitle('대기 중') diff --git a/pages/bots/[id]/edit.tsx b/pages/bots/[id]/edit.tsx index 7cf4628..b273d3d 100644 --- a/pages/bots/[id]/edit.tsx +++ b/pages/bots/[id]/edit.tsx @@ -95,7 +95,7 @@ const ManageBotPage: NextPage = ({ bot, user, csrfToken, theme } {({ errors, touched, values, setFieldTouched, setFieldValue }) => (
    - +

    {bot.name}#{bot.tag} @@ -344,6 +344,7 @@ const ManageBotPage: NextPage = ({ bot, user, csrfToken, theme } {' '} {el.tag === '0' diff --git a/pages/bots/[id]/index.tsx b/pages/bots/[id]/index.tsx index 19c0e84..af3959d 100644 --- a/pages/bots/[id]/index.tsx +++ b/pages/bots/[id]/index.tsx @@ -122,7 +122,7 @@ const Bots: NextPage = ({ data, desc, date, user, theme, csrfToken })

    - +
    = ({ data, user, theme, csrfToken }) => {
    = ({ user, spec, bot, theme,

    - +
    diff --git a/pages/developers/applications/servers/[id].tsx b/pages/developers/applications/servers/[id].tsx index 37f6820..40c2a4c 100644 --- a/pages/developers/applications/servers/[id].tsx +++ b/pages/developers/applications/servers/[id].tsx @@ -77,7 +77,7 @@ const ServerApplication: NextPage = ({ <>
    - +
    diff --git a/pages/security.tsx b/pages/security.tsx index 5cca4fb..5898e4e 100644 --- a/pages/security.tsx +++ b/pages/security.tsx @@ -64,7 +64,7 @@ const Security: NextPage = ({ bugReports }) => { .filter((el) => el) .map((u) => (
    - + {u.globalName} {`(@${u.username})`} diff --git a/pages/servers/[id]/edit.tsx b/pages/servers/[id]/edit.tsx index ca48052..b4154d5 100644 --- a/pages/servers/[id]/edit.tsx +++ b/pages/servers/[id]/edit.tsx @@ -87,7 +87,11 @@ const ManageServerPage: NextPage = ({ {({ errors, touched, values, setFieldTouched, setFieldValue }) => (
    - +

    {server.name}

    ID: {server.id}

    diff --git a/pages/servers/[id]/index.tsx b/pages/servers/[id]/index.tsx index b06bfc3..cfbcbae 100644 --- a/pages/servers/[id]/index.tsx +++ b/pages/servers/[id]/index.tsx @@ -143,7 +143,7 @@ const Servers: NextPage = ({ data, desc, date, user, theme }) => {
    - +

    diff --git a/pages/servers/[id]/vote.tsx b/pages/servers/[id]/vote.tsx index 055e752..042bc81 100644 --- a/pages/servers/[id]/vote.tsx +++ b/pages/servers/[id]/vote.tsx @@ -90,7 +90,7 @@ const VoteServer: NextPage = ({ data, user, theme, csrfToken })
    - + diff --git a/pages/users/[id]/index.tsx b/pages/users/[id]/index.tsx index 499ab6b..6534416 100644 --- a/pages/users/[id]/index.tsx +++ b/pages/users/[id]/index.tsx @@ -55,7 +55,7 @@ const Users: NextPage = ({ user, data }) => { />
    - +
    diff --git a/types/index.ts b/types/index.ts index e04feb8..efc78ce 100644 --- a/types/index.ts +++ b/types/index.ts @@ -350,6 +350,7 @@ export interface ImageOptions { export interface KoreanbotsImageOptions { format?: 'webp' | 'png' | 'gif' size?: 128 | 256 | 512 + hash?: string; } export enum DiscordImageType { diff --git a/utils/Constants.ts b/utils/Constants.ts index a829b93..7a15623 100644 --- a/utils/Constants.ts +++ b/utils/Constants.ts @@ -334,12 +334,20 @@ export const KoreanbotsEndPoints = { } }, CDN: class { - static root = '//cdn.koreanbots.dev' + static root = '/api/image' + static cdn = '//cdn.koreanbots.dev' + static avatar(id: string, options: KoreanbotsImageOptions) { - return makeImageURL(`${this.root}/avatars/${id}`, options) + if (options.hash) { + return makeImageURL(`${this.cdn}/avatars/${id}/${options.hash}`, options) + } + return makeImageURL(`${this.root}/discord/avatars/${id}`, options) } static icon(id: string, options: KoreanbotsImageOptions) { - return makeImageURL(`${this.root}/icons/${id}`, options) + if (options.hash) { + return makeImageURL(`${this.cdn}/icons/${id}/${options.hash}`, options) + } + return makeImageURL(`${this.root}/discord/icons/${id}`, options) } }, URL: class {