LaserService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.fdkankan.download.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.io.file.FileReader;
  4. import cn.hutool.core.io.file.FileWriter;
  5. import cn.hutool.core.thread.ThreadUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import cn.hutool.http.HttpUtil;
  8. import cn.hutool.system.oshi.OshiUtil;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.fdkankan.common.constant.SceneVersionType;
  12. import com.fdkankan.common.exception.BusinessException;
  13. import com.fdkankan.common.util.CmdUtils;
  14. import com.fdkankan.common.util.DateExtUtil;
  15. import com.fdkankan.download.bean.ImageTypeDetail;
  16. import com.fdkankan.download.bean.LaserResultData;
  17. import com.fdkankan.download.bean.ResultData;
  18. import com.fdkankan.download.callback.ErrorCallback;
  19. import com.fdkankan.download.callback.SuccessCallback;
  20. import com.fdkankan.download.callback.SuccessLaserCallback;
  21. import com.fdkankan.download.entity.DTO.OfflineDTO;
  22. import com.fdkankan.download.httpclient.HttpClient;
  23. import com.fdkankan.download.service.IDownloadService;
  24. import com.fdkankan.download.service.ILaserService;
  25. import com.fdkankan.download.service.IScenePlusService;
  26. import com.fdkankan.download.util.DownloadUtil;
  27. import com.fdkankan.download.util.ImgUtil;
  28. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  29. import jodd.util.SystemUtil;
  30. import lombok.SneakyThrows;
  31. import lombok.extern.slf4j.Slf4j;
  32. import net.lingala.zip4j.ZipFile;
  33. import net.lingala.zip4j.model.ZipParameters;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.beans.factory.annotation.Value;
  36. import org.springframework.stereotype.Service;
  37. import oshi.software.os.OperatingSystem;
  38. import oshi.software.os.windows.WindowsOperatingSystem;
  39. import javax.annotation.Resource;
  40. import java.io.File;
  41. import java.nio.charset.Charset;
  42. import java.util.Collections;
  43. import java.util.Date;
  44. import java.util.List;
  45. import java.util.Map;
  46. /**
  47. * @author Xiewj
  48. * @date 2024/1/4
  49. */
  50. @Service
  51. @Slf4j
  52. public class LaserService implements ILaserService {
  53. @Autowired
  54. DownloadUtil downloadUtil;
  55. @Value("${laserhost}")
  56. private String host;
  57. @Resource
  58. private HttpClient httpClient;
  59. @Value("${api.offlineData}")
  60. private String offlineData;
  61. @Resource
  62. private FYunFileServiceInterface fYunFileService;
  63. @Value("${laser.bucket}")
  64. private String bucket;
  65. @Value("${path.source-local-laser}")
  66. private String sourceLocal;
  67. @Value("${path.laserschool}")
  68. private String laserschool;
  69. @Autowired
  70. IDownloadService downloadService;
  71. @Value("${download.config.resource-url}")
  72. private String resourceUrl;
  73. @Value("${download.config.laser-resource-url}")
  74. private String laserResourceUrl;
  75. @Value("${path.zip-local}")
  76. private String zipLocalFormat;
  77. @Override
  78. public String downloadHandler(String num) throws Exception {
  79. String url = host.concat(String.format(offlineData, num));
  80. try {
  81. LaserResultData<List<OfflineDTO> > resultData = httpClient.get(url, new SuccessLaserCallback(), new ErrorCallback());
  82. if (resultData.getCode()==200){
  83. OperatingSystem os = OshiUtil.getOs();
  84. List<OfflineDTO> data = resultData.getData();
  85. data.parallelStream().forEach(datum -> {
  86. //1 json ,2下载,3深度图 ,4全景图
  87. switch (datum.getType()) {
  88. case 1:
  89. FileUtil.writeUtf8String(JSON.toJSONStringWithDateFormat(datum.getData(), "yyyy-MM-dd HH:mm:ss").replaceAll("\\n", " ").replaceAll("\\t", " "), sourceLocal + File.separator + datum.getPath());
  90. break;
  91. case 2:
  92. if (os instanceof WindowsOperatingSystem) {
  93. String path = datum.getKey();
  94. if (datum.getKey().startsWith("/")) {
  95. path = path.substring(1);
  96. }
  97. List<String> strings = fYunFileService.listRemoteFiles(bucket, path);
  98. for (String key : strings) {
  99. if (StrUtil.isEmpty(FileUtil.extName(key))) {
  100. continue;
  101. }
  102. String srcPath = key;
  103. if (!key.startsWith("/")) {
  104. srcPath = "/" + key;
  105. }
  106. downloadUtil.downloadFile(laserResourceUrl + key, sourceLocal +
  107. File.separator + num +
  108. File.separator + "www" +
  109. File.separator + num + srcPath,1);
  110. }
  111. } else {
  112. String srcPath = datum.getKey();
  113. if ("/".equals(datum.getKey().substring(0, 1))) {
  114. srcPath = srcPath.substring(1);
  115. }
  116. fYunFileService.downloadFileByCommand(bucket, sourceLocal + datum.getPath(), srcPath);
  117. }
  118. break;
  119. case 3:
  120. // if (os instanceof WindowsOperatingSystem){
  121. String path = datum.getKey();
  122. if (datum.getKey().startsWith("/")) {
  123. path = path.substring(1);
  124. }
  125. List<String> strings = fYunFileService.listRemoteFiles(bucket, path);
  126. for (String key : strings) {
  127. if (StrUtil.isEmpty(FileUtil.extName(key)) || FileUtil.extName(key).equalsIgnoreCase("ply")) {
  128. continue;
  129. }
  130. downloadUtil.downloadFile(laserResourceUrl + key, sourceLocal +
  131. File.separator + num +
  132. File.separator + "www" +
  133. File.separator + num +
  134. File.separator + "wwwroot" +
  135. File.separator + datum.getSceneCode() +
  136. File.separator + "data" +
  137. File.separator + datum.getSceneCode() +
  138. File.separator + "depthmap" +
  139. File.separator + FileUtil.getName(key),1);
  140. }
  141. // }else{
  142. // String srcPath=datum.getKey();
  143. // if("/".equals(datum.getKey().substring(0,1))){
  144. // srcPath = srcPath.substring(1);
  145. // }
  146. // fYunFileService.downloadFileByCommand(bucket,sourceLocal+datum.getPath(),srcPath);
  147. // File[] deptLs = FileUtil.ls(sourceLocal+datum.getPath());
  148. // for (File dept : deptLs) {
  149. // if (FileUtil.extName(dept).equalsIgnoreCase("ply")){
  150. // FileUtil.del(dept);
  151. // }
  152. // }
  153. // }
  154. break;
  155. case 4:
  156. try {
  157. downloadService.cutImg(datum.getSceneCode(), sourceLocal + datum.getPath(), "4k", "tiles", SceneVersionType.V4.code());
  158. } catch (Exception e) {
  159. throw new RuntimeException(e);
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. });
  166. String path=sourceLocal+File.separator+num;
  167. //处理html的编码
  168. String htmlPath=laserschool+"www/offline.html";
  169. if (FileUtil.exist(htmlPath)){
  170. String copyPath=path+File.separator+"www"+File.separator;
  171. FileUtil.copy(htmlPath, copyPath, true);
  172. FileReader fileReader = new FileReader(copyPath+"offline.html");
  173. String html = fileReader.readString();
  174. FileWriter fileWriter=new FileWriter(copyPath+"offline.html");
  175. fileWriter.write(html.replaceAll("@lang@","zh"),false);
  176. }
  177. String browserBatPath=laserschool+"start-browser.bat";
  178. if (FileUtil.exist(browserBatPath)){
  179. String copyPath=path+File.separator;
  180. FileUtil.copy(browserBatPath, copyPath, true);
  181. FileReader fileReader = new FileReader(copyPath+"start-browser.bat");
  182. String bat = fileReader.readString();
  183. FileWriter fileWriter=new FileWriter(copyPath+"start-browser.bat");
  184. fileWriter.write(bat.replaceAll("@sceneCode@",num),false);
  185. }
  186. String zipPath = String.format(this.zipLocalFormat, DateExtUtil.format(new Date(), DateExtUtil.dateStyle6),num + "_laser");
  187. File file=offlineZip(path,zipPath);
  188. log.info("生成完成路径{}",file.getAbsoluteFile());
  189. return zipPath;
  190. }
  191. }catch (Exception e){
  192. log.error("laser场景下载报错,num:{}", num, e);
  193. throw e;
  194. }finally {
  195. String path=sourceLocal+File.separator+num;
  196. FileUtil.del(path);
  197. }
  198. return "";
  199. }
  200. public final static String ZIP_PATH = "cd @path && cp -f @bashOffline . && zip -u -r bashOffline.zip start-browser.bat www/ && mv -f bashOffline.zip @target";
  201. @SneakyThrows
  202. public File offlineZip( String path,String moveZipPath) {
  203. String bashOfflinePath = laserschool + "bashOffline.zip";
  204. File file = new File(moveZipPath);
  205. if(!file.getParentFile().exists()){
  206. file.getParentFile().mkdirs();
  207. }
  208. String zipCmd = ZIP_PATH;
  209. zipCmd = zipCmd.replace("@bashOffline", bashOfflinePath);
  210. zipCmd = zipCmd.replace("@target", moveZipPath);
  211. zipCmd = zipCmd.replace("@path", path);
  212. CmdUtils.callLineSh(zipCmd);
  213. if(!FileUtil.exist(moveZipPath)){
  214. throw new RuntimeException("打包失败");
  215. }
  216. return FileUtil.file(moveZipPath);
  217. }
  218. }