123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package com.fd.controller;
- import com.fd.constant.Command;
- import com.fd.constant.MsgCode;
- import com.fd.constant.TypeCode;
- import com.fd.dto.ConfigJsonDto;
- import com.fd.dto.PageDto;
- import com.fd.entity.FileEntity;
- import com.fd.entity.OutputFileEntity;
- import com.fd.server.ModelServer;
- import com.fd.thread.AsyncTask;
- import com.fd.util.FileUtils;
- import com.fd.util.R;
- import com.fd.util.RegexUtils;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.log4j.Log4j2;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.shiro.authz.annotation.RequiresAuthentication;
- import org.apache.shiro.authz.annotation.RequiresRoles;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import springfox.documentation.annotations.ApiIgnore;
- import javax.servlet.http.HttpServletRequest;
- import java.io.File;
- import java.util.Date;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * Created by Owen on 2019/11/12 0012 9:40
- * <p>
- * 3D模型数据
- */
- @Api(tags = "模型模块")
- @Log4j2
- @RequestMapping("api/fdModel")
- @RestController
- public class ModelController {
- @Value("${input.file.path.model}")
- private String INPUT_FILE_PATH;
- @Value("${output.file.path.model}")
- private String OUTPUT_FILE_PATH;
- @Autowired
- private AsyncTask asyncTask;
- @Autowired
- private ModelServer modelServer;
- /**
- * 队列
- */
- private static BlockingQueue<Integer> modelQueue = new LinkedBlockingQueue<Integer>(2);
- /**
- * 保证线程安全的
- */
- private static AtomicInteger count = new AtomicInteger();
- @ApiOperation("上传数据,校验文件名")
- @GetMapping("check/{fileName}/")
- @ApiImplicitParam(name = "fileName", value = "文件名", required = true)
- public R checkFileName(@PathVariable("fileName") String fileName) {
- log.info("run checkFileName {}", fileName);
- // 文件是否包含中文字符
- // if (RegexUtils.regexChinese(fileName)) {
- // return new R(51005, MsgCode.E51005);
- // }
- String s = StringUtils.substringAfterLast(fileName, ".");
- if (!"zip".equals(s)) {
- return new R(50007, MsgCode.E50007);
- }
- List<FileEntity> list = modelServer.findByFileName(fileName);
- if (list.size() > 0) {
- return new R(51006, MsgCode.E51006);
- }
- return new R(200, MsgCode.SUCCESS);
- }
- @ApiOperation("上传3D模型数据")
- @PostMapping(value = "upload", consumes = {"multipart/form-data"})
- @ApiImplicitParam(name = "file", value = "只能上传zip文件", required = true)
- public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
- log.info("run upload");
- String token = req.getHeader("Authorization");
- // 文件名全名
- String fileName = file.getOriginalFilename();
- // 文件是否包含中文字符
- // if (RegexUtils.regexChinese(fileName)) {
- // return new R(51005, MsgCode.E51005);
- // }
- String s = StringUtils.substringAfterLast(fileName, ".");
- if (!"zip".equals(s)) {
- return new R(50007, MsgCode.E50007);
- }
- List<FileEntity> list = modelServer.findByFileName(fileName);
- if (list.size() > 0) {
- return new R(51006, MsgCode.E51006);
- }
- return modelServer.uploadBigFile(file, token);
- }
- @ApiOperation("解压zip文件")
- @GetMapping("unzip/{fileId}/")
- @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
- public R fileUnzip(@PathVariable("fileId") Long fileId) {
- log.info("run fileUnzip: {}", fileId);
- OutputFileEntity entity = modelServer.findById(fileId);
- String outputPath = OUTPUT_FILE_PATH + "unzip";
- FileUtils.createDir(outputPath);
- boolean unzip = FileUtils.unzip(entity.getUploadPath(), outputPath);
- if (!unzip) {
- log.info("zip error: {}", MsgCode.E51001);
- return new R(50001, MsgCode.E51001);
- }
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- entity.setStatus(4);
- entity.setUpdateTime(new Date());
- entity.setUnZipPath(outputPath + File.separator + fileName);
- entity = modelServer.save(entity);
- return new R(200, entity);
- }
- @ApiOperation("获取3D模型数据列表")
- @PostMapping(value = "list")
- public R list(@RequestBody PageDto param, HttpServletRequest req) {
- String token = req.getHeader("Authorization");
- Page<OutputFileEntity> page = modelServer.findByList(TypeCode.FILE_TYPE_MODEL, param, token);
- return new R(200, page);
- }
- @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
- @ApiOperation("删除文件")
- @GetMapping("delete/{fileId}/")
- public R deleteFile(@PathVariable("fileId") Long fileId) {
- log.info("run deleteFile: {}", fileId);
- OutputFileEntity entity = modelServer.findById(fileId);
- entity.setResStatus(1);
- modelServer.save(entity);
- FileEntity fileEntity = modelServer.findByInputFileId(entity.getUploadId());
- fileEntity.setResStatus(1);
- modelServer.saveInputFile(fileEntity);
- asyncTask.modelDelete(fileId, modelServer);
- return new R(200, MsgCode.SUCCESS);
- }
- @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
- @ApiOperation("倾斜摄影数据切片")
- @GetMapping("command/osgb/{fileId}/")
- public R cmdModelSlice(@PathVariable("fileId") Long fileId) {
- log.info("run cmdModelSlice: {}", fileId);
- OutputFileEntity entity = modelServer.findById(fileId);
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- String outputPath = OUTPUT_FILE_PATH + "slice";
- FileUtils.createDir(outputPath);
- outputPath = outputPath + File.separator + fileName;
- // 传入的是目录
- String cmd = Command.MODEL_SLICE_OSGB;
- cmd = cmd.replace("@inputFile", entity.getUnZipPath());
- cmd = cmd.replace("@outputFile", outputPath);
- log.info("cmd: {}", cmd);
- // 把数据放入队列中
- boolean offer = false;
- try {
- offer = modelQueue.offer(count.incrementAndGet(), 1, TimeUnit.SECONDS);
- log.info("model slice 入队成功");
- } catch (Exception e) {
- log.error("error producer queue model cmdModelSlice: {}", e);
- e.printStackTrace();
- }
- log.info("end cmdModelSlice");
- if (offer) {
- // 命令产生的是文件夹
- entity.setStatus(6);
- entity.setUpdateTime(new Date());
- entity.setSlicePath(outputPath);
- entity = modelServer.save(entity);
- asyncTask.modelSlice(modelQueue, modelServer, entity, cmd);
- return new R(200, entity);
- }
- // 入队失败
- return new R(52000, MsgCode.E52000);
- }
- @ApiImplicitParam(name = "fileId", value = "文件id", required = true)
- @RequiresRoles("admin")
- @ApiOperation("移动数据到服务器上")
- @PostMapping("move/{fileId}/")
- public R moveFile(@PathVariable("fileId") Long fileId, @RequestBody ConfigJsonDto param) {
- log.info("run moveFile: {}", fileId);
- return modelServer.moveFileToServer(fileId, param);
- }
- @ApiIgnore
- @RequiresRoles("admin")
- @ApiOperation("测试")
- @PostMapping("test")
- public R test() {
- log.info("run test ");
- return new R(2000, "admin 权限可以查看 model");
- }
- }
|