-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#63 카카오 파일 링크 추출 api 구현
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 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
19 changes: 19 additions & 0 deletions
19
linknamu/src/main/java/com/kakao/linknamu/kakao/KakaoExceptionStatus.java
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,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; | ||
} |
35 changes: 35 additions & 0 deletions
35
linknamu/src/main/java/com/kakao/linknamu/kakao/controller/KakaoSendMeController.java
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,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)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
linknamu/src/main/java/com/kakao/linknamu/kakao/dto/KakaoSendMeResponseDto.java
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,9 @@ | ||
package com.kakao.linknamu.kakao.dto; | ||
|
||
public record KakaoSendMeResponseDto( | ||
String link | ||
|
||
) { | ||
|
||
|
||
} |
60 changes: 60 additions & 0 deletions
60
linknamu/src/main/java/com/kakao/linknamu/kakao/service/KaKaoSendMeService.java
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,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); | ||
} | ||
|
||
|
||
} | ||
} |