From a7aaeabc7873a0eafb4a7ecad7f65b018b7a9bc9 Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Fri, 24 Feb 2023 19:31:23 +0800
Subject: [PATCH] What's new? 1. Add license for dock. 2. Modify the logic corresponding to the firmware file and device type. 3. Add multiple mqtt clients options. 4. Modify the structure of the interface for obtaining the device list. 5. Fixed some issues.
---
src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 101 insertions(+), 15 deletions(-)
diff --git a/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java b/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java
index f12979a..f540b66 100644
--- a/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java
+++ b/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java
@@ -7,24 +7,36 @@
import com.dji.sample.common.model.PaginationData;
import com.dji.sample.component.oss.model.OssConfiguration;
import com.dji.sample.component.oss.service.impl.OssServiceContext;
+import com.dji.sample.manage.model.enums.DeviceDomainEnum;
import com.dji.sample.wayline.dao.IWaylineFileMapper;
+import com.dji.sample.wayline.model.dto.KmzFileProperties;
import com.dji.sample.wayline.model.dto.WaylineFileDTO;
import com.dji.sample.wayline.model.entity.WaylineFileEntity;
import com.dji.sample.wayline.model.param.WaylineQueryParam;
import com.dji.sample.wayline.service.IWaylineFileService;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Node;
+import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Optional;
-import java.util.UUID;
+import java.util.*;
import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import static com.dji.sample.wayline.model.dto.KmzFileProperties.WAYLINE_FILE_SUFFIX;
/**
* @author sean
@@ -40,9 +52,6 @@
@Autowired
private OssServiceContext ossService;
-
- @Autowired
- private OssConfiguration configuration;
@Override
public PaginationData<WaylineFileDTO> getWaylinesByParam(String workspaceId, WaylineQueryParam param) {
@@ -85,7 +94,7 @@
if (waylineOpt.isEmpty()) {
throw new SQLException(waylineId + " does not exist.");
}
- return ossService.getObjectUrl(configuration.getBucket(), waylineOpt.get().getObjectKey());
+ return ossService.getObjectUrl(OssConfiguration.bucket, waylineOpt.get().getObjectKey());
}
@Override
@@ -94,13 +103,17 @@
file.setWaylineId(UUID.randomUUID().toString());
file.setWorkspaceId(workspaceId);
- byte[] object = ossService.getObject(configuration.getBucket(), metadata.getObjectKey());
- if (object.length == 0) {
- throw new RuntimeException("The file " + metadata.getObjectKey() +
- " does not exist in the bucket[" + configuration.getBucket() + "].");
+ if (!StringUtils.hasText(file.getSign())) {
+ try (InputStream object = ossService.getObject(OssConfiguration.bucket, metadata.getObjectKey())) {
+ if (object.available() == 0) {
+ throw new RuntimeException("The file " + metadata.getObjectKey() +
+ " does not exist in the bucket[" + OssConfiguration.bucket + "].");
+ }
+ file.setSign(DigestUtils.md5DigestAsHex(object));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
-
- file.setSign(DigestUtils.md5DigestAsHex(object));
int insertId = mapper.insert(file);
return insertId > 0 ? file.getId() : insertId;
}
@@ -143,9 +156,81 @@
if (!isDel) {
return false;
}
- return ossService.deleteObject(configuration.getBucket(), wayline.getObjectKey());
+ return ossService.deleteObject(OssConfiguration.bucket, wayline.getObjectKey());
}
+ @Override
+ public void importKmzFile(MultipartFile file, String workspaceId, String creator) {
+ Optional<WaylineFileDTO> waylineFileOpt = validKmzFile(file);
+ if (waylineFileOpt.isEmpty()) {
+ throw new RuntimeException("The file format is incorrect.");
+ }
+
+ try {
+ WaylineFileDTO waylineFile = waylineFileOpt.get();
+ waylineFile.setWaylineId(workspaceId);
+ waylineFile.setUsername(creator);
+
+ ossService.putObject(OssConfiguration.bucket, waylineFile.getObjectKey(), file.getInputStream());
+ this.saveWaylineFile(workspaceId, waylineFile);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private Optional<WaylineFileDTO> validKmzFile(MultipartFile file) {
+ String filename = file.getOriginalFilename();
+ if (Objects.nonNull(filename) && !filename.endsWith(WAYLINE_FILE_SUFFIX)) {
+ throw new RuntimeException("The file format is incorrect.");
+ }
+ try (ZipInputStream unzipFile = new ZipInputStream(file.getInputStream(), StandardCharsets.UTF_8)) {
+
+ ZipEntry nextEntry = unzipFile.getNextEntry();
+ while (Objects.nonNull(nextEntry)) {
+ boolean isWaylines = (KmzFileProperties.FILE_DIR_FIRST + "/" + KmzFileProperties.FILE_DIR_SECOND_WAYLINES).equals(nextEntry.getName());
+ if (!isWaylines) {
+ nextEntry = unzipFile.getNextEntry();
+ continue;
+ }
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(unzipFile);
+ if (!StandardCharsets.UTF_8.name().equals(document.getXMLEncoding())) {
+ throw new RuntimeException("The file encoding format is incorrect.");
+ }
+
+ Node droneNode = document.selectSingleNode("//" + KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_DRONE_INFO);
+ Node payloadNode = document.selectSingleNode("//" + KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_PAYLOAD_INFO);
+ if (Objects.isNull(droneNode) || Objects.isNull(payloadNode)) {
+ throw new RuntimeException("The file format is incorrect.");
+ }
+
+ String type = droneNode.valueOf(KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_DRONE_ENUM_VALUE);
+ String subType = droneNode.valueOf(KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_DRONE_SUB_ENUM_VALUE);
+ String payloadType = payloadNode.valueOf(KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_PAYLOAD_ENUM_VALUE);
+ String payloadSubType = payloadNode.valueOf(KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_PAYLOAD_SUB_ENUM_VALUE);
+ String templateId = document.valueOf("//" + KmzFileProperties.TAG_WPML_PREFIX + KmzFileProperties.TAG_TEMPLATE_ID);
+
+ if (!StringUtils.hasText(type) || !StringUtils.hasText(subType) ||
+ !StringUtils.hasText(payloadSubType) || !StringUtils.hasText(payloadType) ||
+ !StringUtils.hasText(templateId)) {
+ throw new RuntimeException("The file format is incorrect.");
+ }
+
+ return Optional.of(WaylineFileDTO.builder()
+ .droneModelKey(String.format("%s-%s-%s", DeviceDomainEnum.SUB_DEVICE.getVal(), type, subType))
+ .payloadModelKeys(List.of(String.format("%s-%s-%s",DeviceDomainEnum.PAYLOAD.getVal(), payloadType, payloadSubType)))
+ .objectKey(OssConfiguration.objectDirPrefix + File.separator + filename)
+ .name(filename.substring(0, filename.lastIndexOf(WAYLINE_FILE_SUFFIX)))
+ .sign(DigestUtils.md5DigestAsHex(file.getInputStream()))
+ .templateTypes(List.of(Integer.parseInt(templateId)))
+ .build());
+ }
+
+ } catch (IOException | DocumentException e) {
+ e.printStackTrace();
+ }
+ return Optional.empty();
+ }
/**
* Convert database entity objects into wayline data transfer object.
* @param entity
@@ -192,6 +277,7 @@
.map(String::valueOf)
.collect(Collectors.joining(",")))
.favorited(file.getFavorited())
+ .sign(file.getSign())
.build();
}
--
Gitblit v1.9.3