Skip to content

Commit

Permalink
Merge pull request #14 from wang-bam-bbang/masking
Browse files Browse the repository at this point in the history
feat: apply ocr masking image feature in image upload process
  • Loading branch information
Kimcheolhui authored Dec 9, 2024
2 parents 62f3019 + bbddd43 commit 71a41d3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.7",
"express-basic-auth": "^1.2.1",
"form-data": "^4.0.1",
"passport": "^0.7.0",
"passport-http-bearer": "^1.0.1",
"passport-oauth2": "^1.8.0",
Expand Down
6 changes: 6 additions & 0 deletions src/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export class CommentService {
}

async getPostComments(postId: number): Promise<CommentResponseDto[]> {
const post = this.postService.getPostById(postId);

if (!post) {
throw new NotFoundException('Post not found');
}

return this.commentRepository.getPostComments(postId);
}

Expand Down
47 changes: 45 additions & 2 deletions src/image/image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import axios from 'axios';
import FormData from 'form-data';
import sharp from 'sharp';

@Injectable()
Expand Down Expand Up @@ -53,7 +54,13 @@ export class ImageService {
private async uploadImage(file: Express.Multer.File): Promise<string> {
const key = `${Date.now()}-${Math.random().toString(36).substring(2)}${file.originalname}`;

const webpFile = await this.convertToWebp(file);
// OCR Masking
const maskedImageBuffer = await this.maskingImageWithFlask(file);

const webpFile = await this.convertToWebp({
...file,
buffer: maskedImageBuffer,
});

const command = new PutObjectCommand({
Bucket: this.bucketName,
Expand Down Expand Up @@ -172,4 +179,40 @@ export class ImageService {
throw new InternalServerErrorException('Failed to generate signed URLs');
}
}

/**
* AI feature 서버에 이미지를 보내서 OCR 마스킹 처리
* @param file Express.Multer.File
* @returns Buffer (마스킹된 이미지 데이터)
*/
private async maskingImageWithFlask(
file: Express.Multer.File,
): Promise<Buffer> {
const flaskApiUrl = this.configService.get<string>('FLASK_API_URL'); // Flask API URL
const flaskApiKey = this.configService.get<string>('FLASK_API_KEY'); // Flask API Key

try {
const formData = new FormData();
formData.append('image', file.buffer, file.originalname);

const response = await axios.post(
`${flaskApiUrl}/process_image`,
formData,
{
headers: {
'X-API-KEY': flaskApiKey,
...formData.getHeaders(),
},
responseType: 'arraybuffer', // 이미지 데이터를 Buffer로 받기
},
);

return Buffer.from(response.data); // 마스킹된 이미지 데이터 반환
} catch (error) {
console.error('Error calling Flask API:', error.message);
throw new InternalServerErrorException(
'Failed to process image with Flask API',
);
}
}
}

0 comments on commit 71a41d3

Please sign in to comment.