From 0d73ee1537581d716fd8d2deb4086637d08e7c0d Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 22 Apr 2025 15:41:48 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=B8=80=E4=B8=AA=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E9=93=BE=E6=8E=A5=E6=9C=80=E5=A4=9A=E5=8F=AA?= =?UTF-8?q?=E8=83=BD=E4=B8=8B=E8=BD=BD3=E6=AC=A1=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E7=9B=97=E9=93=BE=E5=92=8C=E6=B5=81=E9=87=8F=E6=94=BB?= =?UTF-8?q?=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xiaozhi/common/redis/RedisKeys.java | 7 +++ .../device/controller/OTAMagController.java | 57 ++++++++++++------- .../modules/security/config/ShiroConfig.java | 1 + main/manager-web/src/apis/module/ota.js | 16 ++++++ main/manager-web/src/views/OtaManagement.vue | 21 +++---- 5 files changed, 69 insertions(+), 33 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java index 6f6e3ee94..5d3b4f32a 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java +++ b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java @@ -89,4 +89,11 @@ public class RedisKeys { public static String getOtaIdKey(String uuid) { return "ota:id:" + uuid; } + + /** + * OTA固件下载次数的Key + */ + public static String getOtaDownloadCountKey(String uuid) { + return "ota:download:count:" + uuid; + } } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAMagController.java b/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAMagController.java index ac1bab40f..18273a95c 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAMagController.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAMagController.java @@ -8,6 +8,7 @@ import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; +import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; @@ -21,7 +22,6 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -36,6 +36,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import xiaozhi.common.constant.Constant; import xiaozhi.common.page.PageData; +import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisUtils; import xiaozhi.common.utils.Result; import xiaozhi.common.validator.ValidatorUtils; @@ -97,22 +98,6 @@ public class OTAMagController { } } - @PutMapping("/{id}") - @Operation(summary = "修改 OTA 固件信息") - @RequiresPermissions("sys:role:superAdmin") - public Result update(@PathVariable("id") String id, @RequestBody OtaEntity entity) { - if (entity == null) { - return new Result<>().error("固件信息不能为空"); - } - entity.setId(id); - try { - otaService.update(entity); - return new Result<>(); - } catch (RuntimeException e) { - return new Result<>().error(e.getMessage()); - } - } - @DeleteMapping("/{id}") @Operation(summary = "OTA 删除") @RequiresPermissions("sys:role:superAdmin") @@ -124,10 +109,40 @@ public class OTAMagController { return new Result(); } - @GetMapping("/download/{id}") - @Operation(summary = "下载固件文件") + @GetMapping("/getDownloadUrl/{id}") + @Operation(summary = "获取 OTA 固件下载链接") @RequiresPermissions("sys:role:superAdmin") - public ResponseEntity downloadFirmware(@PathVariable("id") String id) { + public Result getDownloadUrl(@PathVariable("id") String id) { + String uuid = UUID.randomUUID().toString(); + redisUtils.set(RedisKeys.getOtaIdKey(uuid), id); + return new Result().ok(uuid); + } + + @GetMapping("/download/{uuid}") + @Operation(summary = "下载固件文件") + public ResponseEntity downloadFirmware(@PathVariable("uuid") String uuid) { + String id = (String) redisUtils.get(RedisKeys.getOtaIdKey(uuid)); + if (StringUtils.isBlank(id)) { + return ResponseEntity.notFound().build(); + } + + // 检查下载次数 + String downloadCountKey = RedisKeys.getOtaDownloadCountKey(uuid); + Integer downloadCount = (Integer) redisUtils.get(downloadCountKey); + if (downloadCount == null) { + downloadCount = 0; + } + + // 如果下载次数超过3次,返回404 + if (downloadCount >= 3) { + redisUtils.delete(downloadCountKey); + redisUtils.delete(RedisKeys.getOtaIdKey(uuid)); + logger.warn("Download limit exceeded for UUID: {}", uuid); + return ResponseEntity.notFound().build(); + } + + redisUtils.set(downloadCountKey, downloadCount + 1); + try { // 获取固件信息 OtaEntity otaEntity = otaService.selectById(id); @@ -171,7 +186,7 @@ public class OTAMagController { byte[] fileContent = Files.readAllBytes(path); // 设置响应头 - String originalFilename = otaEntity.getFirmwareName() + "_" + otaEntity.getVersion(); + String originalFilename = otaEntity.getType() + "_" + otaEntity.getVersion(); if (firmwarePath.contains(".")) { String extension = firmwarePath.substring(firmwarePath.lastIndexOf(".")); originalFilename += extension; diff --git a/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java b/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java index fdcb62b20..6e68ce06a 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java @@ -69,6 +69,7 @@ public class ShiroConfig { */ Map filterMap = new LinkedHashMap<>(); filterMap.put("/ota/**", "anon"); + filterMap.put("/otaMag/download/**", "anon"); filterMap.put("/webjars/**", "anon"); filterMap.put("/druid/**", "anon"); filterMap.put("/v3/api-docs/**", "anon"); diff --git a/main/manager-web/src/apis/module/ota.js b/main/manager-web/src/apis/module/ota.js index ae0ca81a6..438aa3d8e 100644 --- a/main/manager-web/src/apis/module/ota.js +++ b/main/manager-web/src/apis/module/ota.js @@ -103,5 +103,21 @@ export default { this.uploadFirmware(file, callback); }); }).send(); + }, + // 获取固件下载链接 + getDownloadUrl(id, callback) { + RequestService.sendRequest() + .url(`${getServiceUrl()}/otaMag/getDownloadUrl/${id}`) + .method('GET') + .success((res) => { + RequestService.clearRequestTime(); + callback(res); + }) + .fail((err) => { + console.error('获取下载链接失败:', err); + RequestService.reAjaxFun(() => { + this.getDownloadUrl(id, callback); + }); + }).send(); } } \ No newline at end of file diff --git a/main/manager-web/src/views/OtaManagement.vue b/main/manager-web/src/views/OtaManagement.vue index 98540e9a2..13ffacf9a 100644 --- a/main/manager-web/src/views/OtaManagement.vue +++ b/main/manager-web/src/views/OtaManagement.vue @@ -361,18 +361,15 @@ export default { this.$message.error('固件信息不完整'); return; } - // 使用正确的API基础URL变量 - const baseUrl = process.env.VUE_APP_API_BASE_URL || ''; - window.open(`${window.location.origin}${baseUrl}/ota/download/${firmware.id}`); - }, - downloadSelectedFirmwares() { - const selectedFirmwares = this.firmwareList.filter(item => item.selected); - if (selectedFirmwares.length === 0) { - this.$message.warning('请先选择需要下载的固件'); - return; - } - selectedFirmwares.forEach(firmware => { - this.downloadFirmware(firmware); + // 先获取下载链接 + Api.ota.getDownloadUrl(firmware.id, (res) => { + if (res.data.code === 0) { + const uuid = res.data.data; + const baseUrl = process.env.VUE_APP_API_BASE_URL || ''; + window.open(`${window.location.origin}${baseUrl}/otaMag/download/${uuid}`); + } else { + this.$message.error('获取下载链接失败'); + } }); }, formatDate,