From dd8965b0de2f1e783d02177d6b58946489a5e96e Mon Sep 17 00:00:00 2001 From: zionhann Date: Tue, 24 Oct 2023 01:07:45 +0900 Subject: [PATCH] =?UTF-8?q?Hotfix:=20=EB=B3=B4=EA=B3=A0=EC=84=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=A1=B0=ED=9A=8C=EA=B0=80=20?= =?UTF-8?q?=EC=95=88=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 보고서 이미지에 대한 모든 응답에 리소스 핸들러 경로를 포함 * 이미지를 DB에 저장할 때 파일명만 기록하도록 수정 * 서버 내 저장된 이미지를 찾을 수 없는 버그 수정 --- .../csee/histudy/config/WebConfig.java | 6 ++--- .../histudy/controller/AdminController.java | 10 ++++++-- .../histudy/controller/TeamController.java | 23 +++++++++++++++---- .../csee/histudy/domain/GroupReport.java | 11 +++++++-- .../handong/csee/histudy/dto/ImageDto.java | 6 ++++- .../handong/csee/histudy/dto/ReportDto.java | 4 ++++ .../csee/histudy/service/ImageService.java | 4 ++-- 7 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/handong/csee/histudy/config/WebConfig.java b/src/main/java/edu/handong/csee/histudy/config/WebConfig.java index 068e9a5..0f6e603 100644 --- a/src/main/java/edu/handong/csee/histudy/config/WebConfig.java +++ b/src/main/java/edu/handong/csee/histudy/config/WebConfig.java @@ -25,8 +25,8 @@ public class WebConfig implements WebMvcConfigurer { @Value("${custom.path-patterns.include}") private String[] includePathPatterns; - @Value("${custom.resource.path}") - private String imageBasePath; + @Value("${custom.resource.path-pattern}") + private String imageBasePathPattern; @Value("${custom.resource.location}") private String imageBaseLocation; @@ -48,7 +48,7 @@ public void addCorsMappings(CorsRegistry registry) { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler(imageBasePath) + registry.addResourceHandler(imageBasePathPattern) .addResourceLocations("file://" + imageBaseLocation); } } diff --git a/src/main/java/edu/handong/csee/histudy/controller/AdminController.java b/src/main/java/edu/handong/csee/histudy/controller/AdminController.java index 05b941d..df18bfd 100644 --- a/src/main/java/edu/handong/csee/histudy/controller/AdminController.java +++ b/src/main/java/edu/handong/csee/histudy/controller/AdminController.java @@ -14,6 +14,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -55,9 +56,14 @@ public ResponseEntity deleteTeam( public ResponseEntity getTeamReports( @Parameter(description = "그룹 아이디", required = true) @PathVariable(name = "id") long id, - @RequestAttribute Claims claims) { + @RequestAttribute Claims claims, + @Value("${custom.resource.path}") String imageBasePath) { if (Role.isAuthorized(claims, Role.ADMIN)) { - return ResponseEntity.ok(teamService.getTeamReports(id, claims.getSubject())); + TeamReportDto res = teamService.getTeamReports(id, claims.getSubject()); + res.getReports() + .forEach(report -> + report.addPathToFilename(imageBasePath)); + return ResponseEntity.ok(res); } throw new ForbiddenException(); } diff --git a/src/main/java/edu/handong/csee/histudy/controller/TeamController.java b/src/main/java/edu/handong/csee/histudy/controller/TeamController.java index 31b407e..0b5a04b 100644 --- a/src/main/java/edu/handong/csee/histudy/controller/TeamController.java +++ b/src/main/java/edu/handong/csee/histudy/controller/TeamController.java @@ -25,6 +25,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.apache.commons.collections.map.SingletonMap; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -45,13 +46,20 @@ public class TeamController { private final ImageService imageService; private final UserRepository userRepository; + @Value("${custom.resource.path}") + String imageBasePath; + @Operation(summary = "그룹 스터디 보고서 생성") @PostMapping("/reports") public ReportDto.ReportInfo createReport( @RequestBody ReportForm form, @RequestAttribute Claims claims) { if (Role.isAuthorized(claims, Role.MEMBER)) { - return reportService.createReport(form, claims.getSubject()); + ReportDto.ReportInfo res = reportService.createReport(form, claims.getSubject()); + res.getImages().forEach(image -> + image.addPathToFilename(imageBasePath)); + + return res; } throw new ForbiddenException(); } @@ -62,6 +70,9 @@ public ReportDto getMyGroupReports( @RequestAttribute Claims claims) { if (Role.isAuthorized(claims, Role.MEMBER)) { List reports = reportService.getReports(claims.getSubject()); + reports.forEach(report -> + report.getImages().forEach( + image -> image.addPathToFilename(imageBasePath))); return new ReportDto(reports); } throw new ForbiddenException(); @@ -78,6 +89,10 @@ public ResponseEntity getReport( @RequestAttribute Claims claims) { if (Role.isAuthorized(claims, Role.MEMBER, Role.ADMIN)) { Optional reportsOr = reportService.getReport(reportId); + reportsOr.ifPresent(report -> + report.getImages().forEach(image -> + image.addPathToFilename(imageBasePath))); + return reportsOr .map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); @@ -153,7 +168,7 @@ public ResponseEntity> getTeamUsers( content = @Content( mediaType = "application/json", examples = @ExampleObject( - value = "{\"imagePath\": \"path/to/image\"}" + value = "{\"imagePath\": \"/path/to/image.png\"}" ) ) ) @@ -166,8 +181,8 @@ public ResponseEntity uploadImage( .orElseThrow(UserNotFoundException::new) .getStudyGroup(); - String pathname = imageService.getImagePaths(image, studyGroup.getTag(), reportIdOr); - SingletonMap response = new SingletonMap("imagePath", pathname); + String filename = imageService.getImagePaths(image, studyGroup.getTag(), reportIdOr); + SingletonMap response = new SingletonMap("imagePath", imageBasePath + filename); return ResponseEntity.ok(response); } throw new ForbiddenException(); diff --git a/src/main/java/edu/handong/csee/histudy/domain/GroupReport.java b/src/main/java/edu/handong/csee/histudy/domain/GroupReport.java index 7922c08..2d36c14 100644 --- a/src/main/java/edu/handong/csee/histudy/domain/GroupReport.java +++ b/src/main/java/edu/handong/csee/histudy/domain/GroupReport.java @@ -93,11 +93,18 @@ private void insert(List images) { this.images.clear(); } List paths = images.stream() - .map(img -> new Image(img, this)) - .toList(); + .map(img -> { + String filename = extractFilenameFromPath(img); + return new Image(filename, this); + }).toList(); this.images.addAll(paths); } + private String extractFilenameFromPath(String fullPath) { + int lastIndex = fullPath.lastIndexOf('/'); + return (lastIndex >= 0) ? fullPath.substring(lastIndex + 1) : fullPath; + } + public boolean update(ReportForm form, List participants, List courses) { this.title = requireNonNullElse(form.getTitle(), this.title); this.content = requireNonNullElse(form.getContent(), this.content); diff --git a/src/main/java/edu/handong/csee/histudy/dto/ImageDto.java b/src/main/java/edu/handong/csee/histudy/dto/ImageDto.java index 45cc196..60d0154 100644 --- a/src/main/java/edu/handong/csee/histudy/dto/ImageDto.java +++ b/src/main/java/edu/handong/csee/histudy/dto/ImageDto.java @@ -20,6 +20,10 @@ public ImageDto(Image entity) { @Schema(description = "Image ID", example = "1", type = "number") private long id; - @Schema(description = "Image URL", example = "https://histudy-bucket.s3.ap-northeast-2.amazonaws.com/1.png") + @Schema(description = "Image URL", example = "/path/to/image.png") private String url; + + public void addPathToFilename(String imageBasePath) { + this.url = imageBasePath + this.url; + } } diff --git a/src/main/java/edu/handong/csee/histudy/dto/ReportDto.java b/src/main/java/edu/handong/csee/histudy/dto/ReportDto.java index a62fa2a..dc8b8f4 100644 --- a/src/main/java/edu/handong/csee/histudy/dto/ReportDto.java +++ b/src/main/java/edu/handong/csee/histudy/dto/ReportDto.java @@ -100,5 +100,9 @@ public ReportBasic(GroupReport groupReport) { @Schema(description = "Thumbnail of the report", example = "https://histudy.s3.ap-northeast-2.amazonaws.com/2021-06-01-00-00-00-1") private String thumbnail; + + public void addPathToFilename(String imageBasePath) { + this.thumbnail = imageBasePath + this.thumbnail; + } } } diff --git a/src/main/java/edu/handong/csee/histudy/service/ImageService.java b/src/main/java/edu/handong/csee/histudy/service/ImageService.java index 8a90eb2..b704f3d 100644 --- a/src/main/java/edu/handong/csee/histudy/service/ImageService.java +++ b/src/main/java/edu/handong/csee/histudy/service/ImageService.java @@ -71,7 +71,7 @@ private String saveImage( MultipartFile image, String pathname) { try { - File file = new File(imageBaseLocation + File.separator + pathname); + File file = new File(imageBaseLocation + pathname); File dir = file.getParentFile(); if (!dir.exists()) { @@ -97,7 +97,7 @@ private Optional getSameContent(MultipartFile src, Long reportId) { try { return (isUrl(path)) ? contentMatches(src, new URL(path)) - : contentMatches(src, Path.of(path)); + : contentMatches(src, Path.of(imageBaseLocation + path)); } catch (MalformedURLException e) { throw new RuntimeException(e); }