UploadController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.fdkankan.manage_jp.controller;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  5. import com.fdkankan.manage_jp.common.Constant;
  6. import com.fdkankan.manage_jp.common.Result;
  7. import com.fdkankan.manage_jp.common.ResultCode;
  8. import com.fdkankan.manage_jp.config.FyunConfig;
  9. import com.fdkankan.manage_jp.exception.BusinessException;
  10. import com.fdkankan.manage_jp.httpClient.client.FdKKClient;
  11. import com.fdkankan.manage_jp.httpClient.param.UploadEditSceneParam;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.util.HashMap;
  24. import java.util.UUID;
  25. @RestController
  26. @RequestMapping("/manage_jp/file")
  27. @Slf4j
  28. public class UploadController extends BaseController{
  29. @Autowired
  30. FYunFileServiceInterface fYunFileServiceInterface;
  31. @PostMapping("/uploadImg")
  32. public Result uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
  33. Long date = System.currentTimeMillis();
  34. String fileName = date + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
  35. String filePath = Constant.AGENT_PATH + "company" + File.separator + fileName;
  36. File targetFile = new File(filePath);
  37. if(!targetFile.getParentFile().exists()){
  38. targetFile.getParentFile().mkdirs();
  39. }
  40. file.transferTo(targetFile);
  41. String url = fYunFileServiceInterface.uploadFile(filePath,"img/".concat(fileName));
  42. return Result.success("",url);
  43. }
  44. @Autowired
  45. FdKKClient fdKKClient;
  46. @PostMapping("/uploadE57")
  47. public Result uploadE57(@RequestParam("file") MultipartFile file,Integer isObj) {
  48. String originalFilename = file.getOriginalFilename();
  49. String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
  50. if(!suffix.equals(".e57")){
  51. throw new BusinessException(ResultCode.UPLOAD_ERROR2);
  52. }
  53. try {
  54. String newFileName = UUID.randomUUID().toString().replace("-","");
  55. String filePath = Constant.MANAGE_PATH + "e57" + File.separator + newFileName +suffix;
  56. File targetFile = new File(filePath);
  57. if(!targetFile.getParentFile().exists()){
  58. targetFile.getParentFile().mkdirs();
  59. }
  60. file.transferTo(targetFile);
  61. fYunFileServiceInterface.uploadFile(filePath,filePath.replace(Constant.MANAGE_PATH,"manage/"));
  62. FileUtil.del(targetFile);
  63. UploadEditSceneParam editSceneParam = new UploadEditSceneParam();
  64. editSceneParam.setTitle(originalFilename.replace(suffix,""));
  65. editSceneParam.setUserId(getUser().getId());
  66. editSceneParam.setPath(filePath.replace(Constant.MANAGE_PATH,"manage/"));
  67. editSceneParam.setIsObj(isObj);
  68. editSceneParam.setOtherType("E57_V4");
  69. JSONObject jsonObject = fdKKClient.reverseScene(editSceneParam);
  70. Integer code = jsonObject.getInteger("code");
  71. if(code != 0){
  72. log.info("调用失败-toFdCreateScene:{}",jsonObject);
  73. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  74. }
  75. }catch (Exception e){
  76. log.info("调用失败-toFdCreateScene:",e);
  77. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  78. }
  79. return Result.success();
  80. }
  81. @Autowired
  82. FyunConfig fyunConfig;
  83. @PostMapping("/getUploadUrl")
  84. public Result getUploadUrl( @RequestParam(value = "fileName",required = false)String fileName) {
  85. String newFileName = UUID.randomUUID().toString().replace("-","");
  86. String suffix = fileName.substring(fileName.lastIndexOf("."));
  87. URL presignedUrl = fyunConfig.getPresignedUrl("manage/e57/" + newFileName + suffix);
  88. HashMap<String, Object> map = new HashMap<>();
  89. map.put("newFileName",newFileName + suffix);
  90. map.put("url",presignedUrl.toString());
  91. return Result.success(map);
  92. }
  93. @PostMapping("/relevanceE57")
  94. public Result relevanceE57( @RequestParam(value = "isObj",required = false)Integer isObj,
  95. @RequestParam(value = "title",required = false)String title,
  96. @RequestParam(value = "newFileName",required = false)String newFileName ){
  97. if(StringUtils.isBlank(newFileName)){
  98. throw new BusinessException(ResultCode.PARAM_ERROR);
  99. }
  100. String ossPath = "manage/e57/"+newFileName;
  101. if(!fYunFileServiceInterface.fileExist(ossPath)){
  102. throw new BusinessException(ResultCode.UPLOAD_ERROR3);
  103. }
  104. UploadEditSceneParam editSceneParam = new UploadEditSceneParam();
  105. editSceneParam.setTitle(title);
  106. editSceneParam.setUserId(getUser().getId());
  107. editSceneParam.setPath("manage/e57/"+newFileName);
  108. editSceneParam.setIsObj(isObj);
  109. editSceneParam.setOtherType("E57_V4");
  110. JSONObject jsonObject = fdKKClient.reverseScene(editSceneParam);
  111. Integer code = jsonObject.getInteger("code");
  112. if(code != 0){
  113. log.info("调用失败-toFdCreateScene:{}",jsonObject);
  114. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  115. }
  116. return Result.success();
  117. }
  118. }