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); }