From f76d121f0c64c4b64fd1edb0b9d6931d68d6f8bf Mon Sep 17 00:00:00 2001 From: huangfeng Date: Mon, 23 Jun 2025 11:17:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E4=BC=A0=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=90=8E=E5=AF=B9=E6=AF=8F=E4=B8=AA=E6=97=A7=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=88=B6=E4=BD=9C=E8=A1=A5=E4=B8=81=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UpgradeController.java | 33 ++++++- .../src/main/resources/application-test.yml | 1 + .../impl/UpgradePatchServiceImpl.java | 98 +++++++++++++++++++ .../impl/UpgradeServiceImpl.java | 6 +- .../service/UpgradePatchService.java | 14 +++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradePatchServiceImpl.java create mode 100644 xymanager_service/src/main/java/com/shxy/xymanager_service/service/UpgradePatchService.java diff --git a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/UpgradeController.java b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/UpgradeController.java index 3918b7c..10b8374 100644 --- a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/UpgradeController.java +++ b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/UpgradeController.java @@ -3,7 +3,9 @@ package com.shxy.xymanager_admin.controller; import com.shxy.xymanager_common.base.BaseController; import com.shxy.xymanager_common.base.ResponseReult; import com.shxy.xymanager_common.entity.MntnUpgrades; +import com.shxy.xymanager_common.entity.MntnUpgradesPatch; import com.shxy.xymanager_common.exception.ApiException; +import com.shxy.xymanager_service.service.UpgradePatchService; import com.shxy.xymanager_service.service.UpgradeService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -16,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.validation.constraints.NotNull; import java.util.List; +import java.util.regex.Pattern; @RestController @Api(tags = {"升级相关接口"}) @@ -25,6 +28,8 @@ public class UpgradeController extends BaseController { @Resource UpgradeService service; + @Resource + UpgradePatchService patchService; @GetMapping("listAll") @ApiOperation("查询全部列表") @@ -33,11 +38,21 @@ public class UpgradeController extends BaseController { return ResponseReult.success(result); } + @GetMapping("listPatch") + @ApiOperation("查询补丁列表") + public ResponseReult> listAll(@RequestParam(value = "app", required = true) String app, + @RequestParam(value = "version", required = false) String version) { + List result = patchService.list(app, version); + return ResponseReult.success(result); + } + @PostMapping("upload") @ApiOperation("上传") public ResponseReult upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "title", required = false) String title, - @RequestParam(value = "type", required = true) Integer type) throws Exception { + @RequestParam(value = "type", required = true) Integer type, + @RequestParam(value = "app", required = false) String app, + @RequestParam(value = "version", required = false) String version) throws Exception { if (file == null) { throw new ApiException("缺少上传的文件"); } @@ -47,10 +62,26 @@ public class UpgradeController extends BaseController { MntnUpgrades record = new MntnUpgrades(); record.setTitle(title); record.setType(type); + if (StringUtils.isNotBlank(app)) { + record.setApp(app); + if (StringUtils.isBlank(version)) { + throw new ApiException("app有值时version不能为空"); + } + if (!this.isValidFormat(version)) { + throw new ApiException("version格式不匹配"); + } + record.setVersion(version); + } service.upload(record, file); return ResponseReult.success("OK"); } + private boolean isValidFormat(String input) { + // 正则表达式:匹配三段数字,由两个点分隔 + String regex = "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$"; + return Pattern.matches(regex, input); + } + @PostMapping("updateTitle") @ApiOperation("修改") public ResponseReult updateTitle(@Validated @RequestBody MntnUpgrades item) throws Exception { diff --git a/xymanager_admin/src/main/resources/application-test.yml b/xymanager_admin/src/main/resources/application-test.yml index 25e310c..5210a3b 100644 --- a/xymanager_admin/src/main/resources/application-test.yml +++ b/xymanager_admin/src/main/resources/application-test.yml @@ -100,6 +100,7 @@ swagger2: maintain: termlog: /home/xymp/termLogs/ apk: /home/xymp/apk/ + patch: /home/xymp/patch/ uri: http://61.169.135.146:40085/ check: false diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradePatchServiceImpl.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradePatchServiceImpl.java new file mode 100644 index 0000000..abe518b --- /dev/null +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradePatchServiceImpl.java @@ -0,0 +1,98 @@ +package com.shxy.xymanager_service.impl; + +import com.shxy.xymanager_common.entity.MntnUpgrades; +import com.shxy.xymanager_common.entity.MntnUpgradesExample; +import com.shxy.xymanager_common.entity.MntnUpgradesPatch; +import com.shxy.xymanager_common.entity.MntnUpgradesPatchExample; +import com.shxy.xymanager_dao.dao.MntnUpgradesMapper; +import com.shxy.xymanager_dao.dao.MntnUpgradesPatchMapper; +import com.shxy.xymanager_service.service.UpgradePatchService; +import io.sigpipe.jbsdiff.Diff; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import javax.annotation.Resource; +import java.io.File; +import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +@Service +@Slf4j +public class UpgradePatchServiceImpl implements UpgradePatchService { + + @Value("${maintain.apk}") + private String apkpath; + @Value("${maintain.patch}") + private String patchpath; + @Value("${maintain.uri}") + private String uri; + + @Resource + MntnUpgradesMapper upgradesMapper; + @Resource + MntnUpgradesPatchMapper patchMapper; + + @Override + @Async + public void processPatch(MntnUpgrades upg) throws Exception { + if (StringUtils.isBlank(upg.getApp()) || StringUtils.isBlank(upg.getVersion())) { + return; + } + String app = upg.getApp(); + String ver = upg.getVersion(); + + MntnUpgradesExample example = new MntnUpgradesExample(); + MntnUpgradesExample.Criteria criteria = example.createCriteria(); + criteria.andAppEqualTo(app); + criteria.andVersionNotEqualTo(ver); + example.setOrderByClause("id asc"); + List list = upgradesMapper.selectByExample(example); + if (CollectionUtils.isEmpty(list)) { + return; + } + + byte[] newIn = Files.readAllBytes(Paths.get(apkpath + upg.getPath())); + for (MntnUpgrades item : list) { + if (StringUtils.isNotBlank(item.getVersion())) { + String oldpath = apkpath + item.getPath(); + try { + byte[] oldIn = Files.readAllBytes(Paths.get(oldpath)); + String filename = app + "_" + item.getVersion() + "_" + ver + ".patch"; + FileOutputStream fos = new FileOutputStream(new File(patchpath + filename)); + Diff.diff(oldIn, newIn, fos); + log.info("生成" + filename); + long fileSize = Files.size(Paths.get(patchpath + filename)); + MntnUpgradesPatch record = new MntnUpgradesPatch(); + record.setApp(app); + record.setOldVer(item.getVersion()); + record.setNewVer(ver); + record.setPath(filename); + record.setFileSize((int) fileSize); + patchMapper.insert(record); + } catch (Exception ignore) { + } + } + } + } + + @Override + public List list(String app, String oldver) { + MntnUpgradesPatchExample example = new MntnUpgradesPatchExample(); + MntnUpgradesPatchExample.Criteria criteria = example.createCriteria(); + criteria.andAppEqualTo(app); + if (StringUtils.isNotBlank(oldver)) { + criteria.andOldVerEqualTo(oldver); + } + List list = patchMapper.selectByExample(example); + for (MntnUpgradesPatch item : list) { + item.setPath(uri + "patch/" + item.getPath()); + } + return list; + } +} diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java index 553e741..ed8edb1 100644 --- a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java @@ -7,6 +7,7 @@ import com.shxy.xymanager_common.exception.ApiException; import com.shxy.xymanager_common.util.DateUtil; import com.shxy.xymanager_common.util.DigestUtils; import com.shxy.xymanager_dao.dao.MntnUpgradesMapper; +import com.shxy.xymanager_service.service.UpgradePatchService; import com.shxy.xymanager_service.service.UpgradeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -34,6 +35,8 @@ public class UpgradeServiceImpl implements UpgradeService { @Resource MntnUpgradesMapper mapper; + @Resource + UpgradePatchService patchService; @Override public List listAll(Integer type) { @@ -69,7 +72,8 @@ public class UpgradeServiceImpl implements UpgradeService { file.transferTo(dest); String md5 = DigestUtils.md5(file.getBytes()); item.setMd5(md5); - mapper.insert(item); + mapper.insertSelective(item); + patchService.processPatch(item); return true; } diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/service/UpgradePatchService.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/service/UpgradePatchService.java new file mode 100644 index 0000000..6dcbb7a --- /dev/null +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/service/UpgradePatchService.java @@ -0,0 +1,14 @@ +package com.shxy.xymanager_service.service; + +import com.shxy.xymanager_common.entity.MntnUpgrades; +import com.shxy.xymanager_common.entity.MntnUpgradesPatch; + +import java.util.List; + +public interface UpgradePatchService { + + void processPatch(MntnUpgrades upg) throws Exception; + + List list(String app, String oldver); + +}