RasterServerImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. package com.fd.server.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fd.constant.MsgCode;
  6. import com.fd.constant.TypeCode;
  7. import com.fd.dto.ConfigJsonDto;
  8. import com.fd.dto.PageDto;
  9. import com.fd.entity.FileEntity;
  10. //import com.fd.entity.LayerEntity;
  11. import com.fd.entity.OutputFileEntity;
  12. import com.fd.entity.StyleEntity;
  13. import com.fd.entity.User;
  14. import com.fd.repository.FileRepository;
  15. //import com.fd.repository.LayerRepository;
  16. import com.fd.repository.OutputFileRepository;
  17. import com.fd.repository.StyleRepository;
  18. import com.fd.repository.UserRepository;
  19. import com.fd.server.CmdServer;
  20. import com.fd.server.RasterServer;
  21. import com.fd.shiro.JWTUtil;
  22. import com.fd.util.FileUtils;
  23. import com.fd.util.R;
  24. import lombok.extern.log4j.Log4j2;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.data.domain.Page;
  29. import org.springframework.data.domain.PageRequest;
  30. import org.springframework.data.domain.Sort;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import java.io.BufferedReader;
  34. import java.io.IOException;
  35. import java.io.InputStreamReader;
  36. import java.util.Date;
  37. import java.util.List;
  38. import java.util.Optional;
  39. /**
  40. * Created by Owen on 2019/11/21 0021 15:29
  41. */
  42. @Log4j2
  43. @Service
  44. public class RasterServerImpl extends BaseServerImpl implements RasterServer {
  45. @Value("${input.file.path.raster}")
  46. private String INPUT_FILE_PATH;
  47. @Value("${output.file.path.raster}")
  48. private String OUTPUT_FILE_PATH;
  49. // @Value("${copy.file.path.raster}")
  50. // private String MOVE_FILE_TO_SERVER;
  51. // config.json 地址
  52. @Value("${config.path}")
  53. private String CONFIG_JSON_PATH;
  54. // config.json teileset 的相对路径
  55. // @Value("${config.tileset}")
  56. // private String CONFIG_TILESET;
  57. @Value("${base.path}")
  58. private String BASE_PATH;
  59. @Autowired
  60. private FileRepository fileRepository;
  61. @Autowired
  62. private OutputFileRepository outputFileRepository;
  63. // @Autowired
  64. // private CmdServer cmdServer;
  65. @Autowired
  66. private StyleRepository styleRepository;
  67. @Autowired
  68. private UserRepository userRepository;
  69. @Override
  70. public R deleteById(Long fileId) {
  71. // 删除服务器文件
  72. // 删除服务器文件
  73. Optional<OutputFileEntity> e = outputFileRepository.findById(fileId);
  74. if (!e.isPresent()) {
  75. return new R(50002, MsgCode.E50002);
  76. }
  77. OutputFileEntity fileEntity = e.get();
  78. // 删除配置文件config.json制定行
  79. StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(fileId);
  80. if (styleEntity != null) {
  81. deleteRowConfigJson(styleEntity);
  82. }
  83. // 删除数据库记录
  84. outputFileRepository.deleteById(fileId);
  85. fileRepository.deleteById(fileEntity.getUploadId());
  86. styleRepository.deleteByOutputFileId(fileId);
  87. // 文件
  88. if (fileEntity.getUploadPath() != null) {
  89. FileUtils.delFolder(fileEntity.getUploadPath());
  90. }
  91. if (fileEntity.getCoordGeneralPath() != null) {
  92. FileUtils.delFolder(fileEntity.getCoordGeneralPath());
  93. }
  94. if (fileEntity.getUnZipPath() != null) {
  95. FileUtils.delFolder(fileEntity.getUnZipPath());
  96. }
  97. if (fileEntity.getGeojsonPath() != null) {
  98. FileUtils.delFolder(fileEntity.getGeojsonPath());
  99. }
  100. if (fileEntity.getCoordStrictPath() != null) {
  101. FileUtils.delFolder(fileEntity.getCoordStrictPath());
  102. }
  103. if (fileEntity.getSlicePath() != null) {
  104. FileUtils.delFolder(fileEntity.getSlicePath());
  105. }
  106. return new R(200, MsgCode.SUCCESS);
  107. }
  108. /**
  109. * 删除指定行的config.json 数据
  110. */
  111. private void deleteRowConfigJson(StyleEntity entity) {
  112. String s = FileUtils.readFile(CONFIG_JSON_PATH);
  113. JSONObject original = JSON.parseObject(s);
  114. log.info("original: {}", s);
  115. JSONArray layers = JSON.parseArray(original.getString("layers"));
  116. for (int i = 0; i < layers.size(); i++) {
  117. JSONObject o = (JSONObject) layers.get(i);
  118. if (o.getString("name").equals(entity.getName())) {
  119. // 删除对象
  120. layers.remove(i);
  121. }
  122. }
  123. // 更新json
  124. original.put("layers", layers);
  125. log.info("original update: {}", original.toJSONString());
  126. // 更新config.json
  127. try {
  128. FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. @Override
  134. public R moveFileToServer(Long fileId, ConfigJsonDto param) {
  135. Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
  136. if (!o.isPresent()) {
  137. log.info("id:{} 不存在", fileId);
  138. return new R(50002, MsgCode.E50002);
  139. }
  140. // 把数据移动到指定服务位置
  141. OutputFileEntity entity = o.get();
  142. try {
  143. // 修改前端的config.json 文件
  144. String lastName = StringUtils.substringAfterLast(entity.getSlicePath(), "/");
  145. log.info("lastName: {}", lastName);
  146. writeJsonFile(param, lastName, entity);
  147. // 成功,更新状态
  148. entity.setStatus(8);
  149. entity.setUpdateTime(new Date());
  150. // 添加图层角色
  151. entity.setLayerRole(param.getRole());
  152. outputFileRepository.save(entity);
  153. return new R(200, MsgCode.SUCCESS);
  154. } catch (Exception e) {
  155. entity.setStatus(10);
  156. entity.setUpdateTime(new Date());
  157. outputFileRepository.save(entity);
  158. e.printStackTrace();
  159. return new R(51004, MsgCode.E51004, e);
  160. }
  161. }
  162. @Override
  163. public R uploadBigFile(MultipartFile file, String strCoord, String token) {
  164. long start = System.currentTimeMillis();
  165. if (file.isEmpty() || file.getSize() <= 0) {
  166. log.info("文件为空");
  167. return new R(50001, MsgCode.E50001);
  168. }
  169. // 文件名全名
  170. String fullFileName = file.getOriginalFilename();
  171. // 创建目录路径
  172. FileUtils.createDir(INPUT_FILE_PATH);
  173. // 拼接唯一文件名
  174. long timeMillis = System.currentTimeMillis();
  175. // String fileName = timeMillis + "_" + fullFileName;
  176. String fileName = fullFileName;
  177. // 文件保存路径
  178. String filePath = INPUT_FILE_PATH + fileName;
  179. // 写文件到本地
  180. try {
  181. FileUtils.bigFileWrite(file.getInputStream(), filePath);
  182. } catch (IOException e) {
  183. e.printStackTrace();
  184. }
  185. log.info("filePath: {}", filePath);
  186. String username = JWTUtil.getUsername(token);
  187. // 根据用户名查找用户
  188. User user = userRepository.findByUsername(username);
  189. // 保存信息到db
  190. FileEntity entity = new FileEntity();
  191. entity.setFileName(fileName);
  192. entity.setFileUrl(filePath);
  193. entity.setCreateTime(new Date());
  194. entity.setUpdateTime(new Date());
  195. entity.setType(TypeCode.FILE_TYPE_RASTER);
  196. entity.setCoord(strCoord);
  197. entity.setStatus(2);
  198. entity.setResStatus(0);
  199. fileRepository.save(entity);
  200. OutputFileEntity outputFile = new OutputFileEntity();
  201. outputFile.setUploadId(entity.getId());
  202. outputFile.setUploadPath(entity.getFileUrl());
  203. outputFile.setFileName(entity.getFileName());
  204. outputFile.setStatus(2);
  205. outputFile.setResStatus(0);
  206. outputFile.setCoord(strCoord);
  207. outputFile.setType(TypeCode.FILE_TYPE_RASTER);
  208. outputFile.setCreateTime(new Date());
  209. outputFile.setUpdateTime(new Date());
  210. // 添加分组
  211. outputFile.setUserId(user.getId());
  212. outputFile.setUserGroup(user.getUserGroup());
  213. outputFile = outputFileRepository.save(outputFile);
  214. long end = System.currentTimeMillis();
  215. log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
  216. return new R(200, outputFile);
  217. }
  218. @Override
  219. public R findByType(String fileTypeRaster, PageDto pageDto) {
  220. Page<OutputFileEntity> page = outputFileRepository.findByTypeAndResStatus(fileTypeRaster, 0, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
  221. return new R(200, page);
  222. }
  223. @Override
  224. public OutputFileEntity findById(Long fileId) {
  225. Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
  226. if (o.isPresent()) {
  227. return o.get();
  228. }
  229. return null;
  230. }
  231. @Override
  232. public List<FileEntity> findByFileName(String fileName) {
  233. return fileRepository.findByFileNameAndTypeAndResStatus(fileName, TypeCode.FILE_TYPE_RASTER, 0);
  234. }
  235. @Override
  236. public OutputFileEntity save(OutputFileEntity fileSchedule) {
  237. return outputFileRepository.save(fileSchedule);
  238. }
  239. @Override
  240. public FileEntity findByInputFileId(Long fileId) {
  241. Optional<FileEntity> o = fileRepository.findById(fileId);
  242. if (o.isPresent()) {
  243. return o.get();
  244. }
  245. return null;
  246. }
  247. @Override
  248. public FileEntity saveInputFile(FileEntity fileEntity) {
  249. return fileRepository.save(fileEntity);
  250. }
  251. @Override
  252. public Integer cmdJudgeCoord(String commandStr) {
  253. // 命令运行结果 1:失败, 0:成功
  254. Integer isCmd = null;
  255. StringBuffer sb = new StringBuffer();
  256. StringBuffer errorStr = new StringBuffer();
  257. try {
  258. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  259. Process ps = Runtime.getRuntime().exec(cmd);
  260. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  261. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  262. // error : 坑, 控制台信息是从errorBuf这里出来的
  263. String errorLine;
  264. while ((errorLine = errorBuf.readLine()) != null) {
  265. errorStr.append(errorLine).append("\n");
  266. }
  267. if (StringUtils.isNotEmpty(errorStr)){
  268. log.info("error result: {}", errorStr.toString());
  269. }
  270. // success ,没有获取到信息
  271. String line;
  272. while ((line = br.readLine()) != null) {
  273. // log.info("===== br.readLine: ======== {}", br.readLine());
  274. //执行结果加上回车
  275. sb.append(line).append("\n");
  276. }
  277. log.info("result: {}", sb.toString());
  278. // 结束命令行
  279. isCmd = ps.waitFor();
  280. // 关闭流
  281. br.close();
  282. errorBuf.close();
  283. } catch (Exception e) {
  284. e.printStackTrace();
  285. }
  286. if (isCmd == 0) {
  287. log.info("end exeCmd : {}", isCmd);
  288. // 判断坐标
  289. if (sb.toString().contains("+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=38500000")) {
  290. // 需要坐标转换
  291. // 1000: 需要转换
  292. isCmd = 1000;
  293. }
  294. if (sb.toString().contains("+proj=longlat +datum=WGS84")) {
  295. // 不需要坐标转换
  296. isCmd = 0;
  297. }
  298. } else {
  299. log.info("error cmd wsitFore: {}", isCmd);
  300. }
  301. return isCmd;
  302. }
  303. @Override
  304. public Integer exeCmd(String cmd1, String cmd2) {
  305. exeCmdSingle(cmd1);
  306. Integer isCmd = exeCmdSingle(cmd2);
  307. return isCmd;
  308. }
  309. @Override
  310. public Integer cmdSlice(String commandStr, OutputFileEntity entity) {
  311. log.warn("run cmdSlice");
  312. Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
  313. try {
  314. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  315. Process ps = Runtime.getRuntime().exec(cmd);
  316. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  317. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  318. StringBuffer sb = new StringBuffer();
  319. // 进度
  320. int pre = 0;
  321. int num;
  322. String str;
  323. while ((num = br.read()) != -1) {
  324. str = String.valueOf((char) num);
  325. // 判断数字
  326. if (numRegex(str)) {
  327. if (!str.equals("0")) {
  328. // 非0开头的
  329. sb.append(",").append(str);
  330. } else {
  331. // 以0开头的
  332. sb.append(str);
  333. }
  334. // 截取进度数字
  335. str = StringUtils.substringAfterLast(sb.toString(), ",");
  336. // 100,1000都会当一次
  337. if (str.length() == 2) {
  338. pre = pre + 5;
  339. log.warn("pre: {}", pre);
  340. // 能被2整除的存一下db
  341. if (pre % 2 == 0) {
  342. entity.setProgress(pre);
  343. entity.setUpdateTime(new Date());
  344. outputFileRepository.save(entity);
  345. }
  346. }
  347. }
  348. }
  349. log.info("cmd console: {}", sb.toString());
  350. StringBuffer errorStr = new StringBuffer();
  351. String errorLine;
  352. while ((errorLine = errorBuf.readLine()) != null) {
  353. errorStr.append(errorLine).append("\n");
  354. }
  355. if (StringUtils.isNotEmpty(errorStr)){
  356. log.info("error result: {}", errorStr.toString());
  357. }
  358. // 结束命令行
  359. isCmd = ps.waitFor();
  360. // 关闭流
  361. br.close();
  362. errorBuf.close();
  363. } catch (Exception e) {
  364. e.printStackTrace();
  365. }
  366. if (isCmd == 0) {
  367. log.info("end exeCmd : {}", isCmd);
  368. } else {
  369. log.info("wsitFore cmd run error : {}", isCmd);
  370. }
  371. log.warn("end exeCmdRasterSlice");
  372. return isCmd;
  373. }
  374. private Integer exeCmdSingle(String commandStr) {
  375. // 命令运行结果 1:失败, 0:成功
  376. Integer isCmd = null;
  377. try {
  378. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  379. Process ps = Runtime.getRuntime().exec(cmd);
  380. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  381. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  382. StringBuffer sb = new StringBuffer();
  383. StringBuffer errorStr = new StringBuffer();
  384. // error : 坑, 控制台信息是从errorBuf这里出来的
  385. String errorLine;
  386. while ((errorLine = errorBuf.readLine()) != null) {
  387. errorStr.append(errorLine).append("\n");
  388. }
  389. if (StringUtils.isNotEmpty(errorStr)){
  390. log.info("error result: {}", errorStr.toString());
  391. }
  392. // success ,没有获取到信息
  393. String line;
  394. while ((line = br.readLine()) != null) {
  395. //执行结果加上回车
  396. sb.append(line).append("\n");
  397. }
  398. log.info("result: {}", sb.toString());
  399. // 结束命令行
  400. isCmd = ps.waitFor();
  401. // 关闭流
  402. br.close();
  403. errorBuf.close();
  404. } catch (Exception e) {
  405. e.printStackTrace();
  406. }
  407. if (isCmd == 0) {
  408. log.info("end exeCmd : {}", isCmd);
  409. } else {
  410. log.info("wsitFore cmd run error : {}", isCmd);
  411. }
  412. return isCmd;
  413. }
  414. private void writeJsonFile(ConfigJsonDto param, String lastName, OutputFileEntity entity) {
  415. String s = FileUtils.readFile(CONFIG_JSON_PATH);
  416. StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getId());
  417. if (styleEntity == null) {
  418. styleEntity = new StyleEntity();
  419. styleEntity.setOutputFileId(entity.getId());
  420. styleEntity.setCreateTime(new Date());
  421. styleEntity.setResStatus(0);
  422. }
  423. JSONObject original = JSON.parseObject(s);
  424. log.info("original: {}", s);
  425. JSONArray layers = JSON.parseArray(original.getString("layers"));
  426. JSONObject subJson = new JSONObject();
  427. long cu = System.currentTimeMillis();
  428. // 需要唯一
  429. String name = "raster_" + cu;
  430. subJson.put("name", name);
  431. subJson.put("text", param.getText());
  432. // raster 就用这个类型
  433. subJson.put("type", "imagery");
  434. subJson.put("checked", false);
  435. subJson.put("show", true);
  436. String slicePath = entity.getSlicePath();
  437. // slicePath = slicePath.replace("/root/gis/data", "");
  438. slicePath = slicePath.replace(BASE_PATH, "");
  439. subJson.put("url", slicePath);
  440. layers.add(subJson);
  441. original.put("layers", layers);
  442. log.info("original update: {}", original.toJSONString());
  443. try {
  444. FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  445. // 将图层信息保存到db
  446. // 前端需要layer信息
  447. styleEntity.setLayer(subJson.toJSONString());
  448. styleEntity.setUpdateTime(new Date());
  449. styleEntity.setName(name);
  450. styleRepository.save(styleEntity);
  451. } catch (IOException e) {
  452. e.printStackTrace();
  453. }
  454. }
  455. // 匹配数字
  456. private boolean numRegex(String str) {
  457. String reg = "\\d";
  458. if (StringUtils.isEmpty(str)) {
  459. return false;
  460. }
  461. return str.matches(reg);
  462. }
  463. }