-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatImageController.java
More file actions
65 lines (60 loc) · 3.5 KB
/
Copy pathChatImageController.java
File metadata and controls
65 lines (60 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.leets.tdd.chat.controller;
import com.leets.tdd.chat.dto.ChatImageConfirmRequest;
import com.leets.tdd.chat.dto.ChatImagePresignRequest;
import com.leets.tdd.chat.dto.ChatImagePresignResponse;
import com.leets.tdd.chat.service.ChatImageService;
import com.leets.tdd.global.common.ApiResponse;
import com.leets.tdd.global.jwt.UserPrincipal;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/parties/{partyId}/chat/images")
@RequiredArgsConstructor
public class ChatImageController {
private final ChatImageService chatImageService;
@Operation(
summary = "채팅 이미지 업로드 1단계(업로드 URL 발급)",
description = "채팅 이미지를 올릴 Presigned PUT URL과 key를 발급한다(비공개 버킷). 해당 팟의 참여자만 "
+ "발급받을 수 있다. 응답으로 받은 uploadUrl로 브라우저가 S3에 직접 PUT한 뒤, 그 key로 "
+ "확정(confirm) API를 호출해야 한다. 허용 형식은 JPEG/PNG/WEBP다. "
+ "Authorization 헤더에 access token(Bearer)이 필요하다."
)
@SecurityRequirement(name = "bearerAuth")
@PostMapping("/presign")
public ResponseEntity<ApiResponse<ChatImagePresignResponse>> presignUpload(
@PathVariable Long partyId,
@AuthenticationPrincipal UserPrincipal userPrincipal,
@Valid @RequestBody ChatImagePresignRequest request
) {
ChatImagePresignResponse response =
chatImageService.presignUpload(partyId, userPrincipal.userId(), request);
return ResponseEntity.ok(ApiResponse.success("업로드 URL이 발급되었습니다.", response));
}
@Operation(
summary = "채팅 이미지 업로드 2단계(업로드 확정)",
description = "브라우저가 S3에 직접 업로드를 마친 뒤 호출한다. 해당 팟의 참여자만 호출할 수 있다. "
+ "서버가 실제로 올라간 객체의 용량/형식을 확인하고, 기준을 벗어나면 객체를 지우고 실패 "
+ "처리한다. 확정에 성공하면 클라이언트는 받은 key로 { messageType: \"IMAGE\", imageUrl: key } "
+ "형태의 메시지를 WebSocket(/app/parties/{partyId}/chat)으로 발행해 이미지 메시지를 전송한다. "
+ "Authorization 헤더에 access token(Bearer)이 필요하다."
)
@SecurityRequirement(name = "bearerAuth")
@PostMapping("/confirm")
public ResponseEntity<ApiResponse<Void>> confirmUpload(
@PathVariable Long partyId,
@AuthenticationPrincipal UserPrincipal userPrincipal,
@Valid @RequestBody ChatImageConfirmRequest request
) {
chatImageService.confirmUpload(partyId, userPrincipal.userId(), request);
return ResponseEntity.ok(ApiResponse.success("이미지 업로드가 확정되었습니다.", null));
}
}