浏览代码

update 2024-03-25

junggilpark 1 年之前
父节点
当前提交
4375997fab

+ 2 - 1
src/main/java/com/its/web/controller/notice/NoticeController.java

@@ -13,6 +13,7 @@ import org.apache.ibatis.annotations.Param;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.annotation.Nullable;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import java.util.List;
@@ -50,7 +51,7 @@ public class NoticeController {
     @PostMapping(value = "/modifyNotice")
     @ResponseBody
     public ResultDto modifyNotice(@ModelAttribute NoticeDto.NoticeDtoUpdateReq param,
-                                  @Param("attachFile") MultipartFile attachFile) {
+                                  @Nullable @Param("attachFile") List<MultipartFile> attachFile) {
         return this.service.modifyNotice(param, attachFile);
     }
 

+ 4 - 0
src/main/java/com/its/web/dto/notice/NoticeDto.java

@@ -98,6 +98,10 @@ public class NoticeDto {
         @ApiModelProperty(value = "공지사항 내용", example = "내용", required = true)
         @JsonProperty("bContent")
         private String bContent;
+
+        @ApiModelProperty(value = "첨부파일 명칭", example = "||", required = true)
+        @JsonProperty("attachFileNames")
+        private String attachFileNames;
     }
 
 }

+ 90 - 27
src/main/java/com/its/web/service/notice/NoticeService.java

@@ -16,10 +16,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
 import java.io.*;
 import java.net.URLDecoder;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
+import java.util.*;
 
 @Slf4j
 @RequiredArgsConstructor
