Skip to content

Commit

Permalink
Merge pull request #481 from boostcampwm2023/BE-GreenEye-#476
Browse files Browse the repository at this point in the history
[BE/#476] Green Eye 적용 코드 구현
  • Loading branch information
namewhat99 authored Dec 11, 2023
2 parents 59cb1f6 + 62f886f commit 6e48f0d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
3 changes: 2 additions & 1 deletion BE/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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 {}
15 changes: 11 additions & 4 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 },
Expand Down
36 changes: 36 additions & 0 deletions BE/src/utils/greenEyeHandler.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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'),
},
},
);
}
}
53 changes: 0 additions & 53 deletions BE/src/utils/ocrHandler.ts

This file was deleted.

0 comments on commit 6e48f0d

Please sign in to comment.