Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/remove nextjs #91

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ postgres/

# Migrations
**/database/migrations/*

# Local Uploads
/backend/uploads/*
78 changes: 60 additions & 18 deletions backend/package-lock.json

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

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-socket.io": "^9.0.0",
"@nestjs/serve-static": "^4.0.0",
"@nestjs/websockets": "^9.0.0",
"@prisma/client": "^4.12.0",
"argon2": "^0.30.3",
"argon2": "^0.31.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"cookie-parser": "^1.4.6",
Expand All @@ -57,6 +58,7 @@
"@types/express": "^4.17.13",
"@types/jest": "29.5.0",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.9",
"@types/node": "18.15.11",
"@types/passport-jwt": "^3.0.8",
"@types/passport-oauth2": "^1.4.12",
Expand Down
8 changes: 8 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { AuthModule } from './auth/auth.module';
import { FriendsModule } from './friends/friends.module';
import { LeaderboardModule } from './leaderboard/leaderboard.module';
import { MatchHistoryModule } from './match-history/match-history.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { AvatarUploadModule } from './avatar-upload/avatar-upload.module';

@Module({
imports: [
Expand All @@ -23,6 +26,11 @@ import { MatchHistoryModule } from './match-history/match-history.module';
FriendsModule,
LeaderboardModule,
MatchHistoryModule,
AvatarUploadModule,
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'uploads'),
serveRoot: '/avatars',
}),
],
providers: [PrismaService],
})
Expand Down
20 changes: 20 additions & 0 deletions backend/src/avatar-upload/avatar-upload.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AvatarUploadController } from './avatar-upload.controller';
import { AvatarUploadService } from './avatar-upload.service';

describe('AvatarUploadController', () => {
let controller: AvatarUploadController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AvatarUploadController],
providers: [AvatarUploadService],
}).compile();

controller = module.get<AvatarUploadController>(AvatarUploadController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
69 changes: 69 additions & 0 deletions backend/src/avatar-upload/avatar-upload.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
Controller,
Post,
Patch,
Delete,
UseInterceptors,
UploadedFile,
ParseFilePipe,
MaxFileSizeValidator,
FileTypeValidator,
UseGuards,
Req,
} from '@nestjs/common';
import { AvatarUploadService } from './avatar-upload.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { AccessTokenGuard } from 'src/auth/jwt/jwt.guard';
import { User } from '@prisma/client';

const fileValidators = [
new MaxFileSizeValidator({
// 1MB
maxSize: 1024 * 1024,
}),
new FileTypeValidator({ fileType: /image\/(png|jpg|gif)/ }),
];

@Controller('avatar-upload')
@UseGuards(AccessTokenGuard)
export class AvatarUploadController {
constructor(private readonly avatarUploadService: AvatarUploadService) {}

@Post()
@UseInterceptors(FileInterceptor('file'))
create(
@UploadedFile(
new ParseFilePipe({
validators: fileValidators,
}),
)
file: Express.Multer.File,
@Req() request: Request & { user: User },
) {
const { id } = request.user;

return this.avatarUploadService.create(id, file);
}

@Patch()
update(
@UploadedFile(
new ParseFilePipe({
validators: fileValidators,
}),
)
file: Express.Multer.File,
@Req() request: Request & { user: User },
) {
const { id } = request.user;

return this.avatarUploadService.update(id, file);
}

@Delete()
remove(@Req() request: Request & { user: User }) {
const { id } = request.user;

return this.avatarUploadService.remove(id);
}
}
25 changes: 25 additions & 0 deletions backend/src/avatar-upload/avatar-upload.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Module } from '@nestjs/common';
import { AvatarUploadService } from './avatar-upload.service';
import { AvatarUploadController } from './avatar-upload.controller';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import * as crypto from 'crypto';
import { PrismaService } from 'src/prisma/prisma.service';

@Module({
imports: [
MulterModule.register({
storage: diskStorage({
destination: './uploads',
filename: (req, file, cb) => {
const randomName = crypto.randomUUID();
const extension = file.originalname.split('.').pop();
return cb(null, `${randomName}.${extension}`);
},
}),
}),
],
controllers: [AvatarUploadController],
providers: [AvatarUploadService, PrismaService],
})
export class AvatarUploadModule {}
18 changes: 18 additions & 0 deletions backend/src/avatar-upload/avatar-upload.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AvatarUploadService } from './avatar-upload.service';

describe('AvatarUploadService', () => {
let service: AvatarUploadService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AvatarUploadService],
}).compile();

service = module.get<AvatarUploadService>(AvatarUploadService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
36 changes: 36 additions & 0 deletions backend/src/avatar-upload/avatar-upload.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class AvatarUploadService {
constructor(private prisma: PrismaService) {}

async create(id: string, file: Express.Multer.File) {
const { filename } = file;
const user = await this.prisma.user.update({
where: { id: id },
data: { avatar: filename },
});

return user;
}

async update(id: string, file: Express.Multer.File) {
const { filename } = file;
const user = await this.prisma.user.update({
where: { id: id },
data: { avatar: filename },
});

return user;
}

async remove(id: string) {
const user = await this.prisma.user.update({
where: { id: id },
data: { avatar: null },
});

return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateAvatarUploadDto {}
4 changes: 4 additions & 0 deletions backend/src/avatar-upload/dto/update-avatar-upload.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateAvatarUploadDto } from './create-avatar-upload.dto';

export class UpdateAvatarUploadDto extends PartialType(CreateAvatarUploadDto) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class AvatarUpload {}
3 changes: 2 additions & 1 deletion backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@
}
}

afterInit(_: Server) {

Check warning on line 556 in backend/src/chat/chat.gateway.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used
this.server.use((socket, next) => {
this.validateConnection(socket)
.then((user) => {
Expand All @@ -569,7 +569,8 @@
}

private validateConnection(client: Socket) {
const token = client.handshake.headers.cookie.split(';')[0].split('=')[1];
const token = client.handshake.auth.token;

try {
const payload = this.jwtService.verify<TokenPayload>(token, {
secret: process.env.JWT_SECRET,
Expand Down
Loading
Loading