Ver Fonte

Initial release

gagamel há 5 anos atrás
pai
commit
d15b9fde83

+ 174 - 0
src/main/java/com/style24/front/biz/web/TsfCommonController.java

@@ -0,0 +1,174 @@
+package com.style24.front.biz.web;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.style24.front.support.controller.TsfBaseController;
+
+import lombok.extern.slf4j.Slf4j;
+
+import com.gagaframework.web.util.GagaFileUploadUtil;
+import com.gagaframework.web.util.GagaFileUtil;
+import com.gagaframework.web.util.GagaUploadedFileInfo;
+
+/**
+ * 공통 Controller
+ *
+ * @author gagamel
+ * @since 2021. 2. 19
+ */
+@Controller
+@RequestMapping("/common")
+@Slf4j
+public class TsfCommonController extends TsfBaseController {
+
+	@Autowired
+	private Environment env;
+
+	@Value("${upload.default.target.path}")
+	private String uploadTargetPath;
+
+	/**
+	 * Download a file
+	 *
+	 * @param request - HttpServletRequest
+	 * @param downloadFile - 다운로드할 파일
+	 * @return
+	 * @throws IOException
+	 * @author gagamel
+	 * @since 2019. 12. 6
+	 */
+	@GetMapping("/file/download")
+	@ResponseBody
+	public ResponseEntity<InputStreamResource> downloadFile(HttpServletRequest request, @RequestParam("downloadFile") String downloadFile) throws IOException {
+		String fileName = StringUtils.replace(downloadFile, "../", "");
+		log.debug("fileName: {}", fileName);
+
+		String downloadPath = env.getProperty("download.path");
+		log.debug("downLoadPath :{}", downloadPath);
+
+		String fileNameWithPath = GagaFileUtil.getConcatenationPath(downloadPath, fileName);
+		log.debug("fileNameWithPath: {}", fileNameWithPath);
+
+		return GagaFileUtil.writeFile(request, fileNameWithPath);
+	}
+
+	/**
+	 * Upload a file
+	 *
+	 * @param subDir - 업로드 하위 디렉토리
+	 * @param policy - 업로드 정책
+	 * @param file - MultipartFile
+	 * @return 업로드한 파일의 정보
+	 * @throws IOException
+	 * @author gagamel
+	 * @since 2019. 12. 6
+	 */
+	@PostMapping("/file/upload")
+	@ResponseBody
+	public GagaUploadedFileInfo uploadFile(@RequestParam(value = "subDir") String subDir, @RequestParam(value = "policy", required = false) String policy, MultipartFile file) throws IOException {
+		if (StringUtils.isEmpty(policy)) {
+			policy = "default";
+		}
+
+		String targetPath = GagaFileUtil.getConcatenationPath(env.getProperty("upload." + policy + ".target.path"), subDir);
+
+		GagaFileUploadUtil fuUtil = new GagaFileUploadUtil(targetPath, Long.parseLong(env.getProperty("upload." + policy + ".max.size")), env.getProperty("upload." + policy + ".allow.extension"), env.getProperty("upload." + policy + ".view"));
+
+		GagaUploadedFileInfo ufInfo = fuUtil.uploadFile(file);
+//		if (!"excel".equals(policy)) {
+//			// 운영서버에서만 FTP로 파일 업로드
+//			String profiles = env.getProperty("spring.profiles.active").toLowerCase();
+//			if ("run".equals(profiles) || "locp".equals(profiles)) {
+//				GagaFtpUtil ftpUtil = new GagaFtpUtil(env.getProperty("speedy.ftp.host"), env.getProperty("speedy.ftp.port"), env.getProperty("speedy.ftp.username"), env.getProperty("speedy.ftp.pwd"));
+//
+//				File srcFile = new File(GagaFileUtil.getConcatenationPath(targetPath, ufInfo.getNewFileName()));
+//				ftpUtil.upload(srcFile, "/" + subDir);
+//				ftpUtil.close();
+//			}
+//		}
+		return ufInfo;
+	}
+
+	/**
+	 * Delete a file
+	 *
+	 * @param subDir - 업로드 하위 디렉토리
+	 * @param fileNm - 파일명
+	 * @return
+	 * @throws IOException
+	 * @author sasa004
+	 * @since 2020. 04. 06
+	 */
+	@PostMapping("/file/delete")
+	@ResponseBody
+	public void deleteFile(@RequestParam(value = "subDir") String subDir, @RequestParam(value = "fileNm", required = false) String fileNm) throws IOException {
+		String targetPath = GagaFileUtil.getConcatenationPath(env.getProperty("upload.default.target.path"), subDir);
+
+		GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(targetPath, fileNm));
+
+//		// 운영서버이면 FTP 파일 삭제
+//		String profiles = env.getProperty("spring.profiles.active").toLowerCase();
+//		if ("run".equals(profiles) || "locp".equals(profiles)) {
+//			GagaFtpUtil ftpUtil = new GagaFtpUtil(env.getProperty("speedy.ftp.host"), env.getProperty("speedy.ftp.port"), env.getProperty("speedy.ftp.username"), env.getProperty("speedy.ftp.pwd"));
+//			log.debug("targetPath : " + targetPath);
+//			ftpUtil.delete("/" + subDir, fileNm);
+//			ftpUtil.close();
+//		}
+	}
+
+	/**
+	 * Upload files
+	 *
+	 * @param subDir - 업로드 하위 디렉토리
+	 * @param files - MultipartFile List
+	 * @return 업로드한 파일의 정보
+	 * @throws IOException
+	 * @author gagamel
+	 * @since 2019. 12. 6
+	 */
+	@PostMapping("/files/upload")
+	@ResponseBody
+	public Collection<GagaUploadedFileInfo> uploadFiles(@RequestParam(value = "subDir") String subDir, List<MultipartFile> files) throws IOException {
+		String targetPath = GagaFileUtil.getConcatenationPath(env.getProperty("upload.default.target.path"), subDir);
+		GagaFileUploadUtil fuUtil = new GagaFileUploadUtil(targetPath);
+
+		Collection<GagaUploadedFileInfo> ufList = fuUtil.uploadFiles(files);
+		log.debug("ufList: {}", ufList);
+
+//		// 운영서버에서만 FTP로 파일 업로드
+//		String profiles = env.getProperty("spring.profiles.active").toLowerCase();
+//		if ("run".equals(profiles) || "locp".equals(profiles)) {
+//			if (ufList != null && !ufList.isEmpty()) {
+//				GagaFtpUtil ftpUtil = new GagaFtpUtil(env.getProperty("speedy.ftp.host"), env.getProperty("speedy.ftp.port"), env.getProperty("speedy.ftp.username"), env.getProperty("speedy.ftp.pwd"));
+//
+//				for (GagaUploadedFileInfo ufInfo : ufList) {
+//					File srcFile = new File(GagaFileUtil.getConcatenationPath(targetPath, ufInfo.getNewFileName()));
+//					ftpUtil.upload(srcFile, "/" + subDir);
+//				}
+//
+//				ftpUtil.close();
+//			}
+//		}
+
+		return ufList;
+	}
+
+}