diff --git a/BE/src/users/users.module.ts b/BE/src/users/users.module.ts index f495c64..dc00ee3 100644 --- a/BE/src/users/users.module.ts +++ b/BE/src/users/users.module.ts @@ -11,6 +11,7 @@ import { BlockPostEntity } from '../entities/blockPost.entity'; import { AuthGuard } from 'src/utils/auth.guard'; import { RegistrationTokenEntity } from '../entities/registrationToken.entity'; import { FcmHandler } from 'src/utils/fcmHandler'; +import { GreenEyeHandler } from '../utils/greenEyeHandler'; @Module({ imports: [ @@ -24,6 +25,6 @@ import { FcmHandler } from 'src/utils/fcmHandler'; ]), ], controllers: [UsersController], - providers: [UsersService, S3Handler, AuthGuard, FcmHandler], + providers: [UsersService, S3Handler, AuthGuard, FcmHandler, GreenEyeHandler], }) export class UsersModule {} diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index 8a1e0b5..b17e93f 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -15,6 +15,8 @@ import { ConfigService } from '@nestjs/config'; import * as jwt from 'jsonwebtoken'; import { FcmHandler } from 'src/utils/fcmHandler'; import { CACHE_MANAGER, CacheStore } from '@nestjs/cache-manager'; +import { GreenEyeHandler } from '../utils/greenEyeHandler'; +import { log } from 'winston'; @Injectable() export class UsersService { @@ -31,6 +33,7 @@ export class UsersService { private s3Handler: S3Handler, private configService: ConfigService, private fcmHandler: FcmHandler, + private ocrHandler: GreenEyeHandler, ) {} async createUser(imageLocation: string, createUserDto: CreateUserDto) { @@ -98,12 +101,12 @@ export class UsersService { userId: string, ) { await this.checkAuth(id, userId); - if (body) { - await this.changeNickname(id, body.nickname); - } if (file !== undefined) { await this.changeImages(id, file); } + if (body) { + await this.changeNickname(id, body.nickname); + } } async changeNickname(userId: string, nickname: string) { @@ -118,8 +121,12 @@ export class UsersService { } async changeImages(userId: string, file: Express.Multer.File) { + const fileLocation = await this.s3Handler.uploadFile(file); + const isHarmful = await this.ocrHandler.isHarmful(fileLocation); + // if (isHarmful) { + // throw new HttpException('이미지가 유해합니다.', 400); + // } try { - const fileLocation = await this.s3Handler.uploadFile(file); await this.userRepository.update( { user_hash: userId }, { profile_img: fileLocation }, diff --git a/BE/src/utils/greenEyeHandler.ts b/BE/src/utils/greenEyeHandler.ts new file mode 100644 index 0000000..f1c8bd0 --- /dev/null +++ b/BE/src/utils/greenEyeHandler.ts @@ -0,0 +1,36 @@ +import axios from 'axios'; +import { uuid } from 'uuidv4'; +import { ConfigService } from '@nestjs/config'; +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class GreenEyeHandler { + constructor(private configService: ConfigService) {} + async isHarmful(fileLocation: string): Promise { + const response = await this.sendGreenEyeRequest(fileLocation); + const normalResult = response.data.images[0].result.normal.confidence; + return normalResult < 0.8; + } + + async sendGreenEyeRequest(url: string) { + return await axios.post( + this.configService.get('CLOVA_URL'), + { + images: [ + { + name: 'file', + url: url, + }, + ], + requestId: uuid(), + timestamp: 0, + version: 'V1', + }, + { + headers: { + 'X-GREEN-EYE-SECRET': this.configService.get('CLOVA_SECRET'), + }, + }, + ); + } +} diff --git a/BE/src/utils/ocrHandler.ts b/BE/src/utils/ocrHandler.ts deleted file mode 100644 index 75b8965..0000000 --- a/BE/src/utils/ocrHandler.ts +++ /dev/null @@ -1,53 +0,0 @@ -import * as path from 'path'; -import axios from 'axios'; -import { uuid } from 'uuidv4'; -import { ConfigService } from '@nestjs/config'; -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class OcrHandler { - constructor(private configService: ConfigService) {} - async convertImageToText(file: Express.Multer.File): Promise { - const res = await this.sendOcrRequest(file); - let text; - if (res.status === 200) { - text = this.parseTextFromOcr(res.data.images[0].fields); - return text; - } else { - return null; - } - } - parseTextFromOcr(fields: Array): string { - let text: string = ''; - for (let i = 0; i < fields.length; i++) { - text += fields[i]['inferText'] + ' '; - if (fields[i]['lineBreak'] === true) text += '\n'; - } - return text; - } - - async sendOcrRequest(file: Express.Multer.File) { - const imgInfo = path.parse(file.originalname); - const imgBuffer = file.buffer; - return await axios.post( - this.configService.get('CLOVA_URL'), // APIGW Invoke URL - { - images: [ - { - format: imgInfo.ext.slice(1), // file format - name: imgInfo.name, // image name - data: imgBuffer.toString('base64'), // image base64 string(only need part of data). Example: base64String.split(',')[1] - }, - ], - requestId: uuid(), // unique string - timestamp: 0, - version: 'V2', - }, - { - headers: { - 'X-OCR-SECRET': this.configService.get('CLOVA_SECRET'), // Secret Key - }, - }, - ); - } -}