package com.dbnt.faisp.config; import com.dbnt.faisp.main.authMgt.service.AuthMgtService; import com.dbnt.faisp.main.counterIntelligence.activityCase.service.ActivityCaseService; import com.dbnt.faisp.main.counterIntelligence.model.CounterIntelligenceActivity; import com.dbnt.faisp.main.counterIntelligence.model.CounterIntelligenceWork; import com.dbnt.faisp.main.counterIntelligence.service.CounterIntelligenceService; import com.dbnt.faisp.main.faRpt.model.FaRptBoard; import com.dbnt.faisp.main.faRpt.model.Sri; import com.dbnt.faisp.main.faRpt.service.FaRptService; import com.dbnt.faisp.main.faStatistics.unlawfulFishing.model.sailor.Sailor; import com.dbnt.faisp.main.faStatistics.unlawfulFishing.service.FishingBoatService; import com.dbnt.faisp.main.faStatistics.unlawfulFishing.service.SailorService; import com.dbnt.faisp.main.fpiMgt.affair.model.AffairBoard; import com.dbnt.faisp.main.fpiMgt.affair.service.AffairService; import com.dbnt.faisp.main.fpiMgt.affairPlan.model.PlanBoard; import com.dbnt.faisp.main.fpiMgt.affairPlan.service.PlanService; import com.dbnt.faisp.main.fpiMgt.affairResult.model.ResultBoard; import com.dbnt.faisp.main.fpiMgt.affairResult.service.ResultService; import com.dbnt.faisp.main.fpiMgt.intelligenceNetwork.model.OperationPlan; import com.dbnt.faisp.main.fpiMgt.intelligenceNetwork.service.IntelligenceNetworkService; import com.dbnt.faisp.main.fpiMgt.monitoring.model.MonitoringDesignation; import com.dbnt.faisp.main.fpiMgt.monitoring.model.MonitoringResult; import com.dbnt.faisp.main.fpiMgt.monitoring.service.MonitoringService; import com.dbnt.faisp.main.faRpt.service.SriService; import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.model.InvestigationBoard; import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.service.IvsgtService; import com.dbnt.faisp.main.ivsgtMgt.majorStatus.model.MajorStatus; import com.dbnt.faisp.main.ivsgtMgt.majorStatus.service.MajorStatusService; import com.dbnt.faisp.main.publicBoard.model.PublicBoard; import com.dbnt.faisp.main.publicBoard.service.PublicBoardService; import com.dbnt.faisp.main.userInfo.model.UserInfo; import com.dbnt.faisp.util.Utils; import lombok.RequiredArgsConstructor; import ognl.Ognl; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @RestController @RequiredArgsConstructor @RequestMapping("/file") public class FileController{ @Value("${spring.servlet.multipart.location}") protected String locationPath; @Value("${file.dir.editor}") protected String editorPath; @Value("${file.dir.affairTemp}") protected String affairTempPath; private final FaRptService faRptService; private final PlanService planService; private final PublicBoardService publicBoardService; private final AffairService affairService; private final ResultService resultService; private final IvsgtService ivsgtService; private final FishingBoatService fishingBoatService; private final SailorService sailorService; private final SriService sriService; private final CounterIntelligenceService ciService; private final MajorStatusService majorStatusService; private final IntelligenceNetworkService inrelligenceNetworkService; private final MonitoringService monitoringService; private final AuthMgtService authMgtService; private final ActivityCaseService activityCaseService; @GetMapping("/editorFileDisplay") public ResponseEntity editorFileDisplay(HttpServletRequest request, HttpServletResponse response, String fileNm) { String pathStr = locationPath+editorPath+File.separator+fileNm; Resource resource = new FileSystemResource(pathStr); if(!resource.exists()){ return new ResponseEntity<>(HttpStatus.NOT_FOUND); } HttpHeaders header = new HttpHeaders(); Path filePath = null; try { filePath = Paths.get(pathStr); header.add("Content-type", Files.probeContentType(filePath)); }catch (IOException e){ e.printStackTrace(); } return new ResponseEntity(resource, header, HttpStatus.OK); } @GetMapping("/fileDisplay") public ResponseEntity fileDisplay(HttpServletRequest request, HttpServletResponse response, String board, Integer parentKey, Integer fileSeq) { FileInfo fileInfo = getFileInfo(board, parentKey, fileSeq); String pathStr = fileInfo.getSavePath()+File.separator+fileInfo.getConvNm(); Resource resource = new FileSystemResource(pathStr); if(!resource.exists()){ return new ResponseEntity<>(HttpStatus.NOT_FOUND); } HttpHeaders header = new HttpHeaders(); Path filePath = null; try { filePath = Paths.get(pathStr); header.add("Content-type", Files.probeContentType(filePath)); }catch (IOException e){ e.printStackTrace(); } return new ResponseEntity(resource, header, HttpStatus.OK); } @GetMapping("/fileDownload") public void fileDownload(HttpServletRequest request, HttpServletResponse response, String board, Integer menuKey, Integer parentKey, Integer fileSeq, @AuthenticationPrincipal UserInfo loginUser) throws Exception { FileInfo fileInfo = null; if(Utils.isEmpty(menuKey)){ fileInfo = getFileInfo(board, parentKey, fileSeq); }else{ if(menuKey!=0){ String accessAuth = authMgtService.selectAccessAuth(menuKey, loginUser.getUserSeq()); Map wrtInfo = getWrtInfo(menuKey, parentKey); if(!Utils.isEmpty(accessAuth)){ if(wrtOrganCheck(loginUser.getUserSeq(), accessAuth, wrtInfo, loginUser.getDownOrganCdList())){ fileInfo = getFileInfoToMenuKey(menuKey, parentKey, fileSeq); }else{ throw new Exception(); } }else{ throw new Exception(); } }else{ fileInfo = getFileInfoToMenuKey(menuKey, parentKey, fileSeq); } } BufferedInputStream in; BufferedOutputStream out; try { File file = new File(fileInfo.getSavePath(), fileInfo.getConvNm()); Utils.setDisposition(fileInfo.getFullName(), request, response); in = new BufferedInputStream(new FileInputStream(file)); out = new BufferedOutputStream(response.getOutputStream()); FileCopyUtils.copy(in, out); out.flush(); if(out!=null) out.close(); if(in!=null )in.close(); } catch (IOException e) { e.printStackTrace(); } } private boolean wrtOrganCheck(Integer userSeq, String accessAuth, Map wrtInfo, List mgtOrganList) { if (accessAuth.equals("ACC003")){ return mgtOrganList.contains(wrtInfo.get("wrtOrgan").toString()); }else if(accessAuth.equals("ACC002")||accessAuth.equals("ACC001")){ return userSeq.equals(wrtInfo.get("wrtUserSeq")); } return false; } @GetMapping("/downloadAffairFiles") public void downloadAffairFiles(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="fileNm", defaultValue = "견문보고서 첨부파일_") String fileNm, @RequestParam(value="affairKeyList") List affairKeyList){ List fileList = affairService.selectAffairFileList(affairKeyList); makeZipAndOut(request, response, fileNm, fileList); } @GetMapping("/downloadMajorStatusFiles") public void downloadMajorStatusFiles(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="majorKeyList") List majorKeyList){ List fileList = majorStatusService.selectMajorFileList(majorKeyList); makeZipAndOut(request, response, "주요사건처리현황 첨부파일_", fileList); } private void makeZipAndOut(HttpServletRequest request, HttpServletResponse response, String fileName, List fileList) { List> fileInfoList = new ArrayList<>(); for(FileInfo file: fileList){ Map fileInfoMap = new HashMap<>(); fileInfoMap.put("filePath", file.getSavePath()+File.separator+file.getConvNm()); fileInfoMap.put("originalName", file.getOrigNm()+"."+file.getFileExtn()); fileInfoList.add(fileInfoMap); } String zipFile = locationPath+affairTempPath; File saveFolder = new File(zipFile); if (!saveFolder.exists() || saveFolder.isFile()) { saveFolder.mkdirs(); } String downloadFileName = fileName+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+".zip"; zipFile += File.separator+downloadFileName; try{ // ZipOutputStream을 FileOutputStream 으로 감쌈 FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout = new ZipOutputStream(fout); for(Map fileMap: fileInfoList){ //본래 파일명 유지, 경로제외 파일압축을 위해 new File로 ZipEntry zipEntry = new ZipEntry(fileMap.get("originalName")); zout.putNextEntry(zipEntry); //경로포함 압축 //zout.putNextEntry(new ZipEntry(sourceFiles.get(i))); FileInputStream fin = new FileInputStream(fileMap.get("filePath")); byte[] buffer = new byte[1024]; int length; // input file을 1024바이트로 읽음, zip stream에 읽은 바이트를 씀 while((length = fin.read(buffer)) > 0){ zout.write(buffer, 0, length); } zout.closeEntry(); fin.close(); } zout.close(); Utils.setDisposition(downloadFileName, request, response); BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile)); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); FileCopyUtils.copy(in, out); out.flush(); /*byte[] data=new byte[2048]; int input=0; while((input=in.read(data))!=-1){ out.write(data,0,input); out.flush(); }*/ if(out!=null) out.close(); if(in!=null) in.close(); } catch(IOException e){ e.printStackTrace(); } } private FileInfo getFileInfo(String board, Integer parentKey, Integer fileSeq){ FileInfo downloadFile = null; switch (board){ case "faRpt": downloadFile = faRptService.selectFaRptFile(parentKey, fileSeq); break; case "affairPlan": downloadFile = planService.selectPlanFile(parentKey, fileSeq); break; case "publicFile": downloadFile = publicBoardService.selectPublicFile(parentKey, fileSeq); break; case "affair": downloadFile = affairService.selectAffairFile(parentKey, fileSeq); break; case "affairResult": downloadFile = resultService.selectResultFile(parentKey, fileSeq); break; case "ivsgt": downloadFile = ivsgtService.selectIvsgtFile(parentKey, fileSeq); break; case "sailor": downloadFile = sailorService.selectSailorFile(parentKey, fileSeq); break; case "sri": downloadFile = sriService.selectFaSriFile(parentKey, fileSeq); break; case "ciWork": downloadFile = ciService.selectCiWorkFile(parentKey, fileSeq); break; case "majorFile" : downloadFile = majorStatusService.selectMajorFile(parentKey, fileSeq); break; case "ciActivity": downloadFile = ciService.selectCiaForeignerFile(parentKey, fileSeq); break; case "operationPlan": downloadFile = inrelligenceNetworkService.selectOperationPlanFile(parentKey, fileSeq); break; case "designation": downloadFile = monitoringService.selectDesignationFile(parentKey, fileSeq); break; case "monitoringResult": downloadFile = monitoringService.selectMonitoringResultFile(parentKey, fileSeq); break; case "monitoringReport": downloadFile = monitoringService.selectMonitoringReportFile(parentKey, fileSeq); break; case "activityCase": downloadFile = activityCaseService.selectActivityCaseFile(parentKey, fileSeq); break; } return downloadFile; } private FileInfo getFileInfoToMenuKey(Integer menuKey, Integer parentKey, Integer fileSeq){ FileInfo downloadFile = null; switch (menuKey){ case 0: downloadFile = publicBoardService.selectPublicFile(parentKey, fileSeq); break; case 1: downloadFile = faRptService.selectFaRptFile(parentKey, fileSeq); break; case 29: downloadFile = planService.selectPlanFile(parentKey, fileSeq); break; case 30:case 33: downloadFile = affairService.selectAffairFile(parentKey, fileSeq); break; case 31: downloadFile = resultService.selectResultFile(parentKey, fileSeq); break; case 2: downloadFile = ivsgtService.selectIvsgtFile(parentKey, fileSeq); break; case 44: downloadFile = sailorService.selectSailorFile(parentKey, fileSeq); break; case 40: downloadFile = sriService.selectFaSriFile(parentKey, fileSeq); break; case 5: downloadFile = majorStatusService.selectMajorFile(parentKey, fileSeq); break; case 6: downloadFile = ciService.selectCiWorkFile(parentKey, fileSeq); break; case 7: case 8: case 9: case 10:downloadFile = ciService.selectCiaForeignerFile(parentKey, fileSeq); break; case 32: downloadFile = inrelligenceNetworkService.selectOperationPlanFile(parentKey, fileSeq); break; case 36: downloadFile = monitoringService.selectDesignationFile(parentKey, fileSeq); break; case 37: downloadFile = monitoringService.selectMonitoringResultFile(parentKey, fileSeq); break; case 370: downloadFile = monitoringService.selectMonitoringReportFile(parentKey, fileSeq); break; } return downloadFile; } private Map getWrtInfo(Integer menuKey, Integer parentKey){ Map wrtInfo = new HashMap<>(); switch (menuKey){ case 0: PublicBoard board = publicBoardService.selectPublicBoard(parentKey); wrtInfo.put("wrtOrgan", board.getWrtOrgan()); wrtInfo.put("wrtUserSeq", board.getWrtUserSeq()); break; case 1: FaRptBoard faRpt = faRptService.selectFaRptBoard(parentKey, null); wrtInfo.put("wrtOrgan", faRpt.getWrtOrgan()); wrtInfo.put("wrtUserSeq", faRpt.getWrtUserSeq()); break; case 2: InvestigationBoard invest = ivsgtService.selectBoardInvestigation(parentKey); wrtInfo.put("wrtOrgan", invest.getWrtOrgan()); wrtInfo.put("wrtUserSeq", invest.getWrtUserSeq()); break; case 5: MajorStatus major = majorStatusService.selectMajor(parentKey); wrtInfo.put("wrtOrgan", major.getWrtOrgan()); wrtInfo.put("wrtUserSeq", major.getWrtUserSeq()); break; case 6: CounterIntelligenceWork ciw = ciService.selectCounterIntelligenceWork(parentKey); wrtInfo.put("wrtOrgan", ciw.getWrtOrgan()); wrtInfo.put("wrtUserSeq", ciw.getWrtUserSeq()); break; case 7: case 8: case 9: case 10: CounterIntelligenceActivity cia = ciService.selectCia(parentKey); wrtInfo.put("wrtOrgan", cia.getWrtOrgan()); wrtInfo.put("wrtUserSeq", cia.getWrtUserSeq()); break; case 29: PlanBoard plan = planService.selectPlanBoard(parentKey); wrtInfo.put("wrtOrgan", plan.getWrtOrgan()); wrtInfo.put("wrtUserSeq", plan.getWrtUserSeq()); break; case 30:case 33: AffairBoard affair = affairService.selectAffairBoard(parentKey); wrtInfo.put("wrtOrgan", affair.getWrtOrgan()); wrtInfo.put("wrtUserSeq", affair.getWrtUserSeq()); break; case 31: ResultBoard affairResult = resultService.selectResultBoard(parentKey); wrtInfo.put("wrtOrgan", affairResult.getWrtOrgan()); wrtInfo.put("wrtUserSeq", affairResult.getWrtUserSeq()); break; case 32: OperationPlan operationPlan = inrelligenceNetworkService.selectOperationPlan(parentKey); wrtInfo.put("wrtOrgan", operationPlan.getWrtOrgan()); wrtInfo.put("wrtUserSeq", operationPlan.getWrtUserSeq()); break; case 36: MonitoringDesignation designation = monitoringService.selectDesignation(parentKey); wrtInfo.put("wrtOrgan", designation.getWrtOrgan()); wrtInfo.put("wrtUserSeq", designation.getWrtUserSeq()); break; case 37:case 370: MonitoringResult monitoringResult = monitoringService.selectResult(parentKey); wrtInfo.put("wrtOrgan", monitoringResult.getWrtOrgan()); wrtInfo.put("wrtUserSeq", monitoringResult.getWrtUserSeq()); break; case 40: Sri sri = sriService.selectFaSriBoard(parentKey, null); wrtInfo.put("wrtOrgan", sri.getWrtOrgan()); wrtInfo.put("wrtUserSeq", sri.getWrtUserSeq()); break; case 44: Sailor sailor = sailorService.selectSailor(parentKey); wrtInfo.put("wrtOrgan", sailor.getWrtOrgan()); wrtInfo.put("wrtUserSeq", sailor.getWrtUserSeq()); break; } return wrtInfo; } }