Skip to content

Commit ea07f8f

Browse files
authored
Merge branch 'dev' into Refactor/214
2 parents 6937491 + a9832c8 commit ea07f8f

File tree

19 files changed

+672
-30
lines changed

19 files changed

+672
-30
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ dependencies {
7070
testImplementation("net.ttddyy:datasource-proxy:1.8.1")
7171
testImplementation("org.testcontainers:junit-jupiter:1.19.3")
7272
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
73+
testImplementation ("io.findify:s3mock_2.13:0.2.6")
7374

7475
// Redis
7576
implementation("org.springframework.boot:spring-boot-starter-data-redis")
7677
implementation("com.github.codemonstur:embedded-redis:1.4.3")
7778

7879
// AWS S3
7980
implementation ("org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE")
81+
8082
}
8183

8284
tasks.withType<Test> {
Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,83 @@
11
package com.back.domain.file.controller;
22

3+
import com.back.domain.file.dto.*;
4+
import com.back.domain.file.entity.EntityType;
35
import com.back.domain.file.service.FileService;
46
import com.back.global.common.dto.RsData;
7+
import com.back.global.security.user.CustomUserDetails;
58
import lombok.RequiredArgsConstructor;
69
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.MediaType;
711
import org.springframework.http.ResponseEntity;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RestController;
11-
import org.springframework.web.multipart.MultipartFile;
12-
13-
import java.util.List;
12+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
13+
import org.springframework.web.bind.annotation.*;
1414

1515
@RestController
1616
@RequiredArgsConstructor
17-
@RequestMapping("/file")
17+
@RequestMapping("/api/file")
1818
public class FileController {
1919
private final FileService fileService;
2020

21-
@PostMapping
22-
public ResponseEntity<RsData<String>> uploadFile(MultipartFile multipartFile) {
21+
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
22+
public ResponseEntity<RsData<FileUploadResponseDto>> uploadFile(
23+
@ModelAttribute FileUploadRequestDto req,
24+
@AuthenticationPrincipal CustomUserDetails user
25+
) {
26+
FileUploadResponseDto res = fileService.uploadFile(
27+
req.getMultipartFile(),
28+
req.getEntityType(),
29+
req.getEntityId(),
30+
user.getUserId()
31+
);
32+
33+
return ResponseEntity
34+
.status(HttpStatus.OK)
35+
.body(RsData.success("파일 업로드 성공", res));
36+
}
37+
38+
@GetMapping(value = "/read")
39+
public ResponseEntity<RsData<FileReadResponseDto>> getFile(
40+
@RequestParam("entityType") EntityType entityType,
41+
@RequestParam("entityId") Long entityId
42+
) {
43+
FileReadResponseDto res = fileService.getFile(entityType, entityId);
44+
45+
return ResponseEntity
46+
.status(HttpStatus.OK)
47+
.body(RsData.success("파일 조회 성공", res));
48+
}
49+
50+
@PutMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
51+
public ResponseEntity<RsData<Void>> updateFile(
52+
@ModelAttribute FileUpdateRequestDto req,
53+
@AuthenticationPrincipal CustomUserDetails user
54+
) {
55+
fileService.updateFile(
56+
req.getMultipartFile(),
57+
req.getEntityType(),
58+
req.getEntityId(),
59+
user.getUserId()
60+
);
61+
62+
return ResponseEntity
63+
.status(HttpStatus.OK)
64+
.body(RsData.success("파일 업데이트 성공"));
65+
}
66+
67+
@DeleteMapping(value = "/delete")
68+
public ResponseEntity<RsData<Void>> deleteFile(
69+
@RequestParam("entityType") EntityType entityType,
70+
@RequestParam("entityId") Long entityId,
71+
@AuthenticationPrincipal CustomUserDetails user
72+
) {
73+
fileService.deleteFile(
74+
entityType,
75+
entityId,
76+
user.getUserId()
77+
);
78+
2379
return ResponseEntity
2480
.status(HttpStatus.OK)
25-
.body(RsData.success("파일 업로드 성공", fileService.uploadFile(multipartFile)));
81+
.body(RsData.success("파일 삭제 성공"));
2682
}
2783
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.back.domain.file.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class FileReadResponseDto {
7+
private String imageUrl;
8+
9+
public FileReadResponseDto(String imageUrl) {
10+
this.imageUrl = imageUrl;
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.back.domain.file.dto;
2+
3+
import com.back.domain.file.entity.EntityType;
4+
import lombok.Data;
5+
import org.springframework.web.multipart.MultipartFile;
6+
7+
@Data
8+
public class FileUpdateRequestDto {
9+
private MultipartFile multipartFile;
10+
private EntityType entityType;
11+
private Long entityId;
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.back.domain.file.dto;
2+
3+
import com.back.domain.file.entity.EntityType;
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.Data;
6+
import org.springframework.web.multipart.MultipartFile;
7+
8+
@Data
9+
public class FileUploadRequestDto {
10+
private MultipartFile multipartFile;
11+
private EntityType entityType;
12+
private Long entityId;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.back.domain.file.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class FileUploadResponseDto {
7+
private String imageUrl;
8+
9+
public FileUploadResponseDto(String imageUrl) {
10+
this.imageUrl = imageUrl;
11+
}
12+
}

src/main/java/com/back/domain/file/entity/AttachmentMapping.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ public class AttachmentMapping extends BaseEntity {
1717
private EntityType entityType;
1818

1919
private Long entityId;
20+
21+
public AttachmentMapping(FileAttachment fileAttachment, EntityType entityType, Long entityId) {
22+
this.fileAttachment = fileAttachment;
23+
this.entityType = entityType;
24+
this.entityId = entityId;
25+
}
2026
}

src/main/java/com/back/domain/file/entity/FileAttachment.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import jakarta.persistence.*;
66
import lombok.Getter;
77
import lombok.NoArgsConstructor;
8+
import org.springframework.web.multipart.MultipartFile;
89

910
import java.util.ArrayList;
1011
import java.util.List;
@@ -19,10 +20,9 @@ public class FileAttachment extends BaseEntity {
1920

2021
private String filePath;
2122

22-
private int fileSize;
23+
private long fileSize;
2324

24-
@Enumerated(EnumType.STRING)
25-
private MimeType mimeType;
25+
private String contentType;
2626

2727
// 업로드 유저
2828
@ManyToOne(fetch = FetchType.LAZY)
@@ -31,4 +31,30 @@ public class FileAttachment extends BaseEntity {
3131

3232
@OneToMany(mappedBy = "fileAttachment", cascade = CascadeType.ALL, orphanRemoval = true)
3333
private List<AttachmentMapping> attachmentMappings = new ArrayList<>();
34+
35+
public FileAttachment(
36+
String storedName,
37+
MultipartFile multipartFile,
38+
User user,
39+
EntityType entityType,
40+
Long entityId,
41+
String filePath
42+
) {
43+
this.storedName = storedName;
44+
originalName = multipartFile.getOriginalFilename();
45+
this.filePath = filePath;
46+
fileSize = multipartFile.getSize();
47+
this.contentType = multipartFile.getContentType();
48+
this.user = user;
49+
50+
attachmentMappings.add(new AttachmentMapping(this ,entityType, entityId));
51+
}
52+
53+
public void update(String storedName, MultipartFile multipartFile, String filePath) {
54+
this.storedName = storedName;
55+
originalName = multipartFile.getOriginalFilename();
56+
this.filePath = filePath;
57+
fileSize = multipartFile.getSize();
58+
this.contentType = multipartFile.getContentType();
59+
}
3460
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.back.domain.file.repository;
2+
3+
import com.back.domain.file.entity.AttachmentMapping;
4+
import com.back.domain.file.entity.EntityType;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
import java.util.Optional;
8+
9+
public interface AttachmentMappingRepository extends JpaRepository<AttachmentMapping, Long> {
10+
Optional<AttachmentMapping> findByEntityTypeAndEntityId(EntityType entityType, Long entityID);
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.back.domain.file.repository;
2+
3+
import com.back.domain.file.entity.FileAttachment;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface FileAttachmentRepository extends JpaRepository<FileAttachment, Long> {
7+
}

0 commit comments

Comments
 (0)