feat: returing res.buffer()

This commit is contained in:
Junseo Park 2021-02-24 22:48:58 +09:00
parent 228beeecac
commit a11366078f
2 changed files with 6 additions and 6 deletions

View File

@ -11,14 +11,14 @@ const Avatar = nc<ApiRequest, NextApiResponse>()
.get(async(req, res) => {
res.setHeader('Access-Control-Allow-Origin', process.env.KOREANBOTS_URL)
const { imageRateLimit } = await import('@utils/Query')
const { id: param, size=256 } = req.query
const { id: param, size='256' } = req.query
const rate = ratelimit.image(req.socket.remoteAddress)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ratelimited = RateLimitHandler(res, { used: rate, limit: 600, reset: (<any>imageRateLimit).scheduler.get(req.socket.remoteAddress).expiry, onLimitExceed: async(res) => {
const img = await get.images.user.load(DiscordEnpoints.CDN.default(Math.floor(Math.random() * 6), { format: 'png' }))
res.setHeader('Content-Type', 'image/png')
res.setHeader('Cache-Control', 'no-cache')
img.pipe(res as unknown as NodeJS.WritableStream)
res.send(img)
} })
if(ratelimited) return
const splitted = param.split('.')
@ -42,7 +42,7 @@ const Avatar = nc<ApiRequest, NextApiResponse>()
res.setHeader('Content-Type', `image/${ext}`)
res.setHeader('Cache-Control', 'public, max-age=86400')
img.pipe(res as unknown as NodeJS.WritableStream)
res.send(img)
})

View File

@ -49,6 +49,7 @@ async function getBot(id: string, owners=true):Promise<Bot> {
if(!discordBot) return null
res[0].flags = res[0].flags | (discordBot.flags && DiscordUserFlags.VERIFIED_BOT ? BotFlags.verifed : 0) | (res[0].trusted ? BotFlags.trusted : 0)
res[0].tag = discordBot.discriminator
res[0].avatar = discordBot.avatar
res[0].name = discordBot.username
res[0].category = JSON.parse(res[0].category)
res[0].owners = JSON.parse(res[0].owners)
@ -216,11 +217,10 @@ async function submitBot(id: string, data: AddBotSubmit):Promise<number|Submitte
return await getBotSubmit(botId, date)
}
async function getImage(url: string):Promise<Stream> {
async function getImage(url: string) {
const res = await fetch(url)
if(!res.ok) return null
const cache = new StreamCache()
return res.body.pipe(cache)
return await res.buffer()
}
async function getDiscordUser(id: string):Promise<DiscordUser> {