refactor: 중복코드 제거

This commit is contained in:
shinmj
2021-12-30 14:17:35 +09:00
parent 64a3877619
commit e7b275b44b
5 changed files with 81 additions and 59 deletions

View File

@@ -1,9 +1,11 @@
package org.egovframe.cloud.portalservice.api.attachment.dto; package org.egovframe.cloud.portalservice.api.attachment.dto;
import java.util.Objects;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.springframework.util.StringUtils;
/** /**
* org.egovframe.cloud.portalservice.api.attachment.dto.AttachmentSaveRequestDto * org.egovframe.cloud.portalservice.api.attachment.dto.AttachmentSaveRequestDto
@@ -49,4 +51,8 @@ public class AttachmentTempSaveRequestDto {
this.entityId = entityId; this.entityId = entityId;
this.isDelete = isDelete; this.isDelete = isDelete;
} }
public boolean hasUniqueId() {
return Objects.nonNull(uniqueId) || StringUtils.hasText(uniqueId);
}
} }

View File

@@ -1,5 +1,6 @@
package org.egovframe.cloud.portalservice.domain.attachment; package org.egovframe.cloud.portalservice.domain.attachment;
import java.util.Objects;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@@ -9,6 +10,7 @@ import org.egovframe.cloud.servlet.domain.BaseEntity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.EmbeddedId; import javax.persistence.EmbeddedId;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.springframework.util.StringUtils;
/** /**
* org.egovframe.cloud.portalservice.domain.attachment.Attachment * org.egovframe.cloud.portalservice.domain.attachment.Attachment
@@ -125,5 +127,13 @@ public class Attachment extends BaseEntity {
return this; return this;
} }
public boolean hasEntityId() {
return (Objects.nonNull(entityId) || StringUtils.hasText(entityId)) && !"-1".equals(entityId);
}
public boolean isDeleted() {
return Boolean.TRUE.equals(isDelete);
}
} }

View File

@@ -1,5 +1,7 @@
package org.egovframe.cloud.portalservice.service.attachment; package org.egovframe.cloud.portalservice.service.attachment;
import java.io.File;
import java.util.Objects;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
@@ -50,6 +52,11 @@ import java.util.stream.Collectors;
@Transactional @Transactional
@Service @Service
public class AttachmentService extends AbstractService { public class AttachmentService extends AbstractService {
private static final String SUCCESS_MESSAGE = "Success";
private static final String FILE_SEPARATOR = File.separator;
private static final String EDITOR_FILE_SEPARATOR = "-";
private static final String BASE_PATH = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
private static final String EDITOR_PATH = "editor/"+BASE_PATH;
private final AttachmentRepository attachmentRepository; private final AttachmentRepository attachmentRepository;
private final StorageUtils storageUtils; private final StorageUtils storageUtils;
@@ -63,10 +70,7 @@ public class AttachmentService extends AbstractService {
* @return * @return
*/ */
public AttachmentFileResponseDto uploadFile(MultipartFile file) { public AttachmentFileResponseDto uploadFile(MultipartFile file) {
return upload(file, BASE_PATH, true);
String basePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
return upload(file, basePath, true);
} }
/** /**
@@ -82,8 +86,8 @@ public class AttachmentService extends AbstractService {
String storeFile = storageUtils.storeFile(file, basePath, isTemp); String storeFile = storageUtils.storeFile(file, basePath, isTemp);
return AttachmentFileResponseDto.builder() return AttachmentFileResponseDto.builder()
.originalFileName(file.getOriginalFilename()) .originalFileName(file.getOriginalFilename())
.physicalFileName(StringUtils.cleanPath(basePath + "/" + storeFile)) .physicalFileName(StringUtils.cleanPath(basePath + FILE_SEPARATOR + storeFile))
.message("Success") .message(SUCCESS_MESSAGE)
.size(file.getSize()) .size(file.getSize())
.fileType(file.getContentType()) .fileType(file.getContentType())
.build(); .build();
@@ -99,11 +103,10 @@ public class AttachmentService extends AbstractService {
* @return * @return
*/ */
public List<AttachmentFileResponseDto> uploadFiles(List<MultipartFile> files) { public List<AttachmentFileResponseDto> uploadFiles(List<MultipartFile> files) {
String basePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
List<AttachmentFileResponseDto> responseDtoList = new ArrayList<>(); List<AttachmentFileResponseDto> responseDtoList = new ArrayList<>();
for (MultipartFile file : files) { for (MultipartFile file : files) {
responseDtoList.add(upload(file, basePath, true)); responseDtoList.add(upload(file, BASE_PATH, true));
} }
return responseDtoList; return responseDtoList;
} }
@@ -117,7 +120,7 @@ public class AttachmentService extends AbstractService {
public AttachmentEditorResponseDto uploadEditor(AttachmentBase64RequestDto editorRequestDto) throws BusinessMessageException { public AttachmentEditorResponseDto uploadEditor(AttachmentBase64RequestDto editorRequestDto) throws BusinessMessageException {
String fileBase64 = editorRequestDto.getFileBase64(); String fileBase64 = editorRequestDto.getFileBase64();
if (fileBase64 == null || fileBase64.equals("")) { if (Objects.isNull(fileBase64) || "".equals(fileBase64)) {
// 업로드할 파일이 없습니다. // 업로드할 파일이 없습니다.
throw new BusinessMessageException("valid.file.not_exists"); throw new BusinessMessageException("valid.file.not_exists");
} }
@@ -127,16 +130,15 @@ public class AttachmentService extends AbstractService {
throw new BusinessMessageException(getMessage("valid.file.too_big")); throw new BusinessMessageException(getMessage("valid.file.too_big"));
} }
String basePath = "editor/" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM")); String storeFile = storageUtils.storeBase64File(editorRequestDto, EDITOR_PATH);
String storeFile = storageUtils.storeBase64File(editorRequestDto, basePath);
return AttachmentEditorResponseDto.builder() return AttachmentEditorResponseDto.builder()
.uploaded(1) .uploaded(1)
.url(basePath.replaceAll("/", "-") + "-" + storeFile) .url(EDITOR_PATH.replaceAll(FILE_SEPARATOR, EDITOR_FILE_SEPARATOR) + EDITOR_FILE_SEPARATOR + storeFile)
.originalFileName(editorRequestDto.getOriginalName()) .originalFileName(editorRequestDto.getOriginalName())
.size(editorRequestDto.getSize()) .size(editorRequestDto.getSize())
.fileType(editorRequestDto.getFileType()) .fileType(editorRequestDto.getFileType())
.message("Success") .message(SUCCESS_MESSAGE)
.build(); .build();
} }
@@ -148,8 +150,7 @@ public class AttachmentService extends AbstractService {
*/ */
@Transactional(readOnly = true) @Transactional(readOnly = true)
public AttachmentImageResponseDto loadImage(String imagename) { public AttachmentImageResponseDto loadImage(String imagename) {
imagename = imagename.replaceAll("-", "/"); return storageUtils.loadImage(imagename.replaceAll(EDITOR_FILE_SEPARATOR, FILE_SEPARATOR));
return storageUtils.loadImage(imagename);
} }
/** /**
@@ -160,9 +161,7 @@ public class AttachmentService extends AbstractService {
*/ */
@Transactional(readOnly = true) @Transactional(readOnly = true)
public AttachmentImageResponseDto loadImageByUniqueId(String uniqueId) throws EntityNotFoundException { public AttachmentImageResponseDto loadImageByUniqueId(String uniqueId) throws EntityNotFoundException {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId) Attachment attachment = findAttachmentByUniqueId(uniqueId);
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
return storageUtils.loadImage(attachment.getPhysicalFileName()); return storageUtils.loadImage(attachment.getPhysicalFileName());
} }
@@ -174,11 +173,9 @@ public class AttachmentService extends AbstractService {
* @return * @return
*/ */
public AttachmentDownloadResponseDto downloadFile(String uniqueId) throws EntityNotFoundException, BusinessMessageException { public AttachmentDownloadResponseDto downloadFile(String uniqueId) throws EntityNotFoundException, BusinessMessageException {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId) Attachment attachment = findAttachmentByUniqueId(uniqueId);
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
if (Boolean.TRUE.equals(attachment.getIsDelete())) { if (attachment.isDeleted()) {
throw new BusinessMessageException(getMessage("err.entity.not.found")); throw new BusinessMessageException(getMessage("err.entity.not.found"));
} }
@@ -214,9 +211,7 @@ public class AttachmentService extends AbstractService {
* @return * @return
*/ */
public AttachmentDownloadResponseDto downloadAttachment(String uniqueId) throws EntityNotFoundException { public AttachmentDownloadResponseDto downloadAttachment(String uniqueId) throws EntityNotFoundException {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId) Attachment attachment = findAttachmentByUniqueId(uniqueId);
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
Resource resource = storageUtils.downloadFile(attachment.getPhysicalFileName()); Resource resource = storageUtils.downloadFile(attachment.getPhysicalFileName());
@@ -277,11 +272,12 @@ public class AttachmentService extends AbstractService {
for (AttachmentTempSaveRequestDto saveRequestDto : saveRequestDtoList) { for (AttachmentTempSaveRequestDto saveRequestDto : saveRequestDtoList) {
// 사용자 삭제인 경우 삭제여부 Y // 사용자 삭제인 경우 삭제여부 Y
if (saveRequestDto.isDelete()) { if (saveRequestDto.isDelete()) {
Attachment attachment = attachmentRepository.findAllByUniqueId(saveRequestDto.getUniqueId()) Attachment attachment = findAttachmentByUniqueId(saveRequestDto.getUniqueId());
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + saveRequestDto.getUniqueId()));
attachment.updateIsDelete(saveRequestDto.isDelete()); attachment.updateIsDelete(saveRequestDto.isDelete());
} else if (saveRequestDto.getUniqueId() == null || saveRequestDto.getUniqueId().equals("")) { continue;
}
if (!saveRequestDto.hasUniqueId()) {
// 해당 attachment에 seq 조회해서 attachmentid 생성 // 해당 attachment에 seq 조회해서 attachmentid 생성
AttachmentId attachmentId = attachmentRepository.getId(attachmentCode); AttachmentId attachmentId = attachmentRepository.getId(attachmentCode);
//새로운 첨부파일 저장 (물리적 파일 .temp 제거) //새로운 첨부파일 저장 (물리적 파일 .temp 제거)
@@ -324,10 +320,7 @@ public class AttachmentService extends AbstractService {
* @return * @return
*/ */
public String toggleDelete(String uniqueId, boolean isDelete) throws EntityNotFoundException { public String toggleDelete(String uniqueId, boolean isDelete) throws EntityNotFoundException {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId) Attachment attachment = findAttachmentByUniqueId(uniqueId);
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
attachment.updateIsDelete(isDelete); attachment.updateIsDelete(isDelete);
return uniqueId; return uniqueId;
} }
@@ -338,16 +331,8 @@ public class AttachmentService extends AbstractService {
* @param uniqueId * @param uniqueId
*/ */
public void delete(String uniqueId) throws EntityNotFoundException { public void delete(String uniqueId) throws EntityNotFoundException {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId) Attachment attachment = findAttachmentByUniqueId(uniqueId);
// 파일을 찾을 수 없습니다. deleteFile(attachment);
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
// 물리적 파일 삭제
boolean deleted = storageUtils.deleteFile(attachment.getPhysicalFileName());
if (deleted) {
attachmentRepository.delete(attachment);
} else {
throw new BusinessMessageException(getMessage("valid.file.not_deleted"));
}
} }
/** /**
@@ -357,7 +342,6 @@ public class AttachmentService extends AbstractService {
* @param uploadRequestDto * @param uploadRequestDto
*/ */
public String uploadAndSave(List<MultipartFile> files, AttachmentUploadRequestDto uploadRequestDto) { public String uploadAndSave(List<MultipartFile> files, AttachmentUploadRequestDto uploadRequestDto) {
String basePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
String attachmentCode = PortalUtils.randomAlphanumeric(20); String attachmentCode = PortalUtils.randomAlphanumeric(20);
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
@@ -367,7 +351,7 @@ public class AttachmentService extends AbstractService {
.build(); .build();
// 물리적 파일 생성 // 물리적 파일 생성
AttachmentFileResponseDto fileResponseDto = upload(files.get(i), basePath, false); AttachmentFileResponseDto fileResponseDto = upload(files.get(i), BASE_PATH, false);
attachmentRepository.save( attachmentRepository.save(
Attachment.builder() Attachment.builder()
@@ -402,15 +386,12 @@ public class AttachmentService extends AbstractService {
String attachmentCode, String attachmentCode,
AttachmentUploadRequestDto uploadRequestDto, AttachmentUploadRequestDto uploadRequestDto,
List<AttachmentUpdateRequestDto> updateRequestDtoList) throws EntityNotFoundException { List<AttachmentUpdateRequestDto> updateRequestDtoList) throws EntityNotFoundException {
String basePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
// 기존 파일 삭제 처리 // 기존 파일 삭제 처리
if (updateRequestDtoList != null) { if (updateRequestDtoList != null) {
for (AttachmentUpdateRequestDto saveRequestDto : updateRequestDtoList) { for (AttachmentUpdateRequestDto saveRequestDto : updateRequestDtoList) {
if (saveRequestDto.getIsDelete()) { if (saveRequestDto.getIsDelete()) {
Attachment attachment = attachmentRepository.findAllByUniqueId(saveRequestDto.getUniqueId()) Attachment attachment = findAttachmentByUniqueId(saveRequestDto.getUniqueId());
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + saveRequestDto.getUniqueId()));
attachment.updateIsDelete(saveRequestDto.getIsDelete()); attachment.updateIsDelete(saveRequestDto.getIsDelete());
} }
} }
@@ -423,7 +404,7 @@ public class AttachmentService extends AbstractService {
AttachmentId attachmentId = attachmentRepository.getId(attachmentCode); AttachmentId attachmentId = attachmentRepository.getId(attachmentCode);
// 물리적 파일 생성 // 물리적 파일 생성
AttachmentFileResponseDto fileResponseDto = upload(files.get(i), basePath, false); AttachmentFileResponseDto fileResponseDto = upload(files.get(i), BASE_PATH, false);
attachmentRepository.save( attachmentRepository.save(
Attachment.builder() Attachment.builder()
@@ -470,23 +451,44 @@ public class AttachmentService extends AbstractService {
public void deleteAllEmptyEntity(String attachmentCode) throws EntityNotFoundException, BusinessMessageException { public void deleteAllEmptyEntity(String attachmentCode) throws EntityNotFoundException, BusinessMessageException {
List<Attachment> attachmentList = attachmentRepository.findByCode(attachmentCode); List<Attachment> attachmentList = attachmentRepository.findByCode(attachmentCode);
if (attachmentList == null || attachmentList.size() <= 0) { if (Objects.isNull(attachmentList) || attachmentList.size() <= 0) {
throw new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + attachmentCode); throw new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + attachmentCode);
} }
for (Attachment attachment: attachmentList) { for (Attachment attachment: attachmentList) {
// 첨부파일 저장 후 기능 저장 시 오류 날 경우에만 첨부파일 전체 삭제를 하므로 // 첨부파일 저장 후 기능 저장 시 오류 날 경우에만 첨부파일 전체 삭제를 하므로
// entity 정보가 있는 경우에는 삭제하지 못하도록 한다. // entity 정보가 있는 경우에는 삭제하지 못하도록 한다.
if ((attachment.getEntityId() != null || StringUtils.hasText(attachment.getEntityId())) && !attachment.getEntityId().equals("-1")) { if (attachment.hasEntityId()) {
throw new BusinessMessageException(getMessage("valid.file.not_deleted")); throw new BusinessMessageException(getMessage("valid.file.not_deleted"));
} }
deleteFile(attachment);
}
}
/**
* 첨부파일 삭제
*
* @param attachment
*/
private void deleteFile(Attachment attachment) {
// 물리적 파일 삭제 // 물리적 파일 삭제
boolean deleted = storageUtils.deleteFile(attachment.getPhysicalFileName()); boolean deleted = storageUtils.deleteFile(attachment.getPhysicalFileName());
if (deleted) { if (!deleted) {
attachmentRepository.delete(attachment);
} else {
throw new BusinessMessageException(getMessage("valid.file.not_deleted")); throw new BusinessMessageException(getMessage("valid.file.not_deleted"));
} }
attachmentRepository.delete(attachment);
} }
/**
* unique id 로 첨부파일 조회
*
* @param uniqueId
* @return
*/
private Attachment findAttachmentByUniqueId(String uniqueId) {
return attachmentRepository.findAllByUniqueId(uniqueId)
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
} }
} }

View File

@@ -1,6 +1,8 @@
package org.egovframe.cloud.portalservice.utils; package org.egovframe.cloud.portalservice.utils;
import java.nio.file.Files; import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;

View File

@@ -99,4 +99,6 @@ public interface StorageUtils {
* @return * @return
*/ */
public Path getStorePath(String basePath); public Path getStorePath(String basePath);
} }