-
Notifications
You must be signed in to change notification settings - Fork 16
feat(be): implement polygon tool upload #3527
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
Open
zero1177
wants to merge
6
commits into
main
Choose a base branch
from
t2630-implement-file-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
569fb04
feat(be): implement polygon tool upload
zero1177 034d3ff
feat(be): implement publication service for polygon to run tool files…
zero1177 71d4d60
fix(be): fix fieldName filePath into fileContent
zero1177 0c8d86e
feat(be): add polygon amqp service logic
zero1177 6a1eca4
chore(be): commit migration file
zero1177 66a2346
feat(be): add message result interface
zero1177 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,49 @@ | ||
| import { Injectable } from '@nestjs/common' | ||
| import type { ToolType } from '@prisma/client' | ||
| import type { FileUpload } from 'graphql-upload/processRequest.mjs' | ||
| import { UnprocessableDataException } from '@libs/exception' | ||
| import { PrismaService } from '@libs/prisma' | ||
|
|
||
| const MAX_TOOL_FILE_SIZE = 10 * 1024 * 1024 // 10MB | ||
|
|
||
| @Injectable() | ||
| export class FileService { | ||
| constructor(private readonly prisma: PrismaService) {} | ||
|
|
||
| async uploadPolygonToolFile( | ||
| problemId: number, | ||
| toolType: ToolType, | ||
| file: FileUpload | ||
| ) { | ||
| const { filename, createReadStream } = file | ||
|
|
||
| //ReadStream → [chunk1, chunk2, chunk3, ...] → Buffer.concat | ||
| //→ 최종 Buffer로 변환해 → DB(PostgreSQL)에 저장 | ||
| const chunks: Buffer[] = [] | ||
| let total = 0 | ||
| for await (const chunk of createReadStream()) { | ||
| total += chunk.length | ||
| if (total > MAX_TOOL_FILE_SIZE) { | ||
| throw new UnprocessableDataException('File size exceeds maximum limit') | ||
| } | ||
| chunks.push(chunk) | ||
| } | ||
| const fileContent = Buffer.concat(chunks).toString('utf-8') | ||
|
|
||
| // (problemId, toolType) unique — 재업로드 시 갱신 | ||
| const tool = await this.prisma.polygonTool.upsert({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| where: { problemId_toolType: { problemId, toolType } }, | ||
| update: { fileName: filename, fileContent }, | ||
| create: { problemId, toolType, fileName: filename, fileContent } | ||
| }) | ||
| return tool | ||
| } | ||
|
|
||
| async deletePolygonFile(problemId: number, toolType: ToolType) { | ||
| return await this.prisma.polygonTool.delete({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| where: { problemId_toolType: { problemId, toolType } } | ||
| }) | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
apps/backend/apps/admin/src/polygon/interface/polygonToolRequest.interface.ts
This file contains hidden or 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,15 @@ | ||
| export interface GeneratorRequest { | ||
| problemId: number | ||
| generatorLanguage: string | ||
| generatorCode: string | ||
| generatorArgs: string[] | ||
| solutionLanguage: string | ||
| solutionCode: string | ||
| testCaseCount: number | ||
| } | ||
|
|
||
| export interface ValidatorRequest { | ||
| problemId: number | ||
| language: string | ||
| validatorCode: string | ||
| } |
23 changes: 23 additions & 0 deletions
23
apps/backend/apps/admin/src/polygon/interface/polygonToolResult.interface.ts
This file contains hidden or 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,23 @@ | ||
| export interface GeneratorResultMessage { | ||
| submissionId: number | ||
| resultCode: number | ||
| judgeResult: { | ||
| generatedTestCases: number | ||
| totalTestCases: number | ||
| } | ||
| error: string | ||
| } | ||
|
|
||
| export interface ValidatorResultMessage { | ||
| submissionId: number | ||
| resultCode: number | ||
| judgeResult: { | ||
| isValid: boolean | ||
| testcaseCount: number | ||
| results: Array<{ | ||
| id: number | ||
| isValid: boolean | ||
| }> | ||
| } | ||
| error: string | ||
| } |
55 changes: 55 additions & 0 deletions
55
apps/backend/apps/admin/src/polygon/polygon-pub.service.ts
This file contains hidden or 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,55 @@ | ||
| import { Language, ToolType } from '@prisma/client' | ||
| import type { PolygonAMQPService } from '@libs/amqp' | ||
| import type { PrismaService } from '@libs/prisma' | ||
|
|
||
| export class PolygonPublicationService { | ||
| constructor( | ||
| private readonly prisma: PrismaService, | ||
| private readonly amqpService: PolygonAMQPService | ||
| ) {} | ||
|
|
||
| async publishGeneratorMessage( | ||
| problemId: number, | ||
| generatorArgs: string[], | ||
| testCaseCount: number | ||
| ) { | ||
| //DB에서 generator, solution 조회 | ||
| const [generator, solution] = await Promise.all([ | ||
| this.prisma.polygonTool.findUniqueOrThrow({ | ||
| where: { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| problemId_toolType: { problemId, toolType: ToolType.Generator } | ||
| } | ||
| }), | ||
| this.prisma.polygonSolution.findUniqueOrThrow({ | ||
| where: { problemId } | ||
| }) | ||
| ]) | ||
|
|
||
| //실행 요청 메시지 publish | ||
| await this.amqpService.publishGeneratorMessage({ | ||
| problemId, | ||
| generatorLanguage: Language.Cpp, | ||
| generatorCode: generator.fileContent, | ||
| generatorArgs, | ||
| solutionLanguage: solution.language, | ||
| solutionCode: solution.fileContent, | ||
| testCaseCount | ||
| }) | ||
| } | ||
|
|
||
| async publishValidatorMessage(problemId: number) { | ||
| const validator = await this.prisma.polygonTool.findUniqueOrThrow({ | ||
| where: { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| problemId_toolType: { problemId, toolType: ToolType.Validator } | ||
| } | ||
| }) | ||
|
|
||
| await this.amqpService.publishValidatorMessage({ | ||
| problemId, | ||
| language: Language.Cpp, | ||
| validatorCode: validator.fileContent | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import { Module } from '@nestjs/common' | ||
| import { AMQPModule } from '@libs/amqp' | ||
| import { RolesModule } from '@libs/auth' | ||
| import { FileService } from './file/file.service' | ||
| import { PolygonResolver } from './polygon.resolver' | ||
| import { PolygonService } from './polygon.service' | ||
|
|
||
| @Module({ | ||
| imports: [RolesModule], | ||
| providers: [PolygonResolver, PolygonService] | ||
| imports: [RolesModule, AMQPModule], | ||
| providers: [PolygonResolver, PolygonService, FileService] | ||
| }) | ||
| export class PolygonModule {} |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,34 @@ | ||
| import { Resolver } from '@nestjs/graphql' | ||
| import { Args, Int, Mutation, Resolver } from '@nestjs/graphql' | ||
| import { ToolType } from '@prisma/client' | ||
| import GraphQLUpload from 'graphql-upload/GraphQLUpload.mjs' | ||
| import type { FileUpload } from 'graphql-upload/processRequest.mjs' | ||
| import { UseDisableAdminGuard } from '@libs/auth' | ||
| import { PolygonProblem } from '@admin/@generated' | ||
| import { PolygonProblem, PolygonTool } from '@admin/@generated' | ||
| import { PolygonService } from './polygon.service' | ||
|
|
||
| @Resolver(() => PolygonProblem) | ||
| @UseDisableAdminGuard() | ||
| export class PolygonResolver { | ||
| constructor(private readonly polygonService: PolygonService) {} | ||
|
|
||
| @Mutation(() => PolygonTool) | ||
| async uploadPolygonTool( | ||
| @Args('problemId', { type: () => Int }) problemId: number, | ||
| @Args('toolType', { type: () => ToolType }) toolType: ToolType, | ||
| @Args('file', { type: () => GraphQLUpload }) file: Promise<FileUpload> | ||
| ) { | ||
| return this.polygonService.uploadPolygonTool( | ||
| problemId, | ||
| toolType, | ||
| await file | ||
| ) | ||
| } | ||
|
|
||
| @Mutation(() => PolygonTool) | ||
| async deletePolygonTool( | ||
| @Args('problemId', { type: () => Int }) problemId: number, | ||
| @Args('toolType', { type: () => ToolType }) toolType: ToolType | ||
| ) { | ||
| return this.polygonService.deletePolygonTool(problemId, toolType) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,47 @@ | ||
| import { Injectable } from '@nestjs/common' | ||
| import { ToolType } from '@prisma/client' | ||
| import type { FileUpload } from 'graphql-upload/processRequest.mjs' | ||
| import { PrismaService } from '@libs/prisma' | ||
| import { FileService } from './file/file.service' | ||
| import { PolygonPublicationService } from './polygon-pub.service' | ||
|
|
||
| @Injectable() | ||
| export class PolygonService { | ||
| constructor(private readonly prisma: PrismaService) {} | ||
| constructor( | ||
| private readonly prisma: PrismaService, | ||
| private readonly fileService: FileService, | ||
| private readonly publicationService: PolygonPublicationService | ||
| ) {} | ||
|
|
||
| async uploadPolygonTool( | ||
| problemId: number, | ||
| toolType: ToolType, | ||
| file: FileUpload | ||
| ) { | ||
| //DB에 파일 저장 | ||
| await this.fileService.uploadPolygonToolFile(problemId, toolType, file) | ||
| } | ||
|
|
||
| async deletePolygonTool(problemId: number, toolType: ToolType) { | ||
| return this.fileService.deletePolygonFile(problemId, toolType) | ||
| } | ||
|
|
||
| //파일 실행 | ||
| async runGenerator( | ||
| problemId: number, | ||
| generatorArgs: string[], | ||
| testCaseCount: number | ||
| ) { | ||
| await this.publicationService.publishGeneratorMessage( | ||
| problemId, | ||
| generatorArgs, | ||
| testCaseCount | ||
| ) | ||
| } | ||
|
|
||
| async runValidator(problemId: number) { | ||
| await this.publicationService.publishValidatorMessage(problemId) | ||
| } | ||
|
|
||
| //테스트케이스 저장 | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
파일 내용을
utf-8문자열로 변환하여 DB에 저장하고 있습니다. PR 설명에 따르면.cpp소스코드를 상정하고 있으나, 사용자가 바이너리 파일 등 의도하지 않은 형식의 파일을 업로드할 경우 데이터가 손상될 수 있습니다. 업로드된 파일의mimetype이나 확장자를 검증하는 로직을 추가하여 의도한 형식의 파일만 처리하도록 하는 것이 안전합니다.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확장자 검증 추가하겠습니다..!