update 优化生成短链接标识符识别

This commit is contained in:
AprilWind 2024-04-12 14:44:08 +08:00
parent bf6102cf64
commit df6641a659
5 changed files with 16 additions and 21 deletions

View File

@ -195,7 +195,7 @@ sms:
sdk-app-id: 您的sdkAppId
shortlink:
enabled: true
enabled: false
api: /dev-api
address: http://localhost/dev-api/resource/short/link?shortUrl=
errorAddress: http://localhost/error

View File

@ -126,7 +126,7 @@ security:
# EasyRetry Job 分派 @see com.aizuda.easy.retry.client.job.core.client.JobEndPoint
- /job/dispatch/v1
- /job/stop/v1
- /resource/short/link
- /resource/short/link/*
# 多租户配置
tenant:

View File

@ -30,6 +30,6 @@ public class ShortLinkProperties {
/**
* 错误页面的主机名或域名
*/
private String errorAddress="/error";
private String errorAddress = "/error";
}

View File

@ -78,22 +78,22 @@ public class ShortLinkUtils {
/**
* 根据短链接获取对应的完整长链接如果找不到长链接则返回错误链接
*
* @param shortUrl 短链接标识符
* @param shortLinkIdentifier 短链接标识符
* @return 对应的完整长链接如果未找到则返回错误链接
*/
public static String getLongLink(String shortUrl) {
return getLongLink(shortUrl, SHORT_LINK_PROPERTIES.getErrorAddress());
public static String getLongLink(String shortLinkIdentifier) {
return getLongLink(shortLinkIdentifier, SHORT_LINK_PROPERTIES.getErrorAddress());
}
/**
* 根据短链接获取对应的完整长链接如果找不到长链接则返回指定的错误链接
*
* @param shortUrl 短链接标识符
* @param shortLinkIdentifier 短链接标识符
* @param errUrl 错误链接
* @return 对应的完整长链接如果未找到则返回错误链接
*/
public static String getLongLink(String shortUrl, String errUrl) {
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl);
public static String getLongLink(String shortLinkIdentifier, String errUrl) {
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortLinkIdentifier);
return StringUtils.isNotEmpty(longLink) ? longLink : errUrl;
}

View File

@ -8,10 +8,7 @@ import org.dromara.common.shortlink.enums.ValidityType;
import org.dromara.common.shortlink.properties.ShortLinkProperties;
import org.dromara.common.shortlink.utils.ShortLinkUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
import java.net.MalformedURLException;
@ -31,12 +28,12 @@ public class SysShortLinkController {
/**
* 根据短链接获取长链接并重定向
*
* @param shortUrl 短链接
* @param shortLinkIdentifier 短链接标识符
* @return 重定向视图
*/
@GetMapping("/link")
public RedirectView getLongLink(@NotBlank(message = "参数不能为空") String shortUrl) {
String longLink = ShortLinkUtils.getLongLink(shortUrl, shortLinkProperties.getErrorAddress());
@GetMapping("/link/{shortLinkIdentifier}")
public RedirectView getLongLink(@PathVariable("shortLinkIdentifier") String shortLinkIdentifier) {
String longLink = ShortLinkUtils.getLongLink(shortLinkIdentifier, shortLinkProperties.getErrorAddress());
return new RedirectView(longLink);
}
@ -52,11 +49,9 @@ public class SysShortLinkController {
if (Boolean.FALSE.equals(shortLinkProperties.getEnabled())) {
shorturl = ShortLinkUtils.generateShortUrl(shortLinkProperties.getAddress(), longUrl, ValidityType.THREE_DAYS);
} else {
String requestUrl = request.getRequestURL().toString();
String requestUrl = request.getRequestURL().toString().replace("shorturl", "link/");
String host = new URL(requestUrl).getHost();
String address = requestUrl
.replace(host, host + shortLinkProperties.getApi())
.replace("shorturl", "link?shortUrl=");
String address = requestUrl.replace(host, host + shortLinkProperties.getApi());
shorturl = ShortLinkUtils.generateShortUrl(address, longUrl, ValidityType.THREE_DAYS);
}
return R.ok(shorturl);