LaserService.java 11 KB

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