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

[BE/#476] Green Eye 적용 코드 구현 #481

Merged
merged 2 commits into from
Dec 11, 2023
Merged
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: 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다~ 이미지 url 이 존재해야 isHarmful 처리가 가능해서 먼저 s3 에 저장했네요 굳굳!

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.

Loading