Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ out/

src/main/resources/firebase/
#/src/main/java/com/campick/server/common/config/DataInitializer.java

*/.DS_Store
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.campick.server.api.chat.controller;

import com.campick.server.api.chat.dto.ChatImageReqDto;
import com.campick.server.api.chat.dto.ChatImageResDto;
import com.campick.server.api.product.service.ChatImageService;
import com.campick.server.common.response.ApiResponse;
import com.campick.server.common.response.SuccessStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/chat/image")
@RequiredArgsConstructor
public class ChatImageController {
private final ChatImageService chatImageService;

@PostMapping(consumes = {"multipart/form-data"})
public ResponseEntity<ApiResponse<ChatImageResDto>> upload(@RequestPart("chatId") Long chatId,
@RequestPart("file") MultipartFile file) {
String imageUrl = chatImageService.uploadImage(chatId, file);
ChatImageResDto chatImageResDto = ChatImageResDto.builder()
.chatImageUrl(imageUrl).build();
return ApiResponse.success(SuccessStatus.UPLOAD_CHAT_IMAGE_SUCCESS, chatImageResDto);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/campick/server/api/chat/dto/ChatImageReqDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.campick.server.api.chat.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ChatImageReqDto {
private Long chatId;
}
13 changes: 13 additions & 0 deletions src/main/java/com/campick/server/api/chat/dto/ChatImageResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.campick.server.api.chat.dto;

import com.campick.server.api.product.dto.ProductImageResDto;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ChatImageResDto {
private String chatImageUrl;
}
23 changes: 23 additions & 0 deletions src/main/java/com/campick/server/api/chat/entity/ChatImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.campick.server.api.chat.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "chat_image")
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class ChatImage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chat_id", nullable = false)
private ChatRoom chatRoom;

@Column(name = "image_url", nullable = false)
private String imageUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.campick.server.api.chat.repository;

import com.campick.server.api.chat.entity.ChatImage;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChatImageRepository extends JpaRepository<ChatImage, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.campick.server.api.product.service;

import com.campick.server.api.chat.repository.ChatImageRepository;
import com.campick.server.common.storage.FirebaseStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class ChatImageService {
private final ChatImageRepository chatImageRepository;
private final FirebaseStorageService firebaseStorageService;

@Transactional
public String uploadImage(Long chatId, MultipartFile file) {
return firebaseStorageService.uploadChatImage(chatId, file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public enum SuccessStatus {
SEND_MY_CHATROOMS(HttpStatus.OK, "내 채팅방 조회 성공"),
SEND_TOTAL_UNREAD_MSG(HttpStatus.OK, "총 안 읽은 메시지 수 조회 성공"),
COMPLETE_CHAT(HttpStatus.OK, "채팅방 종료 완료"),
UPLOAD_CHAT_IMAGE_SUCCESS(HttpStatus.OK, "채팅방 내 이미지 업로드 성공"),


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public Map<String, String> uploadProductImage(MultipartFile file) {
}
}

public String uploadChatImage(Long chatId, MultipartFile file) {
if (file == null || file.isEmpty())
throw new BadRequestException(ErrorStatus.EMPTY_FILE_EXCEPTION.getMessage());

try {
String ext = extractExtension(file.getOriginalFilename());
String objectName = String.format("chat/%d%s", chatId, ext);

StorageClient.getInstance().bucket().create(objectName, file.getBytes(), file.getContentType());

return String.format("%s/o/%s?alt=media",storageBaseUrl, urlEncode(objectName));
} catch (IOException e) {
throw new ImageUploadFailedException(ErrorStatus.IMAGE_UPLOAD_FAILED_EXCEPTION);
}
}

private void uploadThumbnail(MultipartFile file, String objectName, int width, int height) throws IOException {
ByteArrayOutputStream thumbnailOStream = new ByteArrayOutputStream();

Expand Down
Binary file added src/main/resources/.DS_Store
Binary file not shown.