From c332031b9bec28f11125f7f16a7f54a479de5c0f Mon Sep 17 00:00:00 2001 From: minuk926 Date: Mon, 28 Mar 2022 20:55:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20/=20=EC=82=AD=EC=A0=9C=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CtgyFileMgtController.java | 49 ++----- .../biz/ctgy/service/ICtgyFileService.java | 2 + .../ctgy/service/impl/CtgyFileService.java | 123 ++++++++++-------- 3 files changed, 81 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java index 5bf612f..8e75eb7 100644 --- a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java +++ b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java @@ -1,7 +1,5 @@ package com.xit.biz.ctgy.controller; -import com.xit.biz.cmm.dto.CmmFileDto; -import com.xit.biz.cmm.entity.CmmFileMst; import com.xit.biz.ctgy.dto.MinInfoBoard680Dto; import com.xit.biz.ctgy.entity.MinInfoBoard680; @@ -18,7 +16,6 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.lang.NonNull; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; import javax.annotation.Nonnull; import java.util.List; @@ -37,47 +34,27 @@ public class CtgyFileMgtController { return RestResponse.of(fileService.findFiles(inCode)); } - //@Operation(summary = "파일 저장" , description = "파일 저장") - //@PostMapping() -// public ResponseEntity saveFiles2(CmmFileMst cmmFileMst, @RequestParam("files") MultipartFile[] files) { -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileMst), "파일 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileMst.getFileCtgCd()), "파일 구분 코드[fileCtgCd] 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileMst.getFileBizId()), "파일 업무 ID[fileBizId] 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(files), "대상 파일이 존재하지 않습니다."); -// return RestResponse.of(fileService.saveFiles(cmmFileMst, files)); -// -// RedirectAttributes redirectAttributes -// redirectAttributes.addFlashAttribute("message", -// "You successfully uploaded " + file.getOriginalFilename() + "!"); -// -// return "redirect:/"; -// } - - @Operation(summary = "파일 저장" , description = "파일 저장") - @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ResponseEntity saveFiles(@Nonnull MinInfoBoard680Dto dto) { + @Operation(summary = "공지사항 저장" , description = "공지사항 저장") + @PostMapping(value="/pboard", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity savePublicBoardFiles(@Nonnull MinInfoBoard680Dto dto) { AssertUtils.isTrue(!Checks.isEmpty(dto), "파일 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFileCtgCd()), "파일 구분 코드[fileCtgCd] 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFileBizId()), "파일 업무 ID[fileBizId] 정보가 존재하지 않습니다."); -// AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFiles()), "대상 파일이 존재하지 않습니다."); -// CmmFileMst cmmFileMst = CmmFileMst.builder() -// .fileMstId(cmmFileDto.getFileMstId()) -// .fileCtgCd(cmmFileDto.getFileCtgCd()) -// .fileBizId(cmmFileDto.getFileBizId()) -// .build(); + MinInfoBoard680 minInfoBoard680 = MinInfoBoard680.builder() .inCode(dto.getInCode()) .inBgubun(dto.getInBgubun()) .inTitle(dto.getInTitle()) .inContents(dto.getInContents()) + .inFilename(dto.getInFilename()) .build(); - //fileService.saveFiles(minInfoBoard680, dto.getFiles()); return RestResponse.of(fileService.saveFiles(minInfoBoard680, dto.getFiles())); + } + + @Operation(summary = "공지사항 삭제" , description = "공지사항 삭제") + @PostMapping(value="/pboard/{inCode}") + public ResponseEntity removePublicBoardFile(@PathVariable @Nonnull final Long inCode) { + AssertUtils.isTrue(!Checks.isEmpty(inCode), "공지 사항이 선택되지 않았습니다."); + fileService.removePublicBoardFile(inCode); -// RedirectAttributes redirectAttributes -// redirectAttributes.addFlashAttribute("message", -// "You successfully uploaded " + file.getOriginalFilename() + "!"); -// -// return "redirect:/"; + return RestResponse.of(HttpStatus.OK); } } diff --git a/src/main/java/com/xit/biz/ctgy/service/ICtgyFileService.java b/src/main/java/com/xit/biz/ctgy/service/ICtgyFileService.java index c73917f..735a980 100644 --- a/src/main/java/com/xit/biz/ctgy/service/ICtgyFileService.java +++ b/src/main/java/com/xit/biz/ctgy/service/ICtgyFileService.java @@ -15,4 +15,6 @@ public interface ICtgyFileService { MinInfoBoard680 findFiles(Long inCode); List saveFiles(@Nonnull MinInfoBoard680 minInfoBoard680, MultipartFile[] files); + + void removePublicBoardFile(Long inCode); } diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java index c004b03..6daa30f 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java @@ -1,19 +1,13 @@ package com.xit.biz.ctgy.service.impl; -import com.xit.biz.cmm.entity.CmmFileDtl; -import com.xit.biz.cmm.entity.CmmFileMst; -import com.xit.biz.cmm.entity.ids.CmmFileDtlIds; -import com.xit.biz.cmm.repository.ICmmFileDtlRepository; -import com.xit.biz.cmm.repository.ICmmFileMstRepository; -import com.xit.biz.cmm.service.ICmmFileService; -import com.xit.biz.ctgy.CtgyConstants; import com.xit.biz.ctgy.entity.MinInfoBoard680; -import com.xit.biz.ctgy.entity.Tf680Recall; import com.xit.biz.ctgy.repository.IPublicBoardRepository; import com.xit.biz.ctgy.service.ICtgyFileService; +import com.xit.core.constant.ErrorCode; +import com.xit.core.exception.CustomBaseException; import com.xit.core.support.jpa.JpaUtil; import com.xit.core.util.AssertUtils; -import com.xit.core.util.CommUtil; +import com.xit.core.util.Checks; import com.xit.core.util.DateUtil; import io.jsonwebtoken.lang.Assert; import lombok.RequiredArgsConstructor; @@ -72,61 +66,76 @@ public class CtgyFileService implements ICtgyFileService { @Override @Transactional public List saveFiles(@Nonnull MinInfoBoard680 entity, MultipartFile[] files) { - String makePath = ""; - - makePath = File.separator + DateUtil.getToday(""); - - // 파일 경로 : upload root 제외 - String urlPath = this.uploadPath + makePath; - // 물리적인 파일 저장 위치 - String fileUploadPath = this.rootPath + urlPath; - File file = new File(fileUploadPath); - if(!file.exists()) file.mkdirs(); - List entityList = new ArrayList<>(); - for(MultipartFile mf : files){ - if(!mf.isEmpty()) { - String orgFileName = ""; - try { - orgFileName = StringUtils.cleanPath(Objects.requireNonNull(mf.getOriginalFilename())); - - // 파일 저장 && 전송 - MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); - savedEntity.setInFilename(orgFileName); - savedEntity.setInFilesize(mf.getSize()); - savedEntity.setInFileurl(serviceUrl+urlPath); - JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity); - - entityList.add(savedEntity); - mf.transferTo(new File(fileUploadPath + File.separator + orgFileName)); - - // inputStream을 가져와서 - // copyOfLocation (저장위치)로 파일을 쓴다. - // copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다 - //Files.copy(multipartFile.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING); - - }catch(IOException e){ - String errMsg = String.format("File Upload Error :: %s", orgFileName); - //TODO : 에러처리 - //return RestError.of(String.format("File Upload Error :: %s", orgFileName)); - AssertUtils.isTrue(false, String.format("File Upload Error :: %s", orgFileName)); + + if(files != null && files.length > 0){ + String makePath = File.separator + DateUtil.getToday(""); + + // 파일 경로 : upload root 제외 + String urlPath = this.uploadPath + makePath; + // 물리적인 파일 저장 위치 + String fileUploadPath = this.rootPath + urlPath; + File file = new File(fileUploadPath); + if(!file.exists()) file.mkdirs(); + + for(MultipartFile mf : files) { + if (!mf.isEmpty()) { + String orgFileName = ""; + try { + orgFileName = StringUtils.cleanPath(Objects.requireNonNull(mf.getOriginalFilename())); + + // 파일 저장 && 전송 + MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); + savedEntity.setInFilename(orgFileName); + savedEntity.setInFilesize(mf.getSize()); + savedEntity.setInFileurl(serviceUrl + urlPath); + savedEntity.setInBgubun(entity.getInBgubun()); + savedEntity.setInTitle(entity.getInTitle()); + savedEntity.setInContents(entity.getInContents()); + + JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity); + + entityList.add(savedEntity); + mf.transferTo(new File(fileUploadPath + File.separator + orgFileName)); + + // inputStream을 가져와서 + // copyOfLocation (저장위치)로 파일을 쓴다. + // copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다 + //Files.copy(multipartFile.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING); + + } catch (IOException e) { + String errMsg = String.format("File Upload Error :: %s", orgFileName); + //TODO : 에러처리 + //return RestError.of(String.format("File Upload Error :: %s", orgFileName)); + AssertUtils.isTrue(false, String.format("File Upload Error :: %s", orgFileName)); + } } } + }else{ + MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); + savedEntity.setInBgubun(entity.getInBgubun()); + savedEntity.setInTitle(entity.getInTitle()); + savedEntity.setInContents(entity.getInContents()); + JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity); } return entityList; } -// @Transactional -// public void removeExistsUploadFile(String fileMstId, String orgFileNm){ -// CmmFileDtl cmmFileDtl = cmmFileDtlRepository.findByFileMstIdAndOrgFileNmIgnoreCase(fileMstId, orgFileNm); -// if(cmmFileDtl != null){ -// CmmFileDtlIds cmmFileDtlIds = new CmmFileDtlIds(); -// cmmFileDtlIds.setFileMstId(fileMstId); -// cmmFileDtlIds.setFileId(cmmFileDtl.getFileId()); -// cmmFileDtlRepository.deleteById(cmmFileDtlIds); -// new File(this.uploadPath + cmmFileDtl.getFileUpldPath() + File.separator + cmmFileDtl.getFileId()).delete(); -// } -// } + @Override + public void removePublicBoardFile(Long inCode) { + + MinInfoBoard680 savedEntity = repository.findById(inCode).orElseThrow(() -> new CustomBaseException(ErrorCode.NOT_FOUND)); + + File file = null; + if(Checks.isNotEmpty(savedEntity.getInFilename())){ + String absFile = rootPath + savedEntity.getInFileurl().split(serviceUrl)[1]+File.separator + savedEntity.getInFilename(); + file = new File(absFile); + } + + // 정보 삭제후 파일 삭제 + repository.deleteById(inCode); + if(file.exists()) file.delete(); + } } /*