UploadController.java 6.5 KB

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