ModelController.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.fd.controller;
  2. import com.fd.constant.Command;
  3. import com.fd.constant.MsgCode;
  4. import com.fd.constant.TypeCode;
  5. import com.fd.dto.ConfigJsonDto;
  6. import com.fd.dto.PageDto;
  7. import com.fd.entity.FileEntity;
  8. import com.fd.entity.OutputFileEntity;
  9. import com.fd.server.ModelServer;
  10. import com.fd.thread.AsyncTask;
  11. import com.fd.util.FileUtils;
  12. import com.fd.util.R;
  13. import com.fd.util.RegexUtils;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiImplicitParam;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.extern.log4j.Log4j2;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.shiro.authz.annotation.RequiresAuthentication;
  20. import org.apache.shiro.authz.annotation.RequiresRoles;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Value;
  23. import org.springframework.data.domain.Page;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import springfox.documentation.annotations.ApiIgnore;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.io.File;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.concurrent.BlockingQueue;
  32. import java.util.concurrent.LinkedBlockingQueue;
  33. import java.util.concurrent.TimeUnit;
  34. import java.util.concurrent.atomic.AtomicInteger;
  35. /**
  36. * Created by Owen on 2019/11/12 0012 9:40
  37. * <p>
  38. * 3D模型数据
  39. */
  40. @Api(tags = "模型模块")
  41. @Log4j2
  42. @RequestMapping("api/fdModel")
  43. @RestController
  44. public class ModelController {
  45. @Value("${input.file.path.model}")
  46. private String INPUT_FILE_PATH;
  47. @Value("${output.file.path.model}")
  48. private String OUTPUT_FILE_PATH;
  49. @Autowired
  50. private AsyncTask asyncTask;
  51. @Autowired
  52. private ModelServer modelServer;
  53. /**
  54. * 队列
  55. */
  56. private static BlockingQueue<Integer> modelQueue = new LinkedBlockingQueue<Integer>(2);
  57. /**
  58. * 保证线程安全的
  59. */
  60. private static AtomicInteger count = new AtomicInteger();
  61. @ApiOperation("上传数据,校验文件名")
  62. @GetMapping("check/{fileName}/")
  63. @ApiImplicitParam(name = "fileName", value = "文件名", required = true)
  64. public R checkFileName(@PathVariable("fileName") String fileName) {
  65. log.info("run checkFileName {}", fileName);
  66. // 文件是否包含中文字符
  67. // if (RegexUtils.regexChinese(fileName)) {
  68. // return new R(51005, MsgCode.E51005);
  69. // }
  70. String s = StringUtils.substringAfterLast(fileName, ".");
  71. if (!"zip".equals(s)) {
  72. return new R(50007, MsgCode.E50007);
  73. }
  74. List<FileEntity> list = modelServer.findByFileName(fileName);
  75. if (list.size() > 0) {
  76. return new R(51006, MsgCode.E51006);
  77. }
  78. return new R(200, MsgCode.SUCCESS);
  79. }
  80. @ApiOperation("上传3D模型数据")
  81. @PostMapping(value = "upload", consumes = {"multipart/form-data"})
  82. @ApiImplicitParam(name = "file", value = "只能上传zip文件", required = true)
  83. public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
  84. log.info("run upload");
  85. String token = req.getHeader("Authorization");
  86. // 文件名全名
  87. String fileName = file.getOriginalFilename();
  88. // 文件是否包含中文字符
  89. // if (RegexUtils.regexChinese(fileName)) {
  90. // return new R(51005, MsgCode.E51005);
  91. // }
  92. String s = StringUtils.substringAfterLast(fileName, ".");
  93. if (!"zip".equals(s)) {
  94. return new R(50007, MsgCode.E50007);
  95. }
  96. List<FileEntity> list = modelServer.findByFileName(fileName);
  97. if (list.size() > 0) {
  98. return new R(51006, MsgCode.E51006);
  99. }
  100. return modelServer.uploadBigFile(file, token);
  101. }
  102. @ApiOperation("解压zip文件")
  103. @GetMapping("unzip/{fileId}/")
  104. @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
  105. public R fileUnzip(@PathVariable("fileId") Long fileId) {
  106. log.info("run fileUnzip: {}", fileId);
  107. OutputFileEntity entity = modelServer.findById(fileId);
  108. String outputPath = OUTPUT_FILE_PATH + "unzip";
  109. FileUtils.createDir(outputPath);
  110. boolean unzip = FileUtils.unzip(entity.getUploadPath(), outputPath);
  111. if (!unzip) {
  112. log.info("zip error: {}", MsgCode.E51001);
  113. return new R(50001, MsgCode.E51001);
  114. }
  115. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  116. entity.setStatus(4);
  117. entity.setUpdateTime(new Date());
  118. entity.setUnZipPath(outputPath + File.separator + fileName);
  119. entity = modelServer.save(entity);
  120. return new R(200, entity);
  121. }
  122. @ApiOperation("获取3D模型数据列表")
  123. @PostMapping(value = "list")
  124. public R list(@RequestBody PageDto param, HttpServletRequest req) {
  125. String token = req.getHeader("Authorization");
  126. Page<OutputFileEntity> page = modelServer.findByList(TypeCode.FILE_TYPE_MODEL, param, token);
  127. return new R(200, page);
  128. }
  129. @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
  130. @ApiOperation("删除文件")
  131. @GetMapping("delete/{fileId}/")
  132. public R deleteFile(@PathVariable("fileId") Long fileId) {
  133. log.info("run deleteFile: {}", fileId);
  134. OutputFileEntity entity = modelServer.findById(fileId);
  135. entity.setResStatus(1);
  136. modelServer.save(entity);
  137. FileEntity fileEntity = modelServer.findByInputFileId(entity.getUploadId());
  138. fileEntity.setResStatus(1);
  139. modelServer.saveInputFile(fileEntity);
  140. asyncTask.modelDelete(fileId, modelServer);
  141. return new R(200, MsgCode.SUCCESS);
  142. }
  143. @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
  144. @ApiOperation("倾斜摄影数据切片")
  145. @GetMapping("command/osgb/{fileId}/")
  146. public R cmdModelSlice(@PathVariable("fileId") Long fileId) {
  147. log.info("run cmdModelSlice: {}", fileId);
  148. OutputFileEntity entity = modelServer.findById(fileId);
  149. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  150. String outputPath = OUTPUT_FILE_PATH + "slice";
  151. FileUtils.createDir(outputPath);
  152. outputPath = outputPath + File.separator + fileName;
  153. // 传入的是目录
  154. String cmd = Command.MODEL_SLICE_OSGB;
  155. cmd = cmd.replace("@inputFile", entity.getUnZipPath());
  156. cmd = cmd.replace("@outputFile", outputPath);
  157. log.info("cmd: {}", cmd);
  158. // 把数据放入队列中
  159. boolean offer = false;
  160. try {
  161. offer = modelQueue.offer(count.incrementAndGet(), 1, TimeUnit.SECONDS);
  162. log.info("model slice 入队成功");
  163. } catch (Exception e) {
  164. log.error("error producer queue model cmdModelSlice: {}", e);
  165. e.printStackTrace();
  166. }
  167. log.info("end cmdModelSlice");
  168. if (offer) {
  169. // 命令产生的是文件夹
  170. entity.setStatus(6);
  171. entity.setUpdateTime(new Date());
  172. entity.setSlicePath(outputPath);
  173. entity = modelServer.save(entity);
  174. asyncTask.modelSlice(modelQueue, modelServer, entity, cmd);
  175. return new R(200, entity);
  176. }
  177. // 入队失败
  178. return new R(52000, MsgCode.E52000);
  179. }
  180. @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
  181. @RequiresRoles("admin")
  182. @ApiOperation("移动数据到服务器上")
  183. @PostMapping("move/{fileId}/")
  184. public R moveFile(@PathVariable("fileId") Long fileId, @RequestBody ConfigJsonDto param) {
  185. log.info("run moveFile: {}", fileId);
  186. return modelServer.moveFileToServer(fileId, param);
  187. }
  188. @ApiIgnore
  189. @RequiresRoles("admin")
  190. @ApiOperation("测试")
  191. @PostMapping("test")
  192. public R test() {
  193. log.info("run test ");
  194. return new R(2000, "admin 权限可以查看 model");
  195. }
  196. }