From 89c6c515f17049b2db66379e3076da0a6c36f3ea Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Sun, 5 Mar 2023 15:26:22 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E9=81=BF=E5=85=8D=E6=9C=AA=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=B9=B6=E5=8F=91=E7=94=9F=E6=88=90=E7=9F=AD=E9=93=BE?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=94=9F=E6=88=90=E5=A4=9A=E6=9D=A1=E7=9A=84?= =?UTF-8?q?=20BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../link/controller/ShortLinkController.java | 27 +++++++++++-------- src/main/resources/mapper/ShortLinkMapper.xml | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/im/zhaojun/zfile/module/link/controller/ShortLinkController.java b/src/main/java/im/zhaojun/zfile/module/link/controller/ShortLinkController.java index ec6c366..c6effb9 100644 --- a/src/main/java/im/zhaojun/zfile/module/link/controller/ShortLinkController.java +++ b/src/main/java/im/zhaojun/zfile/module/link/controller/ShortLinkController.java @@ -54,40 +54,45 @@ public class ShortLinkController { @Resource private StorageSourceService storageSourceService; + private final Object lock = new Object(); + @PostMapping("/api/short-link/batch/generate") @ResponseBody @ApiOperationSupport(order = 1) @ApiOperation(value = "生成短链", notes = "对指定存储源的某文件路径生成短链") public AjaxJson> generatorShortLink(@RequestBody @Valid BatchGenerateLinkRequest batchGenerateLinkRequest) { List result = new ArrayList<>(); - + // 获取站点域名 SystemConfigDTO systemConfig = systemConfigService.getSystemConfig(); - + // 是否允许使用短链和短链,如果都不允许,则提示禁止生成. Boolean showShortLink = systemConfig.getShowShortLink(); Boolean showPathLink = systemConfig.getShowPathLink(); if ( BooleanUtil.isFalse(showShortLink) && BooleanUtil.isFalse(showPathLink)) { throw new IllegalDownloadLinkException("当前系统不允许使用直链和短链."); } - + String domain = systemConfig.getDomain(); String storageKey = batchGenerateLinkRequest.getStorageKey(); Integer storageId = storageSourceService.findIdByKey(storageKey); - + for (String path : batchGenerateLinkRequest.getPaths()) { // 拼接全路径地址. String fullPath = StringUtils.concat(path); - - // 如果没有短链,则生成短链 - ShortLink shortLink = shortLinkService.findByStorageIdAndUrl(storageId, fullPath); - if (shortLink == null) { - shortLink = shortLinkService.generatorShortLink(storageId, fullPath); + + ShortLink shortLink; + synchronized (lock) { + // 如果没有短链,则生成短链 + shortLink = shortLinkService.findByStorageIdAndUrl(storageId, fullPath); + if (shortLink == null) { + shortLink = shortLinkService.generatorShortLink(storageId, fullPath); + } } - + String shortUrl = StringUtils.removeDuplicateSlashes(domain + "/s/" + shortLink.getShortKey()); String pathLink = StringUtils.generatorPathLink(storageKey, fullPath); - + result.add(new BatchGenerateLinkResponse(shortUrl, pathLink)); } return AjaxJson.getSuccessData(result); diff --git a/src/main/resources/mapper/ShortLinkMapper.xml b/src/main/resources/mapper/ShortLinkMapper.xml index bd53643..b7fa673 100644 --- a/src/main/resources/mapper/ShortLinkMapper.xml +++ b/src/main/resources/mapper/ShortLinkMapper.xml @@ -32,7 +32,7 @@ select from short_link - where url=#{url,jdbcType=LONGVARCHAR} and storage_id=#{storageId,jdbcType=INTEGER} + where url=#{url,jdbcType=LONGVARCHAR} and storage_id=#{storageId,jdbcType=INTEGER} limit 1