FAISP/src/main/java/com/dbnt/faisp/config/EditorController.java

127 lines
4.0 KiB
Java
Raw Normal View History

2022-11-14 07:46:11 +00:00
package com.dbnt.faisp.config;
import com.dbnt.faisp.main.menuMgt.service.MenuMgtService;
import org.springframework.core.env.Environment;
import com.dbnt.faisp.main.organMgt.service.OrganConfigService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import com.dbnt.faisp.util.ParamMap;
import com.dbnt.faisp.util.Utils;
import lombok.RequiredArgsConstructor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
2022-11-14 07:46:11 +00:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2022-11-14 07:46:11 +00:00
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
2022-11-14 07:46:11 +00:00
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;
2022-11-14 07:46:11 +00:00
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
2022-11-14 07:46:11 +00:00
@RestController
@RequiredArgsConstructor
public class EditorController {
@Value("${site.domain}")
protected String siteDomain;
2022-11-14 07:46:11 +00:00
@Value("${file.dir}")
protected String fileDir;
@Value("${file.dir.editor}")
protected String editorPath;
2022-11-14 07:46:11 +00:00
@Value("${editor.img.view}")
protected String imgView;
2022-11-14 07:46:11 +00:00
@PostMapping("/Crosseditor/uploadImg")
public @ResponseBody JSONObject uploadImg(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
JSONObject jsonObject = new JSONObject();
JSONObject data = new JSONObject();
JSONArray req_array = new JSONArray();
2022-11-14 07:46:11 +00:00
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
MultipartFile mFile = multipartRequest.getFile("imageFile");
if(!"".equals(mFile.getOriginalFilename())){
String attach_file_Name = mFile.getOriginalFilename();
String attach_save_Name = Utils.generationSaveName();
String imageKind = request.getParameter("imageKind");
String editorFrame = request.getParameter("editorFrame");
2022-11-14 07:46:11 +00:00
//파일 타입
String extNm = "." + attach_file_Name.substring( attach_file_Name.lastIndexOf( "." ) + 1, attach_file_Name.length());
if(".jpg,.png,.jpeg".indexOf(extNm.toLowerCase()) > -1) {
File dir = new File(fileDir+File.separator+editorPath);
2022-11-14 07:46:11 +00:00
if (!dir.exists()) {
try{
// 생성
boolean result2 = dir.mkdir();
if (result2) {
System.out.println("Directory is created.");
} else {
System.out.println("Failed to create directory.");
}
} catch(Exception e){
System.out.println("Exception occurred.");
e.getStackTrace();
}
} else {
System.out.println("Directory already exists");
}
File file = new File(fileDir+File.separator+editorPath, attach_save_Name + extNm);
2022-11-14 07:46:11 +00:00
FileCopyUtils.copy(mFile.getBytes(), file);
String webPath = siteDomain+ imgView + attach_save_Name + extNm;
jsonObject.put("result","success");
data.put("imageURL",webPath);
data.put("imageKind", imageKind);
data.put("editorFrame", editorFrame);
req_array.add(data);
jsonObject.put("addmsg", req_array);
2022-11-14 07:46:11 +00:00
} else {
ParamMap error = new ParamMap();
error.set("message", "Check File Extentions.");
}
} else {
ParamMap error = new ParamMap();
error.set("message", "Check File Extentions.");
2022-11-14 07:46:11 +00:00
}
} catch (Exception e) {
ParamMap error = new ParamMap();
error.set("message", "Check File Extentions.");
}
return jsonObject;
2022-11-14 07:46:11 +00:00
}
}