-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from boostcampwm2023/server/feature/261
이미지 업로드 로직에 CLOVA GreenEye API 추가
- Loading branch information
Showing
8 changed files
with
223 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,69 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { CatchyException } from 'src/config/catchyException'; | ||
import { ERROR_CODE } from 'src/config/errorCode.enum'; | ||
import { GreenEyeResponseDto } from 'src/dto/greenEye.response.dto'; | ||
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; | ||
import { Logger } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class GreenEyeService { | ||
private readonly logger = new Logger('GreenEyeService'); | ||
greenEyeSecretKey: string; | ||
greenEyeRequestUrl: string; | ||
|
||
contentType: string = 'application/json'; | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
this.greenEyeSecretKey = configService.get<string>('GREEN_EYE_SECRET_KEY'); | ||
this.greenEyeRequestUrl = configService.get<string>( | ||
'GREEN_EYE_REQUEST_URL', | ||
); | ||
} | ||
|
||
private getTimeStamp(): number { | ||
return new Date().getTime(); | ||
} | ||
|
||
private getRequestInit(imageUrl: string): RequestInit { | ||
return { | ||
method: 'POST', | ||
headers: { | ||
'X-GREEN-EYE-SECRET': this.greenEyeSecretKey, | ||
'Content-Type': this.contentType, | ||
}, | ||
body: JSON.stringify({ | ||
version: 'V1', | ||
requestId: 'requestId', | ||
timestamp: this.getTimeStamp(), | ||
images: [ | ||
{ | ||
name: 'image', | ||
url: imageUrl, | ||
}, | ||
], | ||
}), | ||
}; | ||
} | ||
|
||
async getResultOfNormalImage(imageUrl: string): Promise<GreenEyeResponseDto> { | ||
return await fetch(this.greenEyeRequestUrl, this.getRequestInit(imageUrl)) | ||
.then((res) => { | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
|
||
throw new Error(); | ||
}) | ||
.catch((err) => { | ||
this.logger.error( | ||
`greenEye.service - getResultOfNormalImage : INVALID_GREEN_EYE_REQUEST`, | ||
); | ||
throw new CatchyException( | ||
'INVALID_GREEN_EYE_REQUEST', | ||
HTTP_STATUS_CODE.BAD_REQUEST, | ||
ERROR_CODE.INVALID_GREEN_EYE_REQUEST, | ||
); | ||
}); | ||
} | ||
} |
This file contains 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,39 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class GreenEyeResponseDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
version: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
requestId: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
timestamp: number; | ||
|
||
@IsNotEmpty() | ||
images: [ | ||
{ | ||
result: { | ||
adult: { | ||
confidence: number; | ||
}; | ||
normal: { | ||
confidence: number; | ||
}; | ||
porn: { | ||
confidence: number; | ||
}; | ||
sexy: { | ||
confidence: number; | ||
}; | ||
}; | ||
latency: number; | ||
confidence: number; | ||
message: string; | ||
name: string; | ||
}, | ||
]; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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