-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vu Van Dung <[email protected]>
- Loading branch information
Showing
6 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { notFound } from "next/navigation"; | ||
import sharp from "sharp"; | ||
|
||
import type { RouteHandler } from "./$types"; | ||
|
||
async function getEmote(id: string | number) { | ||
const res = await fetch(`http://cdn.discordapp.com/emojis/${id}.png`); | ||
if (!res.ok) notFound(); | ||
const emoteBuffer = await res.arrayBuffer(); | ||
return Promise.all([ | ||
sharp(emoteBuffer).resize(40, 40).png().toBuffer(), | ||
sharp(emoteBuffer).resize(156, 156).png().toBuffer(), | ||
]); | ||
} | ||
|
||
async function getTemplate() { | ||
const res = await fetch("https://r2.joulev.dev/files/ui4064gsyc4fslzmcj7bwfz9"); | ||
if (!res.ok) notFound(); | ||
const buffer = await res.arrayBuffer(); | ||
return sharp(buffer).png().toBuffer(); | ||
} | ||
|
||
export const GET: RouteHandler = async (_, { params }) => { | ||
const { emoteId } = params; | ||
const [[smallEmote, largeEmote], template] = await Promise.all([ | ||
getEmote(emoteId), | ||
getTemplate(), | ||
]); | ||
const canvas = sharp({ | ||
create: { width: 320, height: 224, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } }, | ||
}).composite([ | ||
{ input: template, top: 0, left: 0 }, | ||
{ input: smallEmote, top: 7, left: 100 }, | ||
{ input: largeEmote, top: 68, left: 82 }, | ||
]); | ||
const output = await canvas.png().toBuffer(); | ||
return new Response(output, { | ||
headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=604800, immutable" }, | ||
}); | ||
}; |
Oops, something went wrong.