-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite code and correct buffers
- Loading branch information
Showing
12 changed files
with
149 additions
and
147 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
This file was deleted.
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,32 @@ | ||
import {Controller, Get, Header, Param, Req, Res, StreamableFile} from '@nestjs/common'; | ||
import { ApiNotFoundResponse, ApiOkResponse, ApiOperation } from "@nestjs/swagger"; | ||
import { createReadStream } from "node:fs"; | ||
import { Readable } from 'stream'; | ||
|
||
import { SocialCardService } from "./social-card.service"; | ||
|
||
@Controller('users') | ||
export class SocialCardController { | ||
constructor( | ||
private readonly socialCardService: SocialCardService | ||
) {} | ||
|
||
@Get("/:username") | ||
@ApiOperation({ | ||
operationId: "generateUserSocialCard", | ||
summary: "Gets latest cache aware social card link for :username or generates a new one", | ||
}) | ||
@ApiOkResponse({ type: String }) | ||
@ApiNotFoundResponse({ description: "User not found" }) | ||
async generateUserSocialCard ( | ||
@Param("username") username: string, | ||
@Req() request: Request, | ||
@Res() response: Response | ||
): Promise<StreamableFile> { | ||
const image = await this.socialCardService.getUserCard(username); | ||
|
||
console.log(image); | ||
|
||
return new StreamableFile(image); | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HttpModule } from "@nestjs/axios"; | ||
import { SocialCardService } from './social-card.service'; | ||
import { SocialCardController } from './social-card.controller'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [SocialCardService], | ||
controllers: [SocialCardController] | ||
}) | ||
export class SocialCardModule {} |
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,96 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpService } from "@nestjs/axios"; | ||
import { catchError, firstValueFrom, map, throwError } from "rxjs"; | ||
import { readFile } from "node:fs/promises"; | ||
import { Resvg } from "@resvg/resvg-js"; | ||
|
||
import userLangs from "./templates/user-langs"; | ||
import userProfileRepos from "./templates/user-profile-repos"; | ||
import userProfileCard from "./templates/user-profile-card"; | ||
|
||
@Injectable() | ||
export class SocialCardService { | ||
constructor( | ||
private readonly httpService: HttpService, | ||
) {} | ||
|
||
async getUserData(username: string): Promise<{ | ||
langs: string[], | ||
repos: string[], | ||
img: string, | ||
}> { | ||
const request= this.httpService | ||
.get(`https://beta.gs-api.opensauced.pizza/v1/*/contributions?page=1&limit=1&range=30&contributor=${username}`) | ||
.pipe( | ||
map((res) => res.data), | ||
) | ||
.pipe( | ||
catchError((err) => { | ||
console.log(err); | ||
return throwError(err); | ||
} | ||
)); | ||
|
||
const { data } = await firstValueFrom(request); | ||
|
||
const contributor = data[0]; | ||
|
||
if (!contributor) { | ||
throw new Error(`User '${username}' Not Found`); | ||
} | ||
|
||
const langs = contributor.langs ? contributor.langs.split(",") : []; | ||
const repos = contributor.recent_repo_list ? contributor.recent_repo_list.split(",") : []; | ||
|
||
const imgReq = this.httpService | ||
.get(`https://www.github.com/${username}.png?size=300`, { | ||
responseType: 'text', | ||
responseEncoding: 'base64' | ||
}) | ||
.pipe( | ||
catchError((err) => { | ||
console.log(err); | ||
return throwError(err); | ||
} | ||
)); | ||
|
||
const { data: img } = await firstValueFrom(imgReq); | ||
|
||
return { | ||
langs, | ||
repos, | ||
img, | ||
}; | ||
} | ||
|
||
async getUserCard(username: string): Promise<Buffer> { | ||
const { html } = await import("satori-html"); | ||
const satori = (await import("satori")).default; | ||
|
||
const { img, repos, langs } = await this.getUserData(username); | ||
|
||
const template = html(userProfileCard(img, username, userLangs(langs), userProfileRepos(repos))); | ||
|
||
const robotoArrayBuffer = await readFile("public/Roboto-Regular.ttf"); | ||
const svg = await satori(template, { | ||
width: 1200, | ||
height: 627, | ||
fonts: [ | ||
{ | ||
name: "Roboto", | ||
data: robotoArrayBuffer, | ||
weight: 400, | ||
style: "normal", | ||
}, | ||
], | ||
}); | ||
|
||
const resvg = new Resvg(svg, { background: "rgba(238, 235, 230, .9)" }); | ||
|
||
const pngData = resvg.render(); | ||
|
||
const pngBuffer = pngData.asPng(); | ||
|
||
return pngBuffer; | ||
} | ||
} |
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
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
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