|
@@ -0,0 +1,122 @@
|
|
|
+package com.hb.proj.sys.controller;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import com.hb.proj.allconfig.AccessToken;
|
|
|
+import com.hb.proj.model.FileInfoPO;
|
|
|
+import com.hb.proj.model.UploadResult;
|
|
|
+import com.hb.proj.sys.service.UploadService;
|
|
|
+import com.hb.proj.utils.RespVO;
|
|
|
+import com.hb.proj.utils.RespVOBuilder;
|
|
|
+import com.hb.proj.utils.UploadUtils;
|
|
|
+import com.hb.xframework.util.DateUtil;
|
|
|
+import com.hb.xframework.util.MapUtils;
|
|
|
+
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import jakarta.validation.constraints.NotBlank;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/upload")
|
|
|
+@Validated
|
|
|
+public class UploadController {
|
|
|
+
|
|
|
+
|
|
|
+ private String uploadAccessPath="";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UploadService service;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只进行文件的磁盘存储
|
|
|
+ * @param request
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping("/save")
|
|
|
+ public RespVO<Object> save(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{
|
|
|
+ String subDirectory=request.getParameter("sort");
|
|
|
+ if(StringUtils.isBlank(subDirectory)) {
|
|
|
+ subDirectory="other";
|
|
|
+ }
|
|
|
+ String needDate=request.getParameter("needDate");
|
|
|
+ if(StringUtils.isBlank(needDate)||"1".equals(needDate)) {
|
|
|
+ subDirectory+="/"+DateUtil.format(new Date(), "yyyyMM");
|
|
|
+ }
|
|
|
+ UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
|
|
|
+ if(!rst.isSuccess()) {
|
|
|
+ return RespVOBuilder.error(rst.getException());
|
|
|
+ }
|
|
|
+
|
|
|
+ return RespVOBuilder.ok(rst);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 富文本编辑器上传文件
|
|
|
+ * @param request
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping("/forEditor")
|
|
|
+ public Object forEditor(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{
|
|
|
+ String subDirectory="subject/"+DateUtil.format(new Date(), "yyyyMM");
|
|
|
+ UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
|
|
|
+ if(!rst.isSuccess()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return MapUtils.build("location",rst.getAccessPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/add")
|
|
|
+ public RespVO<Object> addFile(HttpServletRequest request,AccessToken token,@RequestParam("file") MultipartFile file) throws Exception{
|
|
|
+ String subDirectory=request.getParameter("sort");
|
|
|
+ if(StringUtils.isBlank(subDirectory)) {
|
|
|
+ subDirectory="other";
|
|
|
+ }
|
|
|
+ String needDate=request.getParameter("needDate");
|
|
|
+ if(StringUtils.isBlank(needDate)||"1".equals(needDate)) {
|
|
|
+ subDirectory+="/"+DateUtil.format(new Date(), "yyyyMM");
|
|
|
+ }
|
|
|
+ UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
|
|
|
+ if(!rst.isSuccess()) {
|
|
|
+ return RespVOBuilder.error(rst.getException());
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String,Object> rstMap=new HashMap<String,Object>();
|
|
|
+ rstMap.put("useFor", request.getParameter("useFor"));
|
|
|
+ rstMap.put("createBy", token!=null?token.getUsName():"unknow");
|
|
|
+ rstMap.put("fileName",rst.getFileName());
|
|
|
+ rstMap.put("filePath",rst.getFilePath());
|
|
|
+ rstMap.put("accessPath",rst.getAccessPath());
|
|
|
+ service.addFile(rstMap);
|
|
|
+
|
|
|
+ return RespVOBuilder.ok(rstMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/del")
|
|
|
+ public RespVO<Object> delFile(@NotBlank(message = "文件编号不能为空") String fileId,HttpServletRequest request) {
|
|
|
+ FileInfoPO fileInfo=service.getFile(fileId);
|
|
|
+ if(fileInfo==null) {
|
|
|
+ return RespVOBuilder.ok();
|
|
|
+ }
|
|
|
+ UploadUtils.removeFile(request, fileInfo.getFilePath());
|
|
|
+
|
|
|
+ service.delFile(fileId);
|
|
|
+ return RespVOBuilder.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|