Skip to content

Commit

Permalink
feat: compress avater
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 28, 2023
1 parent 274110f commit 47cd779
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following parameters are available:
- space: The spacing between avatars (default: 5)
- no_bot: Do not show bots (default: false)
- min_contributions: Only show contributors with at least this number of contributions (default: 0)
- compress: The height/width of each avatar after compression (default: radius \* 4, 0 to disable)

## Example

Expand Down
1 change: 1 addition & 0 deletions app/(empty)/iframe/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default function Page() {
users: users,
no_bot: searchParams.getAll("no_bot").length > 0,
min_contributions: searchParams.get("min_contributions"),
compress: searchParams.get("compress"),
})
const usersGroup = params.users.reduce((acc, user) => {
const index = acc.findIndex((group) => group.length < params.cols)
Expand Down
7 changes: 2 additions & 5 deletions app/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const repoNotFoundCache = new LRUCache<string, boolean>({
ttl: 1000 * 60 * 60 * 12,
})

const avatarCache = new LRUCache<string, string>({
const avatarCache = new LRUCache<string, Buffer>({
max: 2000,
ttl: 1000 * 60 * 60 * 24,
})
Expand Down Expand Up @@ -136,14 +136,11 @@ export async function fetchRepos(repos: string[], maxPages?: number) {
}

export async function fetchAvatar(url: string) {
if (!url.startsWith("http")) {
return url
}
if (avatarCache.has(url)) {
return avatarCache.get(url)!
}
const response = await fetch(url)
const res = Buffer.from(await response.arrayBuffer()).toString("base64")
const res = Buffer.from(await response.arrayBuffer())
avatarCache.set(url, res)
return res
}
1 change: 1 addition & 0 deletions app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
space: searchParams.get("space"),
no_bot: searchParams.getAll("no_bot").length > 0,
min_contributions: searchParams.get("min_contributions"),
compress: searchParams.get("compress"),
})
return new NextResponse(svg, {
headers: {
Expand Down
8 changes: 6 additions & 2 deletions app/api/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ export async function generateSVG(
conf: { repos: string[]; maxPages?: number } & Omit<calParamsArg, "users">
) {
const users = (await fetchRepos(conf.repos, conf.maxPages)) as GhUser[]
const params = calParams({ ...conf, users: users })
const avatars = await Promise.all(
users.map(async (user: GhUserUse) => await fetchAvatar(user.avatar_url))
params.users.map(async (user: GhUserUse) => {
const buf = await fetchAvatar(user.avatar_url)
const compressed = await params.compress(buf)
return compressed.toString("base64")
})
)
const params = calParams({ ...conf, users: users })
let svg = getHead(params)
for (let i = 0; i < params.total; i++) {
svg += getUser(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"postcss": "8.4.29",
"react": "18.2.0",
"react-dom": "18.2.0",
"sharp": "^0.32.6",
"swr": "^2.2.4",
"tailwind-variants": "^0.1.13",
"tailwindcss": "3.3.3",
Expand Down
Loading

0 comments on commit 47cd779

Please sign in to comment.