LaserService.java 11 KB

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