YozoFileController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package jnpf.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import io.swagger.v3.oas.annotations.tags.Tag;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.Parameters;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import jnpf.constant.MsgCode;
  8. import jnpf.entity.FileEntity;
  9. import jnpf.base.ActionResult;
  10. import jnpf.base.vo.PaginationVO;
  11. import jnpf.config.ConfigValueUtil;
  12. import jnpf.model.FileForm;
  13. import jnpf.model.UploaderVO;
  14. import jnpf.model.YozoFileParams;
  15. import jnpf.model.YozoParams;
  16. import jnpf.service.YozoService;
  17. import jnpf.util.FileUtil;
  18. import jnpf.util.JsonUtil;
  19. import jnpf.util.XSSEscape;
  20. import jnpf.util.wxutil.HttpUtil;
  21. import jnpf.utils.YozoUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.http.MediaType;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.*;
  29. /**
  30. * @author JNPF开发平台组
  31. */
  32. @RestController
  33. @RequestMapping
  34. @Tag(name = "在线文档预览", description = "文件在线预览")
  35. public class YozoFileController {
  36. @Autowired
  37. private YozoService yozoService;
  38. @Autowired
  39. private YozoUtils yozoUtil;
  40. @Autowired
  41. private ConfigValueUtil configValueUtil;
  42. @PostMapping("/api/file/getViewUrlWebPath")
  43. @Operation(summary = "文档预览")
  44. public ActionResult getUrl(YozoFileParams params) {
  45. String previewUrl = XSSEscape.escape(yozoService.getPreviewUrl(params));
  46. return ActionResult.success("success", previewUrl);
  47. }
  48. @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  49. @Operation(summary = "上传本地文件")
  50. public ActionResult upload(@RequestPart("multipartFile") MultipartFile file) throws IOException {
  51. String result = yozoUtil.uploadFileInPreview(file.getInputStream(),file.getOriginalFilename());
  52. String fileName = file.getOriginalFilename();
  53. UploaderVO vo = UploaderVO.builder().name(fileName).build();
  54. Map<String, Object> map = JsonUtil.stringToMap(result);
  55. if ("操作成功".equals(map.get("message"))){
  56. Map<String, Object> dataMap = JsonUtil.stringToMap(String.valueOf(map.get("data")));
  57. String verId = String.valueOf(dataMap.get("fileVersionId"));
  58. vo.setFileVersionId(verId);
  59. return ActionResult.success("Success",vo);
  60. }
  61. return ActionResult.fail(MsgCode.FA033.get());
  62. }
  63. /**
  64. *
  65. * @param fileName 新建文件名
  66. * @param templateType (模板类型;1新建doc文档,2新建docx文档,3新建ppt文档,4新建pptx文档,5新建xls文档,6新建xlsx文档)
  67. * @return
  68. */
  69. @GetMapping("/newCreate")
  70. @Operation(summary = "新建文件")
  71. @Parameters({
  72. @Parameter(name = "fileName", description = "名称"),
  73. @Parameter(name = "templateType", description = "类型"),
  74. })
  75. public ActionResult newCreate(@RequestParam("fileName") String fileName, @RequestParam("templateType") String templateType) {
  76. String fileNa = yozoUtil.getFileName(fileName, templateType);
  77. if (fileNa == null) {
  78. return ActionResult.fail(MsgCode.FA042.get());
  79. }
  80. //判断文件是否创建过
  81. FileEntity fileEntity = yozoService.selectByName(fileNa);
  82. if (fileEntity != null) {
  83. return ActionResult.fail(MsgCode.FA043.get());
  84. }
  85. Map<String, String[]> params = new HashMap<String, String[]>();
  86. params.put("templateType", new String[]{templateType});
  87. params.put("fileName", new String[]{fileName});
  88. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  89. String url = YozoParams.CLOUD_DOMAIN + "/api/file/template?templateType=" + templateType +
  90. "&fileName=" + fileName +
  91. "&appId=" + YozoParams.APP_ID +
  92. "&sign=" + sign;
  93. String s = HttpUtil.sendHttpPost(url);
  94. Map<String, Object> maps = JSONObject.parseObject(s, Map.class);
  95. Map<String, String> fileMap = (Map<String, String>) maps.get("data");
  96. String fileVersionId = fileMap.get("fileVersionId");
  97. String fileId = fileMap.get("fileId");
  98. ActionResult back = yozoService.saveFileId(fileVersionId, fileId, fileNa);
  99. //在本地新建文件
  100. FileUtil.createFile(configValueUtil.getDocumentPreviewPath(), fileNa);
  101. return back;
  102. }
  103. @GetMapping("/uploadByHttp")
  104. @Operation(summary = "http上传文件")
  105. @Parameters({
  106. @Parameter(name = "fileUrl", description = "路径"),
  107. })
  108. public ActionResult uploadByHttp(@RequestParam("fileUrl") String fileUrl) {
  109. //获取签名
  110. Map<String, String[]> params = new HashMap<String, String[]>();
  111. params.put("fileUrl", new String[]{fileUrl});
  112. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  113. String url = YozoParams.CLOUD_DOMAIN + "/api/file/http?fileUrl=" + fileUrl +
  114. "&appId=" + YozoParams.APP_ID +
  115. "&sign=" + sign;
  116. String s = HttpUtil.sendHttpPost(url);
  117. Map<String, Object> maps = JSONObject.parseObject(s, Map.class);
  118. Map<String, String> fileMap = (Map<String, String>) maps.get("data");
  119. String fileVersionId = fileMap.get("fileVersionId");
  120. String fileId = fileMap.get("fileId");
  121. ActionResult back = yozoService.saveFileIdByHttp(fileVersionId, fileId, fileUrl);
  122. return back;
  123. }
  124. @GetMapping("/downloadFile")
  125. @Operation(summary = "永中下载文件")
  126. @Parameters({
  127. @Parameter(name = "fileVersionId", description = "主键"),
  128. })
  129. public String downloadFile(@RequestParam("fileVersionId") String fileVersionId) {
  130. String newFileVersionId = XSSEscape.escape(fileVersionId);
  131. FileEntity fileEntity = yozoService.selectByVersionId(newFileVersionId);
  132. if (fileEntity == null) {
  133. return MsgCode.FA044.get();
  134. }
  135. //获取签名
  136. Map<String, String[]> params = new HashMap<String, String[]>();
  137. params.put("fileVersionId", new String[]{newFileVersionId});
  138. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  139. String url = YozoParams.CLOUD_DOMAIN + "/api/file/download?fileVersionId=" + newFileVersionId +
  140. "&appId=" + YozoParams.APP_ID +
  141. "&sign=" + sign;
  142. return url;
  143. }
  144. @GetMapping("/deleteVersionFile")
  145. @Operation(summary = "删除文件版本")
  146. @Parameters({
  147. @Parameter(name = "fileVersionId", description = "主键"),
  148. })
  149. public ActionResult deleteVersion(@RequestParam("fileVersionId") String fileVersionId) {
  150. //获取签名
  151. Map<String, String[]> params = new HashMap<String, String[]>();
  152. params.put("fileVersionId", new String[]{fileVersionId});
  153. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  154. String url = YozoParams.CLOUD_DOMAIN + "/api/file/delete/version?fileVersionId=" + fileVersionId +
  155. "&appId=" + YozoParams.APP_ID +
  156. "&sign=" + sign;
  157. String s = HttpUtil.sendHttpGet(url);
  158. Map<String, Object> maps = JSONObject.parseObject(s, Map.class);
  159. String fileName = yozoService.selectByVersionId(fileVersionId).getFileName();
  160. String path = configValueUtil.getDocumentPreviewPath() + fileName;
  161. if (FileUtil.fileIsFile(path)) {
  162. File file = new File(XSSEscape.escapePath(path));
  163. file.delete();
  164. }
  165. String versionId = (String) maps.get("data");
  166. ActionResult back = yozoService.deleteFileByVersionId(versionId);
  167. return back;
  168. }
  169. @GetMapping("/batchDelete")
  170. @Operation(summary = "批量删除文件版本")
  171. @Parameters({
  172. @Parameter(name = "fileVersionIds", description = "主键"),
  173. })
  174. public ActionResult batchDelete(@RequestParam("fileVersionIds") String[] fileVersionIds) {
  175. List<String> asList = new ArrayList<>(16);
  176. //获取签名
  177. for (String fileVersionId : fileVersionIds) {
  178. String escape = XSSEscape.escape(fileVersionId);
  179. asList.add(escape);
  180. }
  181. String[] newFileVersionIds = asList.toArray(fileVersionIds);
  182. Map<String, String[]> params = new HashMap<>();
  183. params.put("fileVersionIds", newFileVersionIds);
  184. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  185. StringBuilder fileVersionIdList = new StringBuilder();
  186. for (String s : newFileVersionIds) {
  187. String fileName = yozoService.selectByVersionId(s).getFileName();
  188. String path = configValueUtil.getDocumentPreviewPath() + fileName;
  189. File file = new File(XSSEscape.escapePath(path));
  190. file.delete();
  191. fileVersionIdList.append("fileVersionIds=" + s + "&");
  192. }
  193. String list = fileVersionIdList.toString();
  194. String url = YozoParams.CLOUD_DOMAIN + "/api/file/delete/versions?" + list +
  195. "appId=" + YozoParams.APP_ID +
  196. "&sign=" + sign;
  197. String s = HttpUtil.sendHttpGet(url);
  198. ActionResult back = yozoService.deleteBatch(newFileVersionIds);
  199. return back;
  200. }
  201. @GetMapping("/editFile")
  202. @Operation(summary = "在线编辑")
  203. @Parameters({
  204. @Parameter(name = "fileVersionId", description = "主键"),
  205. })
  206. public ActionResult editFile(@RequestParam("fileVersionId") String fileVersionId) {
  207. String newFileVersionId = XSSEscape.escape(fileVersionId);
  208. //获取签名
  209. Map<String, String[]> params = new HashMap<>();
  210. params.put("fileVersionId", new String[]{newFileVersionId});
  211. String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
  212. String url = YozoParams.EDIT_DOMAIN + "/api/edit/file?fileVersionId=" + newFileVersionId +
  213. "&appId=" + YozoParams.APP_ID +
  214. "&sign=" + sign;
  215. return ActionResult.success("success", url);
  216. }
  217. /**
  218. * 永中回调
  219. *
  220. * @param oldFileId
  221. * @param newFileId
  222. * @param message
  223. * @param errorCode
  224. * @return
  225. */
  226. @PostMapping("/3rd/edit/callBack")
  227. @Parameters({
  228. @Parameter(name = "oldFileId", description = "主键"),
  229. @Parameter(name = "newFileId", description = "主键"),
  230. @Parameter(name = "message", description = "消息"),
  231. @Parameter(name = "errorCode", description = "编码"),
  232. })
  233. public Map<String, Object> editCallBack(@RequestParam("oldFileId") String oldFileId, @RequestParam("newFileId") String newFileId, @RequestParam("message") String message, @RequestParam("errorCode") Integer errorCode) {
  234. String escapeOldFileId = XSSEscape.escape(oldFileId);
  235. String escapeNewFileId = XSSEscape.escape(newFileId);
  236. String escapeMessage = XSSEscape.escape(message);
  237. yozoService.editFileVersion(escapeOldFileId, escapeNewFileId);
  238. Map<String, Object> result = new HashMap<>();
  239. result.put("oldFileId", escapeOldFileId);
  240. result.put("newFileId", escapeNewFileId);
  241. result.put("message", escapeMessage);
  242. result.put("errorCode", errorCode);
  243. return result;
  244. }
  245. @PostMapping("/documentList")
  246. @Operation(summary = "文档列表")
  247. @Parameters({
  248. @Parameter(name = "pageModel", description = "分页模型", required = true),
  249. })
  250. public ActionResult documentList(@RequestBody PaginationVO pageModel) {
  251. PaginationVO pv = new PaginationVO();
  252. pv.setCurrentPage(pageModel.getCurrentPage());
  253. pv.setPageSize(pageModel.getPageSize());
  254. pv.setTotal(pageModel.getTotal());
  255. List<FileEntity> list = yozoService.getAllList(pv);
  256. List<FileForm> listVo = JsonUtil.getJsonToList(list, FileForm.class);
  257. return ActionResult.page(listVo, pv);
  258. }
  259. /**
  260. * 传入新的fileVersionId同步
  261. *
  262. * @param fileVersionId
  263. * @return
  264. * @throws Exception
  265. */
  266. @GetMapping("/updateFile")
  267. @Operation(summary = "/同步文件版本到本地")
  268. @Parameters({
  269. @Parameter(name = "fileVersionId", description = "主键"),
  270. })
  271. public ActionResult updateFile(@RequestParam("fileVersionId") String fileVersionId) throws Exception {
  272. FileEntity fileEntity = yozoService.selectByVersionId(fileVersionId);
  273. String fileName = fileEntity.getFileName();
  274. String path = configValueUtil.getDocumentPreviewPath() + fileName;
  275. if (FileUtil.fileIsFile(path)) {
  276. File file = new File(XSSEscape.escapePath(path));
  277. file.delete();
  278. }
  279. String fileUrl = this.downloadFile(fileVersionId);
  280. yozoUtil.downloadFile(fileUrl, path);
  281. return ActionResult.success(MsgCode.SU004.get());
  282. }
  283. }