|
@@ -2,20 +2,26 @@ package com.dtok.api.controller;
|
|
|
|
|
|
import cn.dev33.satoken.annotation.SaCheckRole;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.io.file.FileReader;
|
|
|
import cn.hutool.core.lang.generator.UUIDGenerator;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
+import cn.hutool.http.server.HttpServerResponse;
|
|
|
import com.dtok.entity.Account;
|
|
|
import com.dtok.entity.LoginHistory;
|
|
|
import com.dtok.entity.Script;
|
|
|
import com.dtok.entity.ScriptHistory;
|
|
|
import com.dtok.entity.params.ScriptFormData;
|
|
|
import com.dtok.framework.base.BaseController;
|
|
|
+import com.dtok.framework.core.ScriptSourceManager;
|
|
|
import com.dtok.framework.exception.BizExceptionEnum;
|
|
|
import com.dtok.framework.response.ResponseData;
|
|
|
import com.dtok.framework.util.ToolUtil;
|
|
|
import com.dtok.framework.util.UploadUtils;
|
|
|
+import com.dtok.framework.util.ZipUtils;
|
|
|
import com.dtok.repository.ScriptHistoryRepository;
|
|
|
import com.dtok.repository.ScriptRepository;
|
|
|
+import org.apache.logging.log4j.core.util.IOUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
@@ -27,19 +33,18 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.persistence.Transient;
|
|
|
import javax.persistence.criteria.Order;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.UUID;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/script")
|
|
@@ -53,6 +58,8 @@ public class ScriptController extends BaseController {
|
|
|
@Autowired
|
|
|
ScriptHistoryRepository scriptHistoryRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ScriptSourceManager scriptSourceManager;
|
|
|
|
|
|
@GetMapping("/search")
|
|
|
public ResponseData search(@RequestParam("app") String app, @RequestParam("module") String module) {
|
|
@@ -93,7 +100,6 @@ public class ScriptController extends BaseController {
|
|
|
throw new NullPointerException("APP不能为空");
|
|
|
}
|
|
|
MultipartFile multipartFile = scriptFormData.getFile();
|
|
|
- InputStream inputStream = multipartFile.getInputStream();
|
|
|
Path filepath = Paths.get(UploadUtils.getExcelDirFile(), multipartFile.getOriginalFilename());
|
|
|
try (OutputStream os = Files.newOutputStream(filepath)) {
|
|
|
os.write(multipartFile.getBytes());
|
|
@@ -187,4 +193,62 @@ public class ScriptController extends BaseController {
|
|
|
return ResponseData.success("删除成功");
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("/getScriptZipFile")
|
|
|
+ public void getScriptZipFile(HttpServletResponse httpServerResponse) {
|
|
|
+ List<String> paths = new ArrayList<>();
|
|
|
+ File[] fs = new File(UploadUtils.getScriptDir()).listFiles();
|
|
|
+ for (File f : fs) {
|
|
|
+ if (!f.isDirectory())
|
|
|
+ paths.add(f.getAbsolutePath());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ZipUtils.downLoadFile(httpServerResponse, paths, String.format("script_%s.zip", new Date().getTime()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("/updateScriptByZip")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public ResponseData updateScriptByZip(MultipartFile file) {
|
|
|
+ StringBuilder logContent = new StringBuilder("<p>开始更新...</p>");
|
|
|
+ int count = 0;
|
|
|
+ try {
|
|
|
+ Path filepath = Paths.get(UploadUtils.getExcelDirFile(), file.getOriginalFilename());
|
|
|
+ try (OutputStream os = Files.newOutputStream(filepath)) {
|
|
|
+ os.write(file.getBytes());
|
|
|
+ }
|
|
|
+ try (ZipFile zipFile = new ZipFile(filepath.toFile(),Charset.forName("gbk"))) {
|
|
|
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
|
+ while (entries.hasMoreElements()) {
|
|
|
+
|
|
|
+ ZipEntry entry = entries.nextElement();
|
|
|
+ Optional<Script> scriptOptional = scriptRepository.getScriptByModule(entry.getName().split("\\.")[0]);
|
|
|
+ if (scriptOptional.isPresent()) {
|
|
|
+ InputStream stream = zipFile.getInputStream(entry);
|
|
|
+ InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
|
|
|
+ String content = IOUtils.toString(reader);
|
|
|
+
|
|
|
+ Script script = scriptOptional.get();
|
|
|
+ script.setScriptContent(content);
|
|
|
+ scriptRepository.save(script);
|
|
|
+ count++;
|
|
|
+ logContent.append(String.format("<p style='color:green;'>更新脚本 %s 成功 </p>", script.getModule()));
|
|
|
+ } else {
|
|
|
+ logContent.append(String.format("<p style='color:red;'>未找到脚本 %s </p>", entry.getName().split("\\.")[0]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logContent.append(String.format("<p style='color:red;'>更新失败%s </p> \n", e.getMessage()));
|
|
|
+ return ResponseData.error(logContent.toString());
|
|
|
+ }
|
|
|
+ logContent.append(String.format("<p style='color:green;'>共更新 %s 个脚本 </p>", count));
|
|
|
+ scriptSourceManager.update();
|
|
|
+ return ResponseData.success(logContent.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|