@@ -98,6 +95,23 @@ public class NoticeService {
     }
 
     public ResultDto deleteBoard(String boardNo) {
+        NoticeDto deleteDto = this.findNotice(boardNo);
+        String fileIds   = deleteDto.getAttachFileId();
+        if (fileIds != null){
+            String[] idArray = fileIds.split("\\|");
+            if (idArray.length > 0) {
+                for(String id : idArray) {
+                    File file = new File(boardLocation, id);
+                    if (file.exists()) {
+                        boolean isDelete = file.delete();
+                        if (!isDelete) {
+                            log.error("게시물 파일이 삭제되지 않았습니다. {}", id);
+                        }
+                    }
+                }
+            }
+        }
+
         int affectedRows = this.mapper.deleteBoard(boardNo);
         String message = "선택하신 게시물이 삭제되지 않았습니다.";
         String success = "F";
@@ -162,44 +176,93 @@ public class NoticeService {
         return CommonUtil.getResultDto(success, message);
     }
 
-    public ResultDto modifyNotice(NoticeDto.NoticeDtoUpdateReq param, MultipartFile attachFile) {
+    public ResultDto modifyNotice(NoticeDto.NoticeDtoUpdateReq param, List<MultipartFile> attachFile) {
         Map<String, Object> paramMap = new HashMap<>();
         paramMap.put("bSubject", param.getBSubject());
         paramMap.put("bContent", param.getBContent());
         paramMap.put("boardNo", param.getBoardNo());
-        String fileName = "";
-        String fileId = "";
-        String message = "";
+        String attachFileNames = param.getAttachFileNames();
+        Map<Integer, String> sameIndex = new HashMap<>();
+        String fileId = "||";
         String success = "S";
+        String message = "작성하신 게시물이 수정되었습니다.";
+
+        NoticeDto dto = this.findNotice(param.getBoardNo());
+
+        if (dto != null) {
+            if (!attachFileNames.equals(dto.getAttachFile())) {
+                String originAttachFile = dto.getAttachFile();
+                String originFileIds = dto.getAttachFileId();
+                if (originAttachFile != null && originFileIds != null) {
+                    String[] fileNameArr = originAttachFile.split("\\|");
+                    String[] fileIdArr = originFileIds.split("\\|");
+                    String[] nameArr = attachFileNames.split("\\|");
+                    // 기존 파일과 같은 이름이 있는지 찾고 있으면 유지 해줘야한다
+                    if (fileNameArr.length > 0) {
+                        for (int ii = 0; ii < fileNameArr.length; ii++) {
+                            String originName = fileNameArr[ii];
+                            if (nameArr.length >= (ii +1) && originName.equals(nameArr[ii])) {
+                                sameIndex.put(ii, fileIdArr[ii]);
+                            }
+                            else {
+                                if (fileIdArr.length > ii && fileIdArr[ii] != null) {
+                                    File file = new File(boardLocation, fileIdArr[ii]);
+                                    if (file.exists()) {
+                                        boolean isDelete = file.delete();
+                                        if (!isDelete) {
+                                            log.error("기존 파일 삭제가 진행되지 않았습니다. {}", fileIdArr[ii]);
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            else {
+                fileId = dto.getAttachFileId();
+            }
+        }
 
-        if (attachFile != null && !attachFile.isEmpty()){
-            fileName = attachFile.getOriginalFilename();
-
-            if (fileName != null) {
-                String ext = fileName.substring(fileName.lastIndexOf("."), fileName.length());
-                fileId   = UUID.randomUUID().toString().replaceAll("-", "") + ext;
-                paramMap.put("fileName", fileName);
-                int sameCount = this.mapper.checkImageName(paramMap);
-                if (sameCount == 0) {
-                    boolean isSuccess = uploadAttachFile(attachFile, fileId);
+        if (attachFile != null && attachFile.size() > 0) {
+            int cnt = 0;
+            for (MultipartFile file : attachFile) {
+                String orgName = file.getOriginalFilename();
+                if (orgName != null && orgName.lastIndexOf(".") > 0) {
+                    String ext = orgName.substring(orgName.lastIndexOf("."));
+                    String attachFileId = UUID.randomUUID().toString().replaceAll("-", "") + ext;
+                    if (sameIndex.size() == 0) {
+                        sameIndex.put(cnt++, attachFileId);
+                    }
+                    else {
+                        for (int ii = 0; ii < 3; ii++) {
+                            if (sameIndex.get(ii) == null) {
+                                sameIndex.put(ii, attachFileId);
+                                break;
+                            }
+                        }
+                    }
+                    boolean isSuccess = uploadAttachFile(file, attachFileId);
                     if (!isSuccess) {
                         message = "파일 생성 중 오류가 발생하였습니다.";
                         success = "F";
+                        return CommonUtil.getResultDto(success, message);
                     }
                 }
+            }
 
+            if (sameIndex.size() == 3) {
+                fileId = sameIndex.get(0) + "|" +sameIndex.get(1) + "|" + sameIndex.get(2);
             }
+
         }
 
-        if (success.equals("S")) {
-            paramMap.put("attachFile", fileName + "||");
-            paramMap.put("attachFileId", fileId + "||");
-            int affectedRows = this.mapper.updateNotice(paramMap);
-            message = "작성하신 게시물이 수정되었습니다.";
-            if (affectedRows <= 0) {
-                message = "작성하신 게시물이 수정되지 않았습니다.";
-                success = "F";
-            }
+        paramMap.put("attachFile", attachFileNames);
+        paramMap.put("attachFileId", fileId);
+        int affectedRows = this.mapper.updateNotice(paramMap);
+        if (affectedRows <= 0) {
+            message ="작성하신 게시물이 수정되지 않았습니다.";
+            success = "F";
         }
 
         return CommonUtil.getResultDto(success, message);

+ 12 - 2
src/main/resources/templates/admin/notice-view.html

@@ -92,7 +92,7 @@
         getDataAsync('/api/notice/attach', 'POST', param, null, (jsonData)=>{
             const attachFile = jsonData.attach_file;
             const ext = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
-            if (attachFile.length > 0) {
+            if (attachFile && attachFile.length > 0) {
                 if (!ext) {
                     // return alert("파일 확장자명이 잘못 되었습니다.");
                     return alertError('파일 확장자명이 잘못 되었습니다.', '공지사항', null);
@@ -229,7 +229,17 @@
         formData.append("boardNo", notice.board_no);
         formData.append("bSubject", title);
         formData.append("bContent", content);
-        formData.append("attachFile", file);
+
+        if (file && file.name) {
+            formData.append("attachFileNames", file.name + "|" + file.name + "|" + file.name);
+            formData.append("attachFile", file);
+            formData.append("attachFile", file);
+            formData.append("attachFile", file);
+        }
+        else {
+            formData.append("attachFile", null);
+            formData.append("attachFileNames", "||");
+        }
         $.ajax({
             url: '/api/notice/modifyNotice',
             processData : false,