|
1 | 1 | import {NextFunction, Request, Response} from 'express'; |
2 | 2 | import {detectLabels} from '../services/tags-ai.service.js'; |
3 | 3 | import {StatusCodes} from 'http-status-codes'; |
4 | | -import {DataValidationError} from '../errors.js'; |
| 4 | +import {PhotoDataNotFoundError, PhotoValidationError} from '../errors.js'; |
5 | 5 |
|
6 | 6 | export const labelDetectionController = async ( |
7 | 7 | req: Request, |
8 | 8 | res: Response, |
9 | 9 | next: NextFunction, |
10 | 10 | ): Promise<void> => { |
11 | 11 | /* |
12 | | - #swagger.tags = ['label-detection'] |
13 | | - #swagger.summary = '이미지 라벨링' |
14 | | - #swagger.description = 'Base64 데이터를 JSON으로 받아 이미지를 분석하여 상위 3개의 라벨과 정확도를 반환합니다.' |
15 | | - #swagger.requestBody = { |
16 | | - required: true, |
17 | | - content: { |
18 | | - "application/json": { |
19 | | - schema: { |
20 | | - type: "object", |
21 | | - properties: { |
22 | | - base64_image: { |
23 | | - type: "string", |
24 | | - description: "Base64 인코딩된 이미지 데이터", |
25 | | - example: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..." |
26 | | - } |
27 | | - } |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - #swagger.responses[200] = { |
33 | | - description: "라벨링 결과 반환", |
34 | | - content: { |
35 | | - "application/json": { |
36 | | - schema: { |
37 | | - type: "object", |
38 | | - properties: { |
39 | | - topLabels: { |
40 | | - type: "array", |
41 | | - items: { |
42 | | - type: "object", |
43 | | - properties: { |
44 | | - description: { type: "string", example: "Mountain" }, |
45 | | - score: { type: "number", example: 0.95 } |
46 | | - } |
47 | | - } |
48 | | - } |
49 | | - } |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - } |
54 | | - #swagger.responses[400] = { |
55 | | - description: "잘못된 요청 데이터", |
56 | | - content: { |
57 | | - "application/json": { |
58 | | - schema: { |
59 | | - type: "object", |
60 | | - properties: { |
61 | | - error: { type: "string", example: "Base64 이미지 데이터가 제공되지 않았습니다." } |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | - #swagger.responses[500] = { |
68 | | - description: "서버 내부 오류", |
69 | | - content: { |
70 | | - "application/json": { |
71 | | - schema: { |
72 | | - type: "object", |
73 | | - properties: { |
74 | | - error: { type: "string", example: "라벨링 중 오류가 발생했습니다." } |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - */ |
| 12 | + #swagger.tags = ['label-detection'] |
| 13 | + #swagger.summary = '이미지 라벨링' |
| 14 | + #swagger.description = 'Base64 데이터를 JSON으로 받아 이미지를 분석하여 상위 3개의 라벨과 정확도를 반환합니다.' |
| 15 | + #swagger.requestBody = { |
| 16 | + required: true, |
| 17 | + content: { |
| 18 | + "multipart/form-data": { |
| 19 | + schema: { |
| 20 | + type: "object", |
| 21 | + properties: { |
| 22 | + base64_image: { |
| 23 | + type: "string", |
| 24 | + format: "binary", |
| 25 | + description: "업로드할 이미지 파일" |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + #swagger.responses[200] = { |
| 33 | + description: "라벨링 결과 반환", |
| 34 | + content: { |
| 35 | + "application/json": { |
| 36 | + schema: { |
| 37 | + type: "object", |
| 38 | + properties: { |
| 39 | + topLabels: { |
| 40 | + type: "array", |
| 41 | + items: { |
| 42 | + type: "object", |
| 43 | + properties: { |
| 44 | + description: { type: "string", example: "Mountain" }, |
| 45 | + score: { type: "number", example: 0.95 } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + #swagger.responses[400] = { |
| 55 | + description: "잘못된 요청 데이터", |
| 56 | + content: { |
| 57 | + "application/json": { |
| 58 | + schema: { |
| 59 | + type: "object", |
| 60 | + properties: { |
| 61 | + error: { type: "string", example: "Base64 이미지 데이터가 제공되지 않았습니다." } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + #swagger.responses[500] = { |
| 68 | + description: "서버 내부 오류", |
| 69 | + content: { |
| 70 | + "application/json": { |
| 71 | + schema: { |
| 72 | + type: "object", |
| 73 | + properties: { |
| 74 | + error: { type: "string", example: "라벨링 중 오류가 발생했습니다." } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + */ |
81 | 81 |
|
82 | 82 | try { |
83 | 83 | // Base64 이미지 데이터가 요청에 포함되었는지 확인 |
84 | | - const {base64_image} = req.body; |
85 | | - |
86 | | - if (!base64_image) { |
87 | | - throw new DataValidationError({ |
88 | | - reason: 'Base64 이미지 데이터가 제공되지 않았습니다.', |
| 84 | + if (!req.file) { |
| 85 | + throw new PhotoDataNotFoundError({ |
| 86 | + reason: '라벨링을 추출 할 사진이 없습니다', |
89 | 87 | }); |
90 | 88 | } |
91 | 89 |
|
92 | | - // Base64 데이터에서 MIME 타입 제거 |
93 | | - const base64Data = base64_image.replace(/^data:image\/\w+;base64,/, ''); |
| 90 | + const base64_image = req.file.buffer.toString('base64'); |
94 | 91 |
|
95 | | - // 서비스 호출 |
96 | | - const labels = await detectLabels(base64Data); |
| 92 | + if (!isValidBase64(base64_image)) { |
| 93 | + throw new PhotoValidationError({ |
| 94 | + reason: '올바른 Base64 이미지 형식이 아닙니다.', |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + //서비스 호출 |
| 99 | + const labels = await detectLabels(base64_image); |
97 | 100 |
|
98 | 101 | // 라벨 반환 |
99 | 102 | res.status(StatusCodes.OK).json({topLabels: labels}); |
100 | 103 | } catch (error) { |
101 | 104 | next(error); |
102 | 105 | } |
103 | 106 | }; |
| 107 | + |
| 108 | +const isValidBase64 = (base64String: string): boolean => { |
| 109 | + // base64 문자열 검증 함수 |
| 110 | + const base64Regex = |
| 111 | + /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; |
| 112 | + return base64Regex.test(base64String); |
| 113 | +}; |
0 commit comments