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