ObjectMatchController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.taais.biz.controller;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.*;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipInputStream;
  11. import cn.hutool.http.HttpUtil;
  12. import cn.hutool.json.JSONObject;
  13. import cn.hutool.json.JSONUtil;
  14. import com.taais.biz.constant.BizConstant;
  15. import com.taais.biz.domain.Data;
  16. import com.taais.biz.domain.ObjectMatch;
  17. import com.taais.biz.domain.bo.ObjectMatchBo;
  18. import com.taais.biz.domain.vo.ObjectMatchVo;
  19. import com.taais.biz.service.service.IObjectMatchService;
  20. import com.taais.biz.service.service.impl.ObjectMatchServiceImpl;
  21. import com.taais.common.core.config.TaaisConfig;
  22. import com.taais.common.core.utils.StringUtils;
  23. import com.taais.common.core.utils.file.FileUploadUtils;
  24. import com.taais.common.core.utils.file.UnPackedUtil;
  25. import com.taais.common.core.utils.uuid.UUID;
  26. import com.taais.system.domain.vo.SysOssVo;
  27. import com.taais.system.service.ISysOssService;
  28. import lombok.RequiredArgsConstructor;
  29. import jakarta.servlet.http.HttpServletResponse;
  30. import cn.dev33.satoken.annotation.SaCheckPermission;
  31. import lombok.extern.slf4j.Slf4j;
  32. import net.lingala.zip4j.model.FileHeader;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.validation.annotation.Validated;
  35. import com.taais.common.core.core.domain.CommonResult;
  36. import com.taais.common.excel.utils.ExcelUtil;
  37. import com.taais.common.log.annotation.Log;
  38. import com.taais.common.log.enums.BusinessType;
  39. import com.taais.common.web.annotation.RepeatSubmit;
  40. import com.taais.common.web.core.BaseController;
  41. import jakarta.annotation.Resource;
  42. import com.taais.common.core.core.page.PageResult;
  43. import org.springframework.web.multipart.MultipartFile;
  44. import static com.taais.biz.constant.BizConstant.*;
  45. /**
  46. * 异源图像匹配Controller
  47. *
  48. * @author km
  49. * 2024-10-28
  50. */
  51. @Validated
  52. @RequiredArgsConstructor
  53. @RestController
  54. @RequestMapping("/demo/match")
  55. @Slf4j
  56. public class ObjectMatchController extends BaseController {
  57. @Resource
  58. private ObjectMatchServiceImpl objectMatchService;
  59. @Resource
  60. private ISysOssService sysOssService;
  61. /**
  62. * 查询异源图像匹配列表
  63. */
  64. @SaCheckPermission("demo:match:list")
  65. @GetMapping("/list")
  66. public CommonResult<PageResult<ObjectMatchVo>> list(ObjectMatchBo objectMatchBo) {
  67. return CommonResult.success(objectMatchService.selectPage(objectMatchBo));
  68. }
  69. /**
  70. * 导出异源图像匹配列表
  71. */
  72. @SaCheckPermission("demo:match:export")
  73. @Log(title = "异源图像匹配", businessType = BusinessType.EXPORT)
  74. @PostMapping("/export")
  75. public void export(HttpServletResponse response, ObjectMatchBo objectMatchBo) {
  76. List<ObjectMatchVo> list = objectMatchService.selectList(objectMatchBo);
  77. ExcelUtil.exportExcel(list, "异源图像匹配", ObjectMatchVo.class, response);
  78. }
  79. /**
  80. * 获取异源图像匹配详细信息
  81. */
  82. @SaCheckPermission("demo:match:query")
  83. @GetMapping(value = "/{id}")
  84. public CommonResult<ObjectMatchVo> getInfo(@PathVariable Long id) {
  85. return CommonResult.success(objectMatchService.selectById(id));
  86. }
  87. /**
  88. * 新增异源图像匹配
  89. */
  90. @SaCheckPermission("demo:match:add")
  91. @Log(title = "异源图像匹配", businessType = BusinessType.INSERT)
  92. @RepeatSubmit()
  93. @PostMapping
  94. public CommonResult<Void> add(@Validated @RequestBody ObjectMatchBo objectMatchBo) {
  95. boolean inserted = objectMatchService.insert(objectMatchBo);
  96. if (!inserted) {
  97. return CommonResult.fail("新增异源图像匹配记录失败!");
  98. }
  99. return CommonResult.success();
  100. }
  101. /**
  102. * 修改异源图像匹配
  103. */
  104. @SaCheckPermission("demo:match:edit")
  105. @Log(title = "异源图像匹配", businessType = BusinessType.UPDATE)
  106. @RepeatSubmit()
  107. @PutMapping
  108. public CommonResult<Void> edit(@Validated @RequestBody ObjectMatchBo objectMatchBo) {
  109. Boolean updated = objectMatchService.update(objectMatchBo);
  110. if (!updated) {
  111. return CommonResult.fail("修改异源图像匹配记录失败!");
  112. }
  113. return CommonResult.success();
  114. }
  115. /**
  116. * 删除异源图像匹配
  117. */
  118. @SaCheckPermission("demo:match:remove")
  119. @Log(title = "异源图像匹配", businessType = BusinessType.DELETE)
  120. @DeleteMapping("/{ids}")
  121. public CommonResult<Void> remove(@PathVariable Long[] ids) {
  122. boolean deleted = objectMatchService.deleteByIds(ids);
  123. if (!deleted) {
  124. return CommonResult.fail("删除异源图像匹配记录失败!");
  125. }
  126. return CommonResult.success();
  127. }
  128. @GetMapping("/execute")
  129. public CommonResult<Void> execute(String taskId) {
  130. ObjectMatchBo bo = objectMatchService.getById(Long.parseLong(taskId));
  131. bo.setStartTime(new Date());
  132. Map<String, String> params = new HashMap<>();
  133. params.put("bizId", String.valueOf(bo.getId()));
  134. params.put("bizType", TYPE_OBJ_MATCH);
  135. params.put("logPath", DOCKER_BASE_PATH + bo.getResultPath() + "/log.log");
  136. params.put("sourcePath", DOCKER_BASE_PATH + bo.getPreprocessPath());
  137. params.put("resultPath", DOCKER_BASE_PATH + bo.getResultPath());
  138. params.put("otherParams", new JSONObject().toString());
  139. log.info("obj_match params: {}", params);
  140. try {
  141. String res = HttpUtil.post(MULTI_OBJ_MATCH_URL, JSONUtil.toJsonStr(params));
  142. bo.setStatus(TASK_STATUS_PROCESSING);
  143. } catch (Exception e) {
  144. log.error("HTTP请求失败",e);
  145. bo.setStatus(TASK_STATUS_FAILED);
  146. bo.setEndTime(new Date());
  147. bo.setCostSecond((bo.getEndTime().getTime() - bo.getStartTime().getTime()) / 1000);
  148. return CommonResult.fail("HTTP请求失败" + e.getMessage());
  149. } finally {
  150. objectMatchService.update(bo);
  151. }
  152. return CommonResult.success();
  153. }
  154. @GetMapping("/result")
  155. public CommonResult<List<String>> getResult(String taskId) {
  156. ObjectMatchBo bo = objectMatchService.getById(Long.parseLong(taskId));
  157. if (bo == null) {
  158. return CommonResult.fail("未找到任务", null);
  159. }
  160. String path = bo.getResultPath();
  161. File dir = new File(BizConstant.DOCKER_BASE_PATH + path + "/IR_VIS_obj_in_IR");
  162. List<String> res = new ArrayList<>();
  163. if (dir.exists()) {
  164. for (File file : dir.listFiles()) {
  165. res.add(file.getName());
  166. }
  167. }
  168. return CommonResult.success(res);
  169. }
  170. @PostMapping("/createTask")
  171. public CommonResult<Void> createTask(@RequestBody Map<String, String> params) throws IOException {
  172. String file = params.get("file");
  173. if (file == null) {
  174. return CommonResult.fail("请上传.zip压缩文件。");
  175. }
  176. ObjectMatchBo match = new ObjectMatchBo();
  177. match.setName(params.get("name"));
  178. match.setParameters(params.get("parameters"));
  179. match.setStatus(BizConstant.TASK_STATUS_PENDING);
  180. String path = BizConstant.DOCKER_OBJ_MATCH_PATH + "/" + UUID.randomUUID().toString();
  181. match.setPreprocessPath(path);
  182. SysOssVo _file = sysOssService.getById(Long.parseLong(file));
  183. String filePath = TaaisConfig.getProfile() + _file.getUrl().split("/profile")[1];
  184. ZipFileExtractor.extractZipFile(filePath, DOCKER_BASE_PATH + path);
  185. match.setResultPath(path + "/result");
  186. File dir = new File(DOCKER_BASE_PATH + match.getResultPath());
  187. if (!dir.exists()) {
  188. dir.mkdirs();
  189. }
  190. boolean res = objectMatchService.insert(match);
  191. return res ? CommonResult.success() : CommonResult.fail();
  192. }
  193. public static class ZipFileExtractor {
  194. public static void extractZipFile(String zipFilePath, String destDir) throws IOException {
  195. Path zipPath = Paths.get(zipFilePath);
  196. Path destDirPath = Paths.get(destDir);
  197. if (Files.notExists(destDirPath)) {
  198. Files.createDirectories(destDirPath);
  199. }
  200. try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipPath))) {
  201. ZipEntry entry = zis.getNextEntry();
  202. while (entry != null) {
  203. Path extractedPath = destDirPath.resolve(entry.getName());
  204. if (entry.isDirectory()) {
  205. Files.createDirectories(extractedPath);
  206. } else {
  207. // Ensure parent directories exist
  208. if (extractedPath.getParent() != null && Files.notExists(extractedPath.getParent())) {
  209. Files.createDirectories(extractedPath.getParent());
  210. }
  211. // Write file content
  212. try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(extractedPath))) {
  213. byte[] buffer = new byte[1024];
  214. int len;
  215. while ((len = zis.read(buffer)) != -1) {
  216. bos.write(buffer, 0, len);
  217. }
  218. }
  219. }
  220. zis.closeEntry();
  221. entry = zis.getNextEntry();
  222. }
  223. }
  224. }
  225. public static void main(String[] args) {
  226. try {
  227. String zipFilePath = "path/to/your/zipfile.zip";
  228. String destDir = "path/to/your/destDir";
  229. extractZipFile(zipFilePath, destDir);
  230. } catch (IOException e) {
  231. e.printStackTrace();
  232. }
  233. }
  234. }
  235. }