Skip to content

Commit

Permalink
added bandage og
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Sep 11, 2024
1 parent ac557d8 commit 5ff856e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@prisma/client": "^5.15.0",
"@types/jsonwebtoken": "^9.0.6",
"@types/morgan": "^1.9.9",
"axios": "^1.7.2",
"axios": ">=1.7.4",
"cache-manager": "^5.7.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down Expand Up @@ -72,4 +72,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
1 change: 0 additions & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { UpdateUsersDto } from './dto/updateUser.dto';
import { generate_response } from 'src/common/bandage_response';
import { RolesEnum } from 'src/interfaces/types';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { Console } from 'console';

const discord_url = "https://discord.com/api/v10";

Expand Down
58 changes: 47 additions & 11 deletions src/workshop/bandage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,69 @@ export class BandageService {
return categories;
}

async getBandage(id: string, session: Session) {
/* get bandage by external id */

async _getBandage(id: string, session: Session | null) {
const bandage = await this.prisma.bandage.findFirst({
where: { externalId: id },
include: { User: { include: { UserSettings: true } }, categories: true, stars: true }
});
if (!bandage) {
return null;
}
const hidden = bandage.categories.some(val => val.only_admins);
const no_access = session ? !hasAccess(session.user, RolesEnum.ManageBandages) && session.user.id !== bandage.User?.id : true;
if ((hidden || bandage.access_level === 0) && no_access) {
return null;
}

return bandage;
}

async getDataForOg(id: string) {
const bandage = await this._getBandage(id, null);

if (!bandage) {
return {
message: "Bandage not found",
statusCode: 404
};
}
const hidden = bandage.categories.some(val => val.only_admins);
const no_access = session ? !hasAccess(session.user, RolesEnum.ManageBandages) && session.user.id !== bandage.User?.id : true;
if ((hidden || bandage.access_level === 0) && no_access) {

const buff = Buffer.from(bandage.base64, 'base64');
const { data } = await sharp(buff).resize(1, 1, { fit: 'inside' }).extract({ left: 0, top: 0, width: 1, height: 1 }).raw().toBuffer({ resolveWithObject: true });
const [r, g, b, a] = data;

return {
statusCode: 200,
data: {
id: bandage.id,
external_id: bandage.externalId,
title: bandage.title,
description: bandage.description,
average_og_color: rgbToHex(r, g, b),
stars_count: bandage.stars.length,
author: {
id: bandage.User?.id,
name: bandage.User?.reserved_name || bandage.User?.name,
username: bandage.User?.username,
public: bandage.User && Number(bandage.User?.discordId) > 0 ? bandage.User?.UserSettings?.public_profile : false
}
}
}
}

async getBandage(id: string, session: Session) {
/* get bandage by external id */

const bandage = await this._getBandage(id, session);

if (!bandage) {
return {
message: "Bandage not found",
statusCode: 404
};
}

const hidden = bandage.categories.some(val => val.only_admins);
let permissions_level = 0;
if (session) {
if (session.user.id === bandage.User?.id) permissions_level = 1;
Expand All @@ -263,10 +304,6 @@ export class BandageService {
if (bandage.categories.some(val => val.id === 4)) check = "under review";
if (bandage.categories.some(val => val.id === 13)) check = "denied";

const buff = Buffer.from(bandage.base64, 'base64');
const { data, info } = await sharp(buff).resize(1, 1, { fit: 'inside' }).extract({ left: 0, top: 0, width: 1, height: 1 }).raw().toBuffer({ resolveWithObject: true });
const [r, g, b, a] = data;

return {
statusCode: 200,
data: {
Expand All @@ -277,7 +314,6 @@ export class BandageService {
base64: bandage.base64,
base64_slim: bandage.split_type ? bandage.base64_slim : undefined,
split_type: bandage.split_type,
average_og_color: rgbToHex(r, g, b),
creation_date: bandage.creationDate,
stars_count: bandage.stars.length,
starred: bandage.stars.some(val => val.id == session?.user.id),
Expand Down
15 changes: 14 additions & 1 deletion src/workshop/workshop.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, HttpStatus, Param, Query, Req, Res, StreamableFile, Delete, Put, Post, Body, UseGuards, ValidationPipe, UsePipes } from '@nestjs/common';
import type { Response } from 'express'
import type { Response, Request } from 'express'
import { BandageService } from "./bandage.service";
import { SkipThrottle, Throttle } from '@nestjs/throttler';
import { AuthGuard } from 'src/guards/auth.guard';
Expand Down Expand Up @@ -82,6 +82,19 @@ export class WorkshopController {
res.status(data.statusCode).send(data);
}

@Get("/workshop/:id/info")
@SkipThrottle()
async getBandageOg(@Param('id') id: string, @Req() request: Request, @Res() res: Response): Promise<void> {
/* get bandage info by external id (internal endpoint) */

if (request.headers['unique-access'] !== process.env.WORKSHOP_TOKEN) {
res.status(403).send({ message: 'Forbidden', statusCode: 403 });
return;
}
const data = await this.bandageService.getDataForOg(id);
res.status(data.statusCode).send(data);
}

@Get(["/workshop/:id/as_image", "/workshop/:id/og"])
@Auth(AuthEnum.Weak)
async getBandageImage(@Param('id') id: string, @Req() request: RequestSession, @Res({ passthrough: true }) res: Response, @Query() query: { width: number }): Promise<StreamableFile | void> {
Expand Down

0 comments on commit 5ff856e

Please sign in to comment.