From 2e280e4931aa5290ef9c74cbd2b8661b6a5bc48f Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Mon, 8 Mar 2021 21:12:42 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=88=A0=E9=99=A4=E7=9B=B4=E9=93=BE=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E7=9B=B4=E9=93=BE=20Key=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/home/ShortLinkController.java | 30 +++++++++++++++++-- .../zfile/service/ShortLinkConfigService.java | 6 ++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/im/zhaojun/zfile/controller/home/ShortLinkController.java b/src/main/java/im/zhaojun/zfile/controller/home/ShortLinkController.java index a38be27..51bd7b9 100644 --- a/src/main/java/im/zhaojun/zfile/controller/home/ShortLinkController.java +++ b/src/main/java/im/zhaojun/zfile/controller/home/ShortLinkController.java @@ -9,8 +9,10 @@ import im.zhaojun.zfile.service.ShortLinkConfigService; import im.zhaojun.zfile.service.SystemConfigService; import im.zhaojun.zfile.util.StringUtils; import org.springframework.stereotype.Controller; +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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @@ -33,10 +35,8 @@ public class ShortLinkController { public ResultBean shortLink(String driveId, String path) { SystemConfigDTO systemConfig = systemConfigService.getSystemConfig(); String domain = systemConfig.getDomain(); - // 拼接直链地址. String fullPath = StringUtils.removeDuplicateSeparator("/directlink/" + driveId + path); - ShortLinkConfig shortLinkConfig = shortLinkConfigService.findByUrl(fullPath); if (shortLinkConfig == null) { @@ -71,6 +71,32 @@ public class ShortLinkController { String url = URLUtil.encode(StringUtils.removeDuplicateSeparator(domain + shortLinkConfig.getUrl())); return "redirect:" + url; + } + + + @GetMapping("admin/api/short-link/key") + @ResponseBody + public ResultBean updateShortKey(Integer id, String newKey) { + ShortLinkConfig shortLinkConfig = shortLinkConfigService.findById(id); + if (shortLinkConfig == null) { + throw new RuntimeException("此直链不存在或已失效."); + } + + shortLinkConfig.setKey(newKey); + shortLinkConfigService.save(shortLinkConfig); + return ResultBean.success(); + } + + /** + * 批量删除直链 + */ + @DeleteMapping("admin/api/short-link") + @ResponseBody + public ResultBean batchDelete(@RequestParam("id[]") Integer[] ids) { + for (Integer id : ids) { + shortLinkConfigService.deleteById(id); + } + return ResultBean.success(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/ShortLinkConfigService.java b/src/main/java/im/zhaojun/zfile/service/ShortLinkConfigService.java index b908432..0b53ffc 100644 --- a/src/main/java/im/zhaojun/zfile/service/ShortLinkConfigService.java +++ b/src/main/java/im/zhaojun/zfile/service/ShortLinkConfigService.java @@ -16,6 +16,7 @@ import javax.persistence.criteria.Predicate; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Optional; @Service public class ShortLinkConfigService { @@ -27,6 +28,11 @@ public class ShortLinkConfigService { return shortLinkConfigRepository.findByKey(key); } + public ShortLinkConfig findById(Integer id) { + Optional shortLinkConfigOptional = shortLinkConfigRepository.findById(id); + return shortLinkConfigOptional.orElse(null); + } + public ShortLinkConfig findByUrl(String url) { return shortLinkConfigRepository.findByUrl(url); }