mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
添加分页, 修复本地存储下载错误
This commit is contained in:
@@ -2,13 +2,18 @@ package im.zhaojun.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Configuration
|
||||
public class RestConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(){
|
||||
return new RestTemplate();
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
||||
return restTemplate;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ public class ZfileConstant {
|
||||
|
||||
public static final String HEADER_FILE_NAME = "header.md";
|
||||
|
||||
public static final String README_FILE_NAME = "readme.md";
|
||||
public static final String FOOTER_FILE_NAME = "footer.md";
|
||||
|
||||
public static final String PASSWORD_FILE_NAME = "password.txt";
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api")
|
||||
@@ -39,18 +40,19 @@ public class FileController {
|
||||
@Resource
|
||||
private ViewConfigService viewConfigService;
|
||||
|
||||
public static final Integer PAGE_SIZE = 20;
|
||||
|
||||
@GetMapping("/list")
|
||||
public ResultBean list(@RequestParam(defaultValue = "/") String path,
|
||||
@RequestParam(defaultValue = "name") String sortBy,
|
||||
@RequestParam(defaultValue = "asc") String order,
|
||||
@RequestParam(required = false) String password) throws Exception {
|
||||
@RequestParam(required = false) String password,
|
||||
@RequestParam(defaultValue = "1") Integer page) throws Exception {
|
||||
List<FileItem> fileItems = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + URLUtil.decode(path)));
|
||||
|
||||
for (FileItem fileItem : fileItems) {
|
||||
if (ZfileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())) {
|
||||
String url = StringUtils.removeDuplicateSeparator("/" + fileItem.getPath() + "/" + fileItem.getName());
|
||||
|
||||
if (!fileService.getTextContent(url).equals(password)) {
|
||||
if (!fileService.getTextContent(fileItem.getUrl()).equals(password)) {
|
||||
if (password != null && !"".equals(password)) {
|
||||
return ResultBean.error("密码错误.");
|
||||
}
|
||||
@@ -62,35 +64,29 @@ public class FileController {
|
||||
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
||||
fileItems.sort(new FileComparator(sortBy, order));
|
||||
filterFileList(fileItems);
|
||||
return ResultBean.successData(fileItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载链接
|
||||
*
|
||||
* @param path 路径
|
||||
* @param name 文件名称
|
||||
* @return 下载链接
|
||||
*/
|
||||
@GetMapping("/downloadUrl")
|
||||
public ResultBean getDownloadUrl(@RequestParam String path,
|
||||
@RequestParam String name) throws Exception {
|
||||
path = URLUtil.decode(path);
|
||||
name = URLUtil.decode(name);
|
||||
Integer total = fileItems.size();
|
||||
Integer totalPage = (total + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||
|
||||
String fullPath = StringUtils.concatURL(path, name);
|
||||
String downloadUrl = fileService.getDownloadUrl(fullPath);
|
||||
return ResultBean.successData(downloadUrl);
|
||||
if (page > totalPage) {
|
||||
return ResultBean.successData(new ArrayList<>());
|
||||
}
|
||||
|
||||
Integer start = (page - 1) * PAGE_SIZE;
|
||||
Integer end = page * PAGE_SIZE;
|
||||
end = end > total ? total : end;
|
||||
List<FileItem> fileSubItem = fileItems.subList(start, end);
|
||||
return ResultBean.successData(fileSubItem);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类容, 仅限用于, txt, md, ini 等普通文本文件.
|
||||
* @param path 文件路径
|
||||
* @param url 文件路径
|
||||
* @return 文件内容
|
||||
*/
|
||||
@GetMapping("/content")
|
||||
public ResultBean getContent(String path) throws Exception {
|
||||
return ResultBean.successData(fileService.getTextContent(path));
|
||||
public ResultBean getContent(String url) throws Exception {
|
||||
return ResultBean.successData(fileService.getTextContent(url));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,9 +133,6 @@ public class FileController {
|
||||
|
||||
@GetMapping("/getImageInfo")
|
||||
public ResultBean getImageInfo(String url) throws Exception {
|
||||
if (url != null && url.indexOf("//") == 0) {
|
||||
url = "http:" + url;
|
||||
}
|
||||
return ResultBean.success(fileService.getImageInfo(url));
|
||||
}
|
||||
|
||||
@@ -167,7 +160,7 @@ public class FileController {
|
||||
}
|
||||
|
||||
fileItemList.removeIf(fileItem -> ZfileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())
|
||||
|| ZfileConstant.README_FILE_NAME.equals(fileItem.getName())
|
||||
|| ZfileConstant.FOOTER_FILE_NAME.equals(fileItem.getName())
|
||||
|| ZfileConstant.HEADER_FILE_NAME.equals(fileItem.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,19 @@ import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import im.zhaojun.common.config.ZfileCacheConfiguration;
|
||||
import im.zhaojun.common.constant.ZfileConstant;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.AudioInfo;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
import im.zhaojun.common.model.SiteConfig;
|
||||
import im.zhaojun.common.util.AudioHelper;
|
||||
import im.zhaojun.common.util.SpringContextHolder;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -41,8 +41,10 @@ public interface FileService {
|
||||
/**
|
||||
* 获取文件内容.
|
||||
*/
|
||||
default String getTextContent(String path) throws Exception {
|
||||
return HttpUtil.get(URLUtil.decode(getDownloadUrl(path)));
|
||||
default String getTextContent(String url) throws Exception {
|
||||
RestTemplate restTemplate = SpringContextHolder.getBean(RestTemplate.class);
|
||||
String result = restTemplate.getForObject(url, String.class);
|
||||
return result == null ? "" : result;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
||||
@@ -27,10 +27,10 @@ public class SystemService {
|
||||
List<FileItem> fileItemList = fileService.fileList(path);
|
||||
path = StringUtils.removeLastSeparator(path);
|
||||
for (FileItem fileItem : fileItemList) {
|
||||
if (ZfileConstant.README_FILE_NAME.equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setFooter(fileService.getTextContent(path + "/" + fileItem.getName()));
|
||||
if (ZfileConstant.FOOTER_FILE_NAME.equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setFooter(fileService.getTextContent(fileItem.getUrl()));
|
||||
} else if (ZfileConstant.HEADER_FILE_NAME.equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setHeader(fileService.getTextContent(path + "/" + fileItem.getName()));
|
||||
siteConfig.setHeader(fileService.getTextContent(fileItem.getUrl()));
|
||||
}
|
||||
}
|
||||
return siteConfig;
|
||||
|
||||
@@ -84,7 +84,7 @@ public class LocalService implements FileService {
|
||||
public String getDownloadUrl(String path) throws Exception {
|
||||
InetAddress localHost = Inet4Address.getLocalHost();
|
||||
String host = localHost.getHostAddress();
|
||||
return StringUtils.concatPath( "//" + host + ":" + port + contextPath, "local-download?fileName=" + path);
|
||||
return StringUtils.concatPath( "http://" + host + ":" + port + contextPath, "local-download?fileName=" + path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,11 +96,6 @@ public class LocalService implements FileService {
|
||||
return new ImageInfo(sourceImg.getWidth(), sourceImg.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTextContent(String path) throws Exception {
|
||||
return FileUtil.readUtf8String(StringUtils.concatPath(filePath, URLUtil.decode(path)));
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user