From b4a29faf957568f29336c3c1823fd3340e2802da Mon Sep 17 00:00:00 2001 From: minuk926 Date: Thu, 21 Apr 2022 21:55:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9E=90=EB=A3=8C=EA=B4=80=EB=A6=AC=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=8B=A4=EC=9A=B4=EB=A1=9C=EB=93=9C=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/cmm/controller/CmmFileController.java | 52 +++++++++++++++++-- .../impl/ResidentAndDisabledService.java | 4 +- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/xit/biz/cmm/controller/CmmFileController.java b/src/main/java/com/xit/biz/cmm/controller/CmmFileController.java index 3160bff..c62f00a 100644 --- a/src/main/java/com/xit/biz/cmm/controller/CmmFileController.java +++ b/src/main/java/com/xit/biz/cmm/controller/CmmFileController.java @@ -1,9 +1,14 @@ package com.xit.biz.cmm.controller; +import com.xit.biz.ctgy.CtgyConstants; +import com.xit.biz.ctgy.dto.GnRecallScDto; import com.xit.biz.ctgy.entity.MinInfoBoard680; import com.xit.biz.ctgy.service.ICtgyFileService; +import com.xit.biz.ctgy.service.IResidentAndDisabledService; import com.xit.core.constant.ErrorCode; import com.xit.core.exception.CustomBaseException; +import com.xit.core.util.Checks; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.apache.commons.io.FileUtils; @@ -12,9 +17,12 @@ import org.springframework.core.env.Environment; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; +import javax.annotation.Nonnull; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -36,15 +44,20 @@ public class CmmFileController { @Value("${file.cmm.upload.path:/kangnamSIM/simUpFile/}") private String uploadPath; + @Value("${file.cmm.upload.simsaPath:[simUpFile_sc1]}") + private String[] judgeUploadPath; + @Value("${file.cmm.upload.url}") private String serviceUrl; - private final ICtgyFileService service; + private final ICtgyFileService ctgyFileService; + private final IResidentAndDisabledService judgeService; + @Operation(summary = "공지사항 파일 다운로드" , description = "공지사항 파일 다운로드") @GetMapping("/download/{inCode}") public void download(@PathVariable Long inCode, HttpServletResponse response) { - MinInfoBoard680 entity = service.findFiles(inCode); + MinInfoBoard680 entity = ctgyFileService.findFiles(inCode); String absFile = ""; @@ -53,10 +66,41 @@ public class CmmFileController { else absFile = rootPath + entity.getInFileurl().split(serviceUrl)[1] + File.separator + entity.getInFilename(); + download(absFile, entity.getInFilename(), response); + } + + @Operation(summary = "거주자/장애인 심사자료 파일 다운로드" , description = "거주자/장애인 심사자료 파일 다운로드") + @GetMapping("/download/judge") + public void download(@Nonnull final String scDatagb, @Nonnull final Long scCode, @Nonnull String methodName, HttpServletResponse response) { + + GnRecallScDto savedDto = judgeService.findJudgeData(scCode); + String fileName = ""; + try { + Method method = GnRecallScDto.class.getMethod(methodName); + fileName = String.valueOf(method.invoke(savedDto)); + if(Checks.isEmpty(fileName)) throw new CustomBaseException(ErrorCode.DATA_NOT_FOUND); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + + String absFile = ""; + + if (Arrays.asList(env.getActiveProfiles()).contains("prod")) + absFile = "entity.getInFileurl()" + File.separator + fileName; + else + absFile = rootPath + (CtgyConstants.Judge.DATAGB_RESIDENT.getCode().equals(scDatagb)? judgeUploadPath[0] : judgeUploadPath[1]) + fileName; + + download(absFile, fileName, response); + } + + private void download(String absFile, String fileName, HttpServletResponse response) { + Path path = Paths.get(absFile); String contentType = null; + long fileSize = 0L; try { contentType = Files.probeContentType(path); + fileSize = Files.size(path); } catch (IOException e) { throw new CustomBaseException(ErrorCode.FILE_NOT_FOUND); } @@ -72,9 +116,9 @@ public class CmmFileController { response.setContentType(contentType); response.setHeader(HttpHeaders.CONTENT_TYPE, contentType); response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); - response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(entity.getInFilename(), StandardCharsets.UTF_8) + "\";"); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(fileName, StandardCharsets.UTF_8) + "\";"); //response.setHeader(HttpHeaders.CONTENT_ENCODING, "binary"); - response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getInFilesize())); + response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileSize)); try { response.getOutputStream().write(fileByte); response.getOutputStream().flush(); diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/ResidentAndDisabledService.java b/src/main/java/com/xit/biz/ctgy/service/impl/ResidentAndDisabledService.java index 5f6cce4..00b2dd2 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/ResidentAndDisabledService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/ResidentAndDisabledService.java @@ -41,8 +41,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import static java.util.stream.Collectors.toMap; - @Service @RequiredArgsConstructor @Slf4j @@ -106,7 +104,7 @@ public class ResidentAndDisabledService implements IResidentAndDisabledService { } private void setFileInfoAndFileUpload(GnRecallScDto dto, MultipartFile[] mfs, String setMethodName) { - String makePath = fileService.uploadFiles(mfs, rootPath, uploadPath[0]); + String makePath = fileService.uploadFiles(mfs, rootPath, CtgyConstants.Judge.DATAGB_RESIDENT.getCode().equals(dto.getScDatagb())? uploadPath[0] : uploadPath[1]); makePath = makePath + File.separator; for(int idx = 0; idx < mfs.length; idx++){