diff --git a/src/main/java/project/backend/domain/ticket/controller/TicketController.java b/src/main/java/project/backend/domain/ticket/controller/TicketController.java index c982f40..1dc2f67 100644 --- a/src/main/java/project/backend/domain/ticket/controller/TicketController.java +++ b/src/main/java/project/backend/domain/ticket/controller/TicketController.java @@ -4,9 +4,11 @@ import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import project.backend.domain.culturalevent.entity.CulturalEvent; import project.backend.domain.member.entity.Member; import project.backend.domain.ticket.dto.TicketCreateDto; @@ -42,4 +44,14 @@ public ResponseEntity retrieve(@Positive @PathVariable Long id) { Ticket ticket = ticketService.getTicket(id); return ResponseEntity.status(HttpStatus.OK).body(ticketMapper.ticketToTicketRetrieveDto(ticket)); } + + @ApiOperation(value = "티켓 정보 추출(ocr)") + @PreAuthorize("isAuthenticated()") + @PostMapping(value = "/extract-info", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity extractInfo( + @RequestPart(value = "file") MultipartFile file + ) { + ResponseEntity response = ticketService.extractInfoFromImage(file); + return ResponseEntity.status(response.getStatusCode()).body(response.getBody()); + } } \ No newline at end of file diff --git a/src/main/java/project/backend/domain/ticket/service/TicketService.java b/src/main/java/project/backend/domain/ticket/service/TicketService.java index 5fd95d0..2ed4251 100644 --- a/src/main/java/project/backend/domain/ticket/service/TicketService.java +++ b/src/main/java/project/backend/domain/ticket/service/TicketService.java @@ -1,8 +1,15 @@ package project.backend.domain.ticket.service; import lombok.RequiredArgsConstructor; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; +import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.util.UriComponentsBuilder; import project.backend.domain.media.dto.MediaDto; import project.backend.domain.media.entity.Media; import project.backend.domain.media.service.MediaService; @@ -14,6 +21,8 @@ import project.backend.global.error.exception.BusinessException; import project.backend.global.error.exception.ErrorCode; +import java.io.IOException; + @Service @RequiredArgsConstructor @Transactional @@ -57,4 +66,49 @@ public Ticket getTicket(Long id) { public Ticket verifiedTicket(Long id) { return ticketRepository.findById(id).orElseThrow(() -> new BusinessException(ErrorCode.TICKET_NOT_FOUND)); } + + public ResponseEntity extractInfoFromImage(MultipartFile file) { + try { + // Prepare headers + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + // Create MultiValueMap to hold the file + LinkedMultiValueMap body = new LinkedMultiValueMap<>(); + body.add("image", getByteArrayResource(file)); + + // Create an HttpEntity with the file and headers + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + // Build the URI for the OCR API + UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("http://13.125.32.85:8000/api/ocr"); + + // Create RestTemplate instance + RestTemplate restTemplate = new RestTemplate(); + + // Send request to OCR API and get the response + ResponseEntity response = restTemplate.exchange( + uriBuilder.toUriString(), + HttpMethod.POST, + requestEntity, + String.class + ); + + // Return the response from OCR API to the client + return response; + } catch (IOException e) { + throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND); + } catch (Exception e) { + throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND); + } + } + + private Resource getByteArrayResource(MultipartFile file) throws IOException { + return new ByteArrayResource(file.getBytes()) { + @Override + public String getFilename() { + return file.getOriginalFilename(); + } + }; + } }