package com.dbnt.kcgfilemanager.service; import com.dbnt.kcgfilemanager.config.LogStatus; import com.dbnt.kcgfilemanager.mapper.BoardMapper; import com.dbnt.kcgfilemanager.model.*; import com.dbnt.kcgfilemanager.repository.*; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.UUID; @Service @RequiredArgsConstructor public class BoardService { private final BoardRepository boardRepository; private final BoardLogRepository boardLogRepository; private final HashTagRepository hashTagRepository; private final HashTagLinkRepository hashTagLinkRepository; private final FileInfoRepository fileInfoRepository; private final BoardCategoryRepository boardCategoryRepository; private final BoardMapper boardMapper; private final UserInfoRepository userInfoRepository; @Transactional public Integer saveContent(Board content){ Integer contentSeq = boardRepository.save(content).getContentSeq(); saveBoardLog(content.getContentSeq(), LogStatus.WRITE, null, content.getCreateId()); saveHashTagLink(contentSeq, content.getHashTagStr()); saveUploadFiles(content); return contentSeq; } @Transactional public Integer updateContent(Board updateContent, UserInfo userInfo) { int contentSeq = updateContent.getContentSeq(); Board savedContent = boardRepository.findById(contentSeq).orElse(null); savedContent.setCategorySeq(updateContent.getCategorySeq()); savedContent.setTitle(updateContent.getTitle()); savedContent.setDescription(updateContent.getDescription()); saveBoardLog(contentSeq, LogStatus.MODIFY, null, userInfo.getUserId()); return contentSeq; } private void saveBoardLog(Integer contentSeq, LogStatus status, String description, String createId){ BoardLog lastLog = boardLogRepository.findTopByContentSeqOrderByLogSeqDesc(contentSeq).orElse(null); BoardLog log = new BoardLog(); log.setContentSeq(contentSeq); log.setLogSeq(lastLog == null?1:(lastLog.getLogSeq()+1)); log.setLogStatus(status.name()); log.setDescription(description); log.setCreateId(createId); boardLogRepository.save(log); } private HashTag saveHashTag(String tagName){ HashTag tag = new HashTag(); tag.setTagName(tagName); return hashTagRepository.save(tag); } private void saveHashTagLink(Integer contentSeq, String hashTagStr){ String[] hashTagAry = hashTagStr.trim().replaceAll(",", "").split("#"); for(String tagName : hashTagAry){ if(!tagName.equals("")){ HashTag tag = hashTagRepository.findByTagName(tagName).orElse(null); if(tag==null){ tag = saveHashTag(tagName); } HashTagLink link = new HashTagLink(); link.setContentSeq(contentSeq); link.setTagSeq(tag.getTagSeq()); hashTagLinkRepository.save(link); } } } private void saveUploadFiles(Board content){ FileInfo lastFileInfo = fileInfoRepository.findTopByContentSeqOrderByFileSeqDesc(content.getContentSeq()).orElse(null); int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1); for(MultipartFile file : content.getFileList()){ String saveName = UUID.randomUUID().toString(); String path = makeFilePath(content.getCategorySeq()); File saveFile = new File(path+"\\"+saveName); if(file.getSize()!=0){ // 저장될 파일 확인 if(!saveFile.exists()){ // 저장될 경로 확인 if(saveFile.getParentFile().mkdirs()){ try{ saveFile.createNewFile(); }catch (IOException e){ e.printStackTrace(); } } } try { file.transferTo(saveFile); }catch (IllegalStateException | IOException e){ e.printStackTrace(); } } String[] originalFilename = Objects.requireNonNull(file.getOriginalFilename()).split("\\."); FileInfo fileInfo = new FileInfo(); fileInfo.setContentSeq(content.getContentSeq()); fileInfo.setFileSeq(fileSeq++); fileInfo.setOriginalName(originalFilename[0]); fileInfo.setExtention(originalFilename[1]); fileInfo.setConversionName(saveName); fileInfo.setFileSize(calculationFileSize(file.getSize())); fileInfo.setSavePath(path); fileInfoRepository.save(fileInfo); saveBoardLog(content.getContentSeq(), LogStatus.FILE_ADD, fileInfo.getFullName(), content.getCreateId()); } } public String getPageTitle(Integer categorySeq) { BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null); if(category.getParentSeq()==null){ return category.getCategoryName(); } return getPageTitle(category.getParentSeq())+" > "+category.getCategoryName(); } public List selectContentList(Board board) { return boardMapper.selectContentList(board); } public Integer selectContentListCnt(Board board) { return boardMapper.selectContentListCnt(board); } @Transactional public Board selectContentByContentSeqAndViewCntUp(Integer contentSeq) { Board target = selectContent(contentSeq); if(target.getStatus()==null){ target.setViewCnt(target.getViewCnt()+1); } return target; } public Board selectContent(int contentSeq){ Board content = boardRepository.findById(contentSeq).orElse(null); content.setCreateName(userInfoRepository.findByUserId(content.getCreateId()).orElse(null).getUserId()); return content; } public Board selectContentModifyInfo(int contentSeq){ Board content = selectContent(contentSeq); content = selectContentForeignAttribute(content); StringBuilder hashTagStr = new StringBuilder(); for(HashTag tag : content.getHashTagList()){ hashTagStr.append("#").append(tag.getTagName()).append(", "); } if(!hashTagStr.toString().equals("")) { hashTagStr = new StringBuilder(hashTagStr.substring(0, hashTagStr.length() - 2)); content.setHashTagStr(hashTagStr.toString()); } return content; } public List selectDownloadFileInfoList(Integer contentSeq, List fileSeqList, String userId) { List fileList = fileInfoRepository.findByContentSeqOrderByFileSeqAsc(contentSeq); List targetList = new ArrayList<>(); for(FileInfo file: fileList){ if(fileSeqList.contains(file.getFileSeq())){ targetList.add(file); saveBoardLog(contentSeq, LogStatus.FILE_DOWN, file.getFullName(), userId); } } return targetList; } public FileInfo selectDownloadFileInfo(Integer contestSeq, Integer fileSeq, String userId){ FileInfo fileInfo = fileInfoRepository.findById(new FileInfo.FileInfoId(contestSeq, fileSeq)).orElse(null); saveBoardLog(contestSeq, LogStatus.FILE_DOWN, fileInfo.getFullName(), userId); return fileInfo; } public List selectContentLog(Integer contentSeq){ return boardMapper.selectBoardLogFromContentSeq(contentSeq); } public Board selectContentForeignAttribute(Board target) { target.setHashTagList(boardMapper.selectHashTagListFromContentSeq(target.getContentSeq())); target.setChildFileList(fileInfoRepository.findByContentSeqOrderByFileSeqAsc(target.getContentSeq())); return target; } @Transactional public Integer deleteContent(Board content, UserInfo user) { int contentSeq = content.getContentSeq(); content = boardRepository.findById(contentSeq).orElse(null); content.setTitle("삭제된 게시물"); content.setDescription(null); content.setStatus("D"); saveBoardLog(contentSeq, LogStatus.DELETE, null, user.getUserId()); deleteHashTagLink(contentSeq); deleteFileInfo(contentSeq); return contentSeq; } private void deleteHashTagLink(int contentSeq){ List tagLinkList = hashTagLinkRepository.findByContentSeq(contentSeq); hashTagLinkRepository.deleteAll(tagLinkList); } private void deleteFileInfo(int contentSeq){ List fileInfoList = fileInfoRepository.findByContentSeqOrderByFileSeqAsc(contentSeq); for(FileInfo fileInfo: fileInfoList){ File file = new File(fileInfo.getSavePath(), fileInfo.getConversionName()); file.delete(); } fileInfoRepository.deleteAll(fileInfoList); } private String makeFilePath(Integer categorySeq){ BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null); if(category.getParentSeq()==null){ return "D:\\kcgFileManager\\"+category.getCategoryName(); } return makeFilePath(category.getParentSeq())+"\\"+category.getCategoryName(); } private String calculationFileSize(Long fileSize){ String[] units = {"bytes", "KB", "MB"}; double unitSelector = Math.floor(Math.log(fileSize)/Math.log(1024)); return Math.round((fileSize/Math.pow(1024, unitSelector))*100)/100d+" "+units[(int)unitSelector]; } }