mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
✨ 优化下载地址获取逻辑,直接列表时不直接调用 API 获取下载地址,访问单文件时再调用。
This commit is contained in:
@@ -73,7 +73,11 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService
|
||||
fileItemDTO.setTime(s.getLastModified());
|
||||
fileItemDTO.setType(FileTypeEnum.FILE);
|
||||
fileItemDTO.setPath(path);
|
||||
fileItemDTO.setUrl(getDownloadUrl(StringUtils.concatUrl(path, fileItemDTO.getName())));
|
||||
|
||||
String fullPathAndName = StringUtils.concatUrl(path, fileItemDTO.getName());
|
||||
String directlink = StringUtils.generatorLink(driveId, fullPathAndName);
|
||||
fileItemDTO.setUrl(directlink);
|
||||
|
||||
fileItemList.add(fileItemDTO);
|
||||
}
|
||||
|
||||
@@ -129,6 +133,7 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService
|
||||
for (FileItemDTO fileItemDTO : list) {
|
||||
String fullPath = StringUtils.concatUrl(fileItemDTO.getPath(), fileItemDTO.getName());
|
||||
if (Objects.equals(fullPath, path)) {
|
||||
fileItemDTO.setUrl(getDownloadUrl(path));
|
||||
return fileItemDTO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package im.zhaojun.zfile.util;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||
import im.zhaojun.zfile.service.SystemConfigService;
|
||||
|
||||
/**
|
||||
* @author zhaojun
|
||||
@@ -114,4 +115,32 @@ public class StringUtils {
|
||||
public static String replaceHost(String originUrl, String replaceHost) {
|
||||
return concatPath(replaceHost, URLUtil.getPath(originUrl));
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接 URL,并去除重复的分隔符 '/',但不会影响 http:// 和 https:// 这种头部
|
||||
* @param strs 拼接的字符数组
|
||||
* @return 拼接结果
|
||||
*/
|
||||
public static String concatUrl(String... strs) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
sb.append(strs[i]);
|
||||
if (i != strs.length - 1) {
|
||||
sb.append(DELIMITER);
|
||||
}
|
||||
}
|
||||
return removeDuplicateSeparator(sb.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接文件直链生成 URL
|
||||
* @param driveId 驱动器 ID
|
||||
* @param fullPath 文件全路径
|
||||
* @return 生成结果
|
||||
*/
|
||||
public static String generatorLink(Integer driveId, String fullPath) {
|
||||
SystemConfigService systemConfigService = SpringContextHolder.getBean(SystemConfigService.class);
|
||||
String domain = systemConfigService.getDomain();
|
||||
return concatUrl(domain, "directlink", String.valueOf(driveId), fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user