Skip to content

Commit

Permalink
카카오 파일 링크 추출 api 구현 Merge
Browse files Browse the repository at this point in the history
#63 카카오 파일 링크 추출 api 구현
  • Loading branch information
Train0303 authored Oct 7, 2023
2 parents 0a45e7f + 762f517 commit 88d14bc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.util.List;

Expand All @@ -36,6 +37,11 @@ public ResponseEntity<?> dtoTypeMismatchException(Exception e) {
return new ResponseEntity<>(ApiUtils.error("입력의 타입이 올바르지 않습니다.", HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler({MissingServletRequestPartException.class})
public ResponseEntity<?> requestPartMissingException(Exception e) {
return new ResponseEntity<>(ApiUtils.error("form-data 형식에 file이라는 이름이 존재하지 않습니다.", HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler({Exception400.class, Exception401.class, Exception403.class, Exception404.class})
public ResponseEntity<?> clientException(ClientException e) {
return new ResponseEntity<>(e.body(), e.status());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kakao.linknamu.category;

import com.kakao.linknamu._core.exception.BaseExceptionStatus;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum KakaoExceptionStatus implements BaseExceptionStatus {

FILE_INVALID_FORMAT("파일이 txt 또는 csv 형식이 아닙니다.", 400),
FILE_NOTFOUND("파일이 존재하지 않습니다.", 400),
FILE_READ_FAILED("파일을 읽는 과정에서 오류가 발생했습니다.", 500);


@Getter
private final String message;
@Getter
private final int status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.kakao.linknamu.kakao.controller;

import com.kakao.linknamu._core.util.ApiUtils;
import com.kakao.linknamu.kakao.dto.KakaoSendMeResponseDto;
import com.kakao.linknamu.kakao.service.KaKaoSendMeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/kakao")
public class KakaoSendMeController {

private final KaKaoSendMeService kaKaoSendMeService;


//1. 단순히 file만 요청 받을 경우 => @RequestParam 어노테이션과 MultipartFile 객체사용
//2, file과 json 을 같이 받을 경우 => @RequestPart 어노테이션과 json을 받을 DTO에 key 값을 지정

@PostMapping("/send-me")
public ResponseEntity<?> getKaKaoSendMeText(@RequestPart(value = "file") MultipartFile multipartFile) {

List<KakaoSendMeResponseDto> responseDtos = kaKaoSendMeService.extractLink(multipartFile);


return ResponseEntity.ok(ApiUtils.success(responseDtos));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kakao.linknamu.kakao.dto;

public record KakaoSendMeResponseDto(
String link

) {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.kakao.linknamu.kakao.service;

import com.kakao.linknamu._core.exception.Exception400;
import com.kakao.linknamu._core.exception.Exception500;
import com.kakao.linknamu.category.KakaoExceptionStatus;
import com.kakao.linknamu.kakao.dto.KakaoSendMeResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
public class KaKaoSendMeService {


public List<KakaoSendMeResponseDto> extractLink(MultipartFile multipartFile) {

List<KakaoSendMeResponseDto> responseDtos = new ArrayList<>();

if (multipartFile.isEmpty()) throw new Exception400(KakaoExceptionStatus.FILE_NOTFOUND);

String contentType = multipartFile.getContentType();

boolean isSupportedFormat = (contentType.equals("text/plain")) || (contentType.equals("text/csv"));

if (isSupportedFormat == false) throw new Exception400(KakaoExceptionStatus.FILE_INVALID_FORMAT);


try {
byte[] fileBytes = multipartFile.getBytes();
String fileContent = new String(fileBytes, StandardCharsets.UTF_8); // byte 배열을 문자열로 변환

// https 링크를 추출하기 위한 정규 표현식
String regex = "https?://[a-zA-Z0-9\\-\\.]+(\\:[0-9]+)?\\.[a-zA-Z]{2,3}(\\S*)?";


// \bhttps?://\S+ => 모든 http, https 도메인 검출
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fileContent);
while (matcher.find()) {
String httpsLink = matcher.group();
responseDtos.add(new KakaoSendMeResponseDto(httpsLink));
}
return responseDtos;
} catch (IOException e) {
throw new Exception500(KakaoExceptionStatus.FILE_READ_FAILED);
}


}
}

0 comments on commit 88d14bc

Please sign in to comment.