src/common/common.controller.ts
common
Methods |
|
| Public Async data |
data()
|
Decorators :
@Get('/data')
|
|
Defined in src/common/common.controller.ts:37
|
|
Returns :
unknown
|
| Public domains |
domains()
|
Decorators :
@Get('/domains')
|
|
Defined in src/common/common.controller.ts:32
|
|
Returns :
any
|
| Public Async fetchLatestTranslationFile |
fetchLatestTranslationFile()
|
Decorators :
@Get('/translation')
|
|
Defined in src/common/common.controller.ts:48
|
|
Returns :
unknown
|
| Public Async getLatestDashVersion |
getLatestDashVersion()
|
Decorators :
@Get('/dashversion')
|
|
Defined in src/common/common.controller.ts:63
|
|
Returns :
unknown
|
| Public Async uploadTranslation | ||||||
uploadTranslation(req: FastifyRequest)
|
||||||
Decorators :
@Post('/translation/upload')
|
||||||
|
Defined in src/common/common.controller.ts:81
|
||||||
|
Parameters :
Returns :
unknown
|
import {
BadRequestException,
Controller,
Get,
Post,
Req,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Public } from './decorators/public.decorator';
import { DatabaseService } from '../database/database.service';
import { ApiKey } from './decorators/apikey.decorator';
import { HttpService } from '@nestjs/axios';
import { FastifyRequest } from 'fastify-multipart';
import { Readable } from 'stream';
import { UtilsService } from '../utils/utils.service';
import { BezosService } from '../cloud/bezos.service';
import { UsersService } from '../users/users.service';
import { MessageBuilder, Webhook } from 'discord-ts-webhook';
@Controller('common')
export class CommonController {
private readonly httpService: HttpService = new HttpService();
constructor(
private readonly configService: ConfigService,
private readonly databaseService: DatabaseService,
private readonly utilService: UtilsService,
private readonly bezosService: BezosService,
private readonly usersService: UsersService,
) {}
@Get('/domains')
@Public()
public domains() {
return this.configService.get('domains');
}
@Get('/data')
@Public()
public async data() {
const registers = await this.databaseService.read.users.count();
const uploads = await this.databaseService.read.uploads.count();
return {
registers,
uploads,
};
}
@Get('/translation')
@Public()
@ApiKey()
public async fetchLatestTranslationFile() {
const req = await this.httpService
.get(
'https://raw.githubusercontent.com/fileglass/Beryllium/prod/src/locales/en/translations.ts',
{
headers: {
authorization: `token ${this.configService.get('GH_KEY')}`,
},
},
)
.toPromise();
return req.data;
}
@Get('/dashversion')
@Public()
public async getLatestDashVersion() {
const req = await this.httpService
.get<string>(
'https://raw.githubusercontent.com/fileglass/Beryllium/prod/src/utils/version.ts?token=AGBR5NEFLMRXRRA4ESHQMCDBOKITS',
{
headers: {
authorization: `token ${this.configService.get('GH_KEY')}`,
},
},
)
.toPromise();
const RE = /\d+\.\d+\.\d+/;
return req.data.match(RE)[0];
}
@Post('/translation/upload')
@Public()
@ApiKey()
public async uploadTranslation(@Req() req: FastifyRequest) {
return new Promise((res, rej) => {
if (!req.isMultipart()) {
rej(new BadRequestException('Request is not multipart'));
return;
}
req.multipart(
async (
field: string,
file: Readable | Buffer,
filename: string,
encoding: string,
mimetype: string,
) => {
if (!filename.endsWith('.ts')) {
rej(new BadRequestException('File is not TypeScript'));
return;
}
file = await this.utilService.convertStreamToBuffer(file as Readable);
const bsize = Buffer.byteLength(file as Buffer);
if (bsize > 524288) {
rej(
new BadRequestException(
`File is too large, it shouldn't be more than 0.5 megabytes.`,
),
);
return;
}
const username = await this.usersService.getUsername(req.user);
const name = `${username}_${filename}`;
await this.bezosService.uploadToCdn(
file,
name,
mimetype,
'locales',
'private',
);
const hook = new Webhook(
`https://discordapp.com/api/webhooks/${this.configService.get(
'DEPL_WH_ID',
)}/${this.configService.get('DEPL_WH_SEC')}`,
);
hook.setUsername(username);
const msg = new MessageBuilder()
.setTimestamp()
.setDescription(
`${username} submitted a translation named ${filename}, please verify it manually in the AWS bucket under the \`locales\` folder`,
);
await hook.send(msg);
res('Submitted');
},
() => {},
);
});
}
}