개발 무지렁이

[Spring Boot] 결과보고서 RsData 본문

Backend/스프링부트

[Spring Boot] 결과보고서 RsData

Gaejirang-e 2022. 12. 29. 19:24

[dto.RsData.java]

@AllArgsConstructor
@Getter
@ToString
public class RsData<T> {
    private final String resultCode;
    private final String msg;
    private final T body;

    public boolean isSuccess() {
        return resultCode.startsWith("S-");
    }

    public boolean isFail() {
        return isSuccess() == false;
    }
}

[GenFileService.java]

@Service
@RequiredArgsConstructor
public class GenFileService {
    private final GenFileRepository genFileRepository;

    public RsData<Map<String, GenFile>> saveFiles(Article article, Map<String, MultipartFile> fileMap) {
        String relTypeCode = "article";
        long relId = article.getId();

        Map<String, GenFile> genFileIds = new HashMap<>();

        for (String inputName : fileMap.keySet()) {
            MultipartFile multipartFile = fileMap.get(inputName);

            if (multipartFile.isEmpty()) {
                continue;
            }

            String[] inputNameBits = inputName.split("__");

            String typeCode = inputNameBits[0];
            String type2Code = inputNameBits[1];
            String originFileName = multipartFile.getOriginalFilename();
            String fileExt = Util.file.getExt(originFileName);
            String fileExtTypeCode = Util.file.getFileExtTypeCodeFromFileExt(fileExt);
            String fileExtType2Code = Util.file.getFileExtType2CodeFromFileExt(fileExt);
            int fileNo = Integer.parseInt(inputNameBits[2]);
            int fileSize = (int) multipartFile.getSize();
            String fileDir = relTypeCode + "/" + Util.date.getCurrentDateFormatted("yyyy_MM_dd");

            GenFile genFile = GenFile
                    .builder()
                    .relTypeCode(relTypeCode)
                    .relId(relId)
                    .typeCode(typeCode)
                    .type2Code(type2Code)
                    .fileExtTypeCode(fileExtTypeCode)
                    .fileExtType2Code(fileExtType2Code)
                    .fileNo(fileNo)
                    .fileSize(fileSize)
                    .fileDir(fileDir)
                    .fileExt(fileExt)
                    .originFileName(originFileName)
                    .build();

            genFileRepository.save(genFile);

            String filePath = AppConfig.GET_FILE_DIR_PATH + "/" + fileDir + "/" + genFile.getFileName();

            File file = new File(filePath);

            file.getParentFile().mkdirs();

            try {
                multipartFile.transferTo(file);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            genFileIds.put(inputName, genFile);
        }

        return new RsData("S-1", "파일을 업로드했습니다.", genFileIds);
    }
}
RsData (
  resultCode=S-1,  
  msg=파일을 업로드했습니다.,
  body={
    common__bodyImg__2=GenFile(super=BaseEntity(id=2, createDate=2022-12-29T18:59:41.967930500, modifyDate=2022-12-29T18:59:41.967930500), 
    relTypeCode=article, 
    relId=1, 
    typeCode=common, 
    type2Code=bodyImg, 
    fileExtTypeCode=img, 
    fileExtType2Code=jpg, 
    fileSize=89449, 
    fileNo=2, 
    fileExt=jpg, 
    fileDir=article/2022_12_29, 
    originFileName=vintage-g8881ba0be_640.jpg), 
    common__bodyImg__1=GenFile(super=BaseEntity(id=1, createDate=2022-12-29T18:59:41.936012700, modifyDate=2022-12-29T18:59:41.936012700), 
    relTypeCode=article, 
    relId=1, 
    typeCode=common, 
    type2Code=bodyImg, 
    fileExtTypeCode=img, 
    fileExtType2Code=jpg, 
    fileSize=119736, 
    fileNo=1, 
    fileExt=jpg, 
    fileDir=article/2022_12_29, 
    originFileName=thomas-charters-zAi2Is48-MA-unsplash.jpg)
  }
)
Comments