mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
优化路径处理, 更有效的利用缓存.
This commit is contained in:
@@ -9,12 +9,14 @@ import im.zhaojun.common.model.ResultBean;
|
||||
import im.zhaojun.common.model.SiteConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.SystemConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -29,7 +31,7 @@ public class FileController {
|
||||
|
||||
@GetMapping("/list")
|
||||
public ResultBean list(String path, String sortBy, boolean descending) throws Exception {
|
||||
List<FileItem> fileItems = fileService.fileList(URLUtil.decode(path));
|
||||
List<FileItem> fileItems = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + URLUtil.decode(path)));
|
||||
|
||||
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
||||
fileItems.sort((o1, o2) -> {
|
||||
@@ -85,7 +87,7 @@ public class FileController {
|
||||
*/
|
||||
@GetMapping("/getConfig")
|
||||
public ResultBean getConfig(String path) throws Exception {
|
||||
SiteConfig config = fileService.getConfig(path);
|
||||
SiteConfig config = fileService.getConfig(URLUtil.decode(path));
|
||||
config.setSystemConfig(configService.getSystemConfig());
|
||||
return ResultBean.successData(config);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package im.zhaojun.common.service;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import im.zhaojun.common.config.CaffeineConfiguration;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.model.AudioInfo;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
import im.zhaojun.common.model.SiteConfig;
|
||||
@@ -36,7 +41,7 @@ public interface FileService {
|
||||
default SiteConfig getConfig(String path) throws Exception {
|
||||
path = StringUtils.removeLastSeparator(path);
|
||||
SiteConfig siteConfig = new SiteConfig();
|
||||
for (FileItem fileItem : fileList(path)) {
|
||||
for (FileItem fileItem : fileList(StringUtils.removeDuplicateSeparator("/" + path + "/"))) {
|
||||
if ("readme.md".equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setFooter(getTextContent(path + "/" + fileItem.getName()));
|
||||
} else if ("header.md".equalsIgnoreCase(fileItem.getName())) {
|
||||
@@ -50,7 +55,7 @@ public interface FileService {
|
||||
* 获取文件内容.
|
||||
*/
|
||||
default String getTextContent(String path) throws Exception {
|
||||
return HttpUtil.get(URLDecoder.decode(getDownloadUrl(path), "utf8"));
|
||||
return HttpUtil.get(URLUtil.decode(getDownloadUrl(path)));
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
||||
@@ -41,4 +41,29 @@ public class StringUtils {
|
||||
|
||||
return domain + path;
|
||||
}
|
||||
|
||||
public static String removeDuplicateSeparator(String path) {
|
||||
if (path == null || path.length() < 2) {
|
||||
return path;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (path.indexOf("http://") == 0) {
|
||||
sb.append("http://");
|
||||
} else if (path.indexOf("https://") == 0) {
|
||||
sb.append("http://");
|
||||
}
|
||||
|
||||
for (int i = sb.length(); i < path.length() - 1; i++) {
|
||||
char current = path.charAt(i);
|
||||
char next = path.charAt(i + 1);
|
||||
if (!(current == '/' && next == '/')) {
|
||||
sb.append(current);
|
||||
}
|
||||
}
|
||||
sb.append(path.charAt(path.length() - 1));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user