package froala.editor.web; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; import egovframework.com.cmm.AjaxWrapper; import egovframework.com.cmm.service.EgovProperties; import egovframework.rte.fdl.cmmn.exception.EgovBizException; import egovframework.rte.fdl.property.EgovPropertyService; import froala.editor.EditorFileVO; import froala.editor.File; import froala.editor.Image; import froala.editor.Utils; import froala.editor.Video; import froala.editor.file.FileOptions; import froala.editor.image.ImageOptions; import froala.editor.service.EditorService; import froala.editor.utils.CommonUtil; import froala.editor.video.VideoOptions; /** * froala editor Controller */ @Controller public class EditorController { private static final Logger logger = LoggerFactory.getLogger(EditorController.class); private static final String FILE_ROUTE = EgovProperties.getProperty("editor.fileRoute"); private static final String TEMP_FILE_ROUTE = EgovProperties.getProperty("editor.fileTempRoute"); /** EgovSampleService */ @Resource(name = "editorService") private EditorService editorService; /** EgovPropertyService */ @Resource(name = "propertiesService") protected EgovPropertyService propertiesService; /** * 에디터용 파일 업로드 * * @param request * @return * @throws EgovBizException */ @RequestMapping(value = "/editor/uploadFile.do", produces = "application/json; charset=utf8") @ResponseBody public String editorUploadFile(HttpServletRequest request) { FileOptions options = new FileOptions(); options.setValidation(null); EditorFileVO fileVO = new EditorFileVO(); try { fileVO = File.upload(request, Utils.getAddDateRoute(FILE_ROUTE), options); // DB부분 String fileId = editorService.insertEditorFile(fileVO); fileVO.setFileId(fileId); fileVO.setFullPath(null); fileVO.setUploadDir(null); fileVO.setSysFileNm(null); String contextPath = request.getContextPath(); fileVO.setLink(contextPath + fileVO.getLink()); } catch (EgovBizException e) { logger.error("fail to process EditorController upload_file {}", e); Map responseData = new HashMap<>(); responseData.put("error", "fail to process Editor 파일업로드"); } Gson gson = new Gson(); return gson.toJson(fileVO); } /** * 에디터용 이미지 업로드 * * @param request * @return * @throws EgovBizException * @throws UnsupportedEncodingException */ @RequestMapping(value = "/editor/uploadImage.do", produces = "application/json; charset=utf8") @ResponseBody public String uploadImage(HttpServletRequest request) throws EgovBizException, UnsupportedEncodingException { ImageOptions options = new ImageOptions(); options.setValidation(null); EditorFileVO fileVO = new EditorFileVO(); try { fileVO = Image.upload(request, Utils.getAddDateRoute(FILE_ROUTE), options); // DB부분 String fileId = editorService.insertEditorFile(fileVO); fileVO.setFileId(fileId); fileVO.setFullPath(null); fileVO.setUploadDir(null); fileVO.setSysFileNm(null); String contextPath = request.getContextPath(); fileVO.setLink(contextPath + fileVO.getLink()); } catch (EgovBizException e) { logger.error("fail to process EditorController upload_image {}", e); Map responseData = new HashMap<>(); responseData.put("error", "fail to process Editor 이미지 업로드"); } Gson gson = new Gson(); return gson.toJson(fileVO); } /** * 에디터 파일 삭제 DB, 파일 삭제 하지 않음(다른 두개의 게시물에 파일 링크를 넣고 어느 한쪽에서 지우면 다른 한쪽의 파일이 없어지는 * 문제) * * @param request * @return * @throws IOException * @throws EgovBizException */ @RequestMapping(value = "/editor/deleteFile.do", produces = "application/json; charset=utf8") @ResponseBody public String deleteFile(HttpServletRequest request, HttpServletResponse response) throws EgovBizException, IOException { FileOptions options = new FileOptions(); String src = request.getParameter("src"); String contextPath = request.getContextPath(); src = src.replace(contextPath, ""); String fileId = src.replace(options.getDownloadUrl(), ""); fileId = URLDecoder.decode(fileId, "UTF-8"); fileId = CommonUtil.decryptAES256(fileId); EditorFileVO fileVO = new EditorFileVO(); try { // DB부분 fileVO = editorService.selectEditorFile(fileId); // editorService.deleteEditorFile(fileId); // 파일 삭제하지 않음 // File.delete(request, fileVO.getFullPath()); } catch (EgovBizException e) { logger.error("fail to process EditorController delete_file {}", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } Gson gson = new Gson(); return gson.toJson(fileVO); } /** * 에디터 이미지 삭제 파일 삭제와 같은 문제로 실제 DB, 파일은 삭제하지 않음 * * @param request * @return * @throws IOException * @throws EgovBizException */ @RequestMapping(value = "/editor/deleteImage.do", produces = "application/json; charset=utf8") @ResponseBody public String deleteImage(HttpServletRequest request, HttpServletResponse response) throws IOException { ImageOptions options = new ImageOptions(); String src = request.getParameter("src"); if (src.startsWith("blob:")) { return null; } String fileId = src.replace(options.getDownloadUrl(), ""); EditorFileVO fileVO = new EditorFileVO(); try { // DB부분 fileVO = editorService.selectEditorFile(fileId); // editorService.deleteEditorFile(fileId); // 파일 삭제하지 않음 // File.delete(request, fileVO.getFullPath()); } catch (EgovBizException e) { logger.error("fail to process EditorController deleteImage {}", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } Gson gson = new Gson(); return gson.toJson(fileVO); } /** * 첨부파일 목록 정보 * * @param fileVO fileId형식 : EFI_0000~~,EFI_0000~~ * @return * @throws EgovBizException */ @RequestMapping(value = "/editor/fileList.do", produces = "application/json; charset=utf8") @ResponseBody public String fileList(@ModelAttribute EditorFileVO fileVO, HttpServletRequest request) throws EgovBizException { String fileId = fileVO.getFileId(); List list = new ArrayList<>(); if (StringUtils.isNotBlank(fileId)) { String contextPath = request.getContextPath(); for (String tmp : fileId.split(",")) { EditorFileVO vo = editorService.selectEditorFile(tmp); vo.setLink(contextPath + vo.getLink()); list.add(vo); } } Gson gson = new Gson(); return gson.toJson(list); } /** * pdf, tiff, tif 해당 파일에 대한 전체 페이지 수를 가져온다 * * @param fileVO * @return * @throws EgovBizException */ @RequestMapping(value = "/editor/fileMaxSizeInfo.do", produces = "application/json; charset=utf8") @ResponseBody public String fileMaxSizeInfo(@ModelAttribute EditorFileVO fileVO) throws EgovBizException { fileVO = editorService.selectEditorFile(fileVO.getFileId()); String fileExtSn = fileVO.getFileExtsn(); int pageCount = 1; if ("pdf".indexOf(fileExtSn) > -1) { if (fileExtSn.equals("pdf")) { try { java.io.File file = new java.io.File(fileVO.getFullPath()); PDDocument pdfDoc = PDDocument.load(file); // Document 생성 pageCount = pdfDoc.getPages().getCount(); } catch (IOException e) { logger.error("fail to process EditorController fileMaxSizeInfo {}", e); } } } Gson gson = new Gson(); return gson.toJson(pageCount); } /** * 에디터용 동영상 업로드 * * @param request * @return * @throws EgovBizException */ @RequestMapping(value = "/editor/uploadVideo.do", produces = "application/json; charset=utf8") @ResponseBody public String uploadVideo(HttpServletRequest request) { VideoOptions options = new VideoOptions(); // options.setValidation(new VideoValidation ()); EditorFileVO fileVO = new EditorFileVO(); try { fileVO = Video.upload(request, FILE_ROUTE, options); // DB부분 String fileId = editorService.insertEditorFile(fileVO); fileVO.setFileId(fileId); } catch (EgovBizException | NoSuchAlgorithmException | IOException e) { logger.error("fail to process EditorController upload_video {}", e); Map responseData = new HashMap<>(); responseData.put("error", "fail to process Edtior 동영상 업로드"); } // fileVO 전체를 리턴하면 불필요한 데이터가 노출되어 link만 리턴 EditorFileVO resultVO = new EditorFileVO(); resultVO.setLink(fileVO.getLink()); Gson gson = new Gson(); return gson.toJson(resultVO); } /** * 첨부파일 목록 import용 * * @param fileVO * @param displayType simple: 간단히, detail: 상세 * @param title * @param divId * @param model * @return * @throws EgovBizException */ @RequestMapping(value = "/editor/fileListView.do") public String fileListView(@ModelAttribute EditorFileVO fileVO, @RequestParam(required = false) String viewType, @RequestParam("authorType") String authorType, Model model, HttpServletRequest request) throws EgovBizException { String fileId = fileVO.getFileId(); List list = new ArrayList<>(); if (StringUtils.isNotBlank(fileId)) { String contextPath = request.getContextPath(); for (String tmp : fileId.split(",")) { EditorFileVO vo = editorService.selectEditorFile(tmp); vo.setLink(contextPath + vo.getLink()); list.add(vo); } } model.addAttribute("resultList", list); model.addAttribute("authorType", authorType); if (viewType != null && viewType.equals("image")) { return "egovframework/com/cmm/fms/EgovImgFileList"; } else { return "egovframework/com/cmm/fms/EgovFileList"; } } /** * 파일 업로드 * * @param request * @return * @throws EgovBizException */ @RequestMapping(value = "/uploadFile.do", produces = "application/json; charset=utf8") @ResponseBody public String uploadFile(HttpServletRequest request) { FileOptions options = new FileOptions(); options.setValidation(null); EditorFileVO fileVO = new EditorFileVO(); AjaxWrapper aw; try { fileVO = File.upload(request, Utils.getAddDateRoute(FILE_ROUTE), options); // DB부분 String fileId = editorService.insertEditorFile(fileVO); fileVO.setFileId(fileId); fileVO.setFullPath(null); fileVO.setUploadDir(null); fileVO.setSysFileNm(null); aw = new AjaxWrapper(AjaxWrapper.SUCCESS, fileVO); } catch (EgovBizException e) { aw = new AjaxWrapper(AjaxWrapper.FAIL, e.getCause().getMessage()); } Gson gson = new Gson(); return gson.toJson(aw); } /** * 첨부파일 목록 정보 * * @param fileVO fileId형식 : EFI_0000~~,EFI_0000~~ * @return * @throws EgovBizException */ @RequestMapping(value = "/fileList/selectJson.do", produces = "application/json; charset=utf8") @ResponseBody public String selectFileListJson(@ModelAttribute EditorFileVO fileVO) { AjaxWrapper aw; String fileId = fileVO.getFileId(); List list = new ArrayList<>(); if (StringUtils.isNotBlank(fileId)) { try { for (String tmp : fileId.split(",")) { EditorFileVO vo = editorService.selectEditorFile(tmp); // 불필요 파일 null 처리 vo.setFullPath(null); vo.setUploadDir(null); vo.setSysFileNm(null); list.add(vo); } aw = new AjaxWrapper(AjaxWrapper.SUCCESS, list); } catch (EgovBizException e) { aw = new AjaxWrapper(AjaxWrapper.FAIL, e.getCause().getMessage()); } } else { aw = new AjaxWrapper(); } Gson gson = new Gson(); return gson.toJson(aw); } /** * 파일 업로드 팝업 * * @Author : 임종호 * @Date : 2021. 8. 11. * @Method Name : uploadFilePopup * @return : String */ @RequestMapping(value = "/uploadFile/popup.do") public String uploadFilePopup(@ModelAttribute EditorFileVO fileVO, Model model) throws EgovBizException { return "ctm/cmm/file/uploadFilePopup"; } }