|
@@ -13,7 +13,6 @@ import cc.iotkit.manager.utils.AuthUtil;
|
|
|
import cc.iotkit.model.Paging;
|
|
|
import cc.iotkit.model.UserInfo;
|
|
|
import cc.iotkit.model.protocol.ProtocolComponent;
|
|
|
-import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -25,7 +24,6 @@ import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
@@ -33,6 +31,7 @@ import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
@@ -42,8 +41,8 @@ public class ProtocolController {
|
|
|
@Value("${gateway.function-jar}")
|
|
|
private String functionJar;
|
|
|
|
|
|
- @Value("${spring.servlet.multipart.upload-dir}")
|
|
|
- private String uploadDir;
|
|
|
+ @Value("${component.dir}")
|
|
|
+ private String componentDir;
|
|
|
|
|
|
@Autowired
|
|
|
private ProtocolComponentRepository protocolComponentRepository;
|
|
@@ -57,25 +56,25 @@ public class ProtocolController {
|
|
|
@Autowired
|
|
|
private ComponentManager componentManager;
|
|
|
|
|
|
- private Path fileStorageLocation;
|
|
|
-
|
|
|
- @SneakyThrows
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- this.fileStorageLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
|
|
|
- Files.createDirectories(this.fileStorageLocation);
|
|
|
+ private Path getFilePath(String comId, String type) {
|
|
|
+ return Paths.get(String.format("%s/%s/%s", componentDir, comId, type))
|
|
|
+ .toAbsolutePath().normalize();
|
|
|
}
|
|
|
|
|
|
@PostMapping("/uploadJar")
|
|
|
- public void uploadJar(@RequestParam("file") MultipartFile file) {
|
|
|
+ public String uploadJar(@RequestParam("file") MultipartFile file) {
|
|
|
if (file == null) {
|
|
|
throw new BizException("file is null");
|
|
|
}
|
|
|
-
|
|
|
+ log.info("saving upload jar file:{}", file.getName());
|
|
|
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
|
|
|
try {
|
|
|
- Path targetLocation = this.fileStorageLocation.resolve(fileName);
|
|
|
+ String id = UUID.randomUUID().toString();
|
|
|
+ Path jarFilePath = getFilePath(id, "jar");
|
|
|
+ Files.createDirectories(jarFilePath);
|
|
|
+ Path targetLocation = jarFilePath.resolve(fileName);
|
|
|
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ return id;
|
|
|
} catch (IOException ex) {
|
|
|
throw new BizException("upload jar error", ex);
|
|
|
}
|
|
@@ -83,7 +82,16 @@ public class ProtocolController {
|
|
|
|
|
|
@PostMapping("/addComponent")
|
|
|
public void addComponent(ProtocolComponent component) {
|
|
|
- Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
|
|
|
+ String id = component.getId();
|
|
|
+ if (!StringUtils.hasLength(id)) {
|
|
|
+ throw new BizException("component id is blank");
|
|
|
+ }
|
|
|
+ Path jarPath = getFilePath(id, "jar");
|
|
|
+ if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
|
|
|
+ throw new BizException("component jar file does not exist");
|
|
|
+ }
|
|
|
+
|
|
|
+ Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(id);
|
|
|
if (optComponent.isPresent()) {
|
|
|
throw new BizException("component already exists");
|
|
|
}
|
|
@@ -93,7 +101,6 @@ public class ProtocolController {
|
|
|
throw new BizException("user does not exists");
|
|
|
}
|
|
|
|
|
|
- component.setScript("new (function () {this.decode = function (msg) {return null; };})().decode(msg)");
|
|
|
component.setCreateAt(System.currentTimeMillis());
|
|
|
component.setUid(AuthUtil.getUserId());
|
|
|
protocolComponentRepository.save(component);
|
|
@@ -104,6 +111,15 @@ public class ProtocolController {
|
|
|
|
|
|
@PostMapping("/saveComponent")
|
|
|
public void saveComponent(ProtocolComponent component) {
|
|
|
+ String id = component.getId();
|
|
|
+ if (!StringUtils.hasLength(id)) {
|
|
|
+ throw new BizException("component id is blank");
|
|
|
+ }
|
|
|
+ Path jarPath = getFilePath(id, "jar");
|
|
|
+ if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
|
|
|
+ throw new BizException("component jar file does not exist");
|
|
|
+ }
|
|
|
+
|
|
|
Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
|
|
|
if (!optComponent.isPresent()) {
|
|
|
throw new BizException("the protocol component does not exists");
|
|
@@ -131,7 +147,7 @@ public class ProtocolController {
|
|
|
}
|
|
|
dataOwnerService.checkOwner(component);
|
|
|
ProtocolComponent oldComponent = optComponent.get();
|
|
|
- oldComponent.setScript(component.getScript());
|
|
|
+ oldComponent.setScriptFile(component.getScriptFile());
|
|
|
try {
|
|
|
// gatewayService.saveFunction(oldGateway.getUuid(), oldGateway.getId(),
|
|
|
// "new (function (){" + oldGateway.getScript() + "})()", functionJar);
|
|
@@ -145,6 +161,14 @@ public class ProtocolController {
|
|
|
public void deleteComponent(@PathVariable("id") String id) {
|
|
|
dataOwnerService.checkOwner(protocolComponentRepository, id);
|
|
|
try {
|
|
|
+ Path path = Paths.get(String.format("%s/%s", componentDir, id))
|
|
|
+ .toAbsolutePath().normalize();
|
|
|
+ File file = path.toFile();
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ FileUtils.deleteDirectory(file);
|
|
|
+ } else {
|
|
|
+ FileUtils.delete(file);
|
|
|
+ }
|
|
|
protocolComponentRepository.deleteById(id);
|
|
|
} catch (Throwable e) {
|
|
|
throw new BizException("delete protocol component error", e);
|
|
@@ -176,4 +200,5 @@ public class ProtocolController {
|
|
|
componentManager.stop("123");
|
|
|
componentManager.deRegister("123");
|
|
|
}
|
|
|
+
|
|
|
}
|