mirror of
https://github.com/koreanbots/core.git
synced 2025-12-15 14:10:22 +00:00
feat: added avatar endpoint
This commit is contained in:
parent
fdc1056469
commit
e282beb175
31
pages/api/image/discord/avatars/[id].ts
Normal file
31
pages/api/image/discord/avatars/[id].ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { createReadStream } from 'fs'
|
||||
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
|
||||
import ResponseWrapper from '@utils/ResponseWrapper'
|
||||
import { DiscordEnpoints } from '@utils/Constants'
|
||||
import { get } from '@utils/Query'
|
||||
|
||||
const Avatar: NextApiHandler = async(req: ApiRequest, res: NextApiResponse) => {
|
||||
const splitted = req.query.id.split('.')
|
||||
const ext= splitted[1]
|
||||
const id = splitted[0]
|
||||
if(splitted.length !== 2) return ResponseWrapper(res, { code: 400, message: '올바르지 않은 형식입니다.' })
|
||||
if(!['webp', 'png', 'gif'].includes(ext)) return ResponseWrapper(res, { code: 400, message: '올바르지 않은 확장자입니다.' })
|
||||
|
||||
const user = await get.discord.user.load(id)
|
||||
if(!user) return ResponseWrapper(res, { code: 400, message: '올바르지 않은 유저입니다.' })
|
||||
|
||||
const image = await get.images.user.load(DiscordEnpoints.CDN.user(id, user.avatar, { format: (ext as Ext) }))
|
||||
res.setHeader('Content-Type', `image/${ext}`)
|
||||
|
||||
image.pipe(res)
|
||||
}
|
||||
|
||||
interface ApiRequest extends NextApiRequest {
|
||||
query: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
type Ext = 'webp' | 'png' | 'gif'
|
||||
|
||||
export default Avatar
|
||||
Loading…
x
Reference in New Issue
Block a user