From 8f17423e1b5af6aa30754562064c8716f0e21fdd Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Sun, 6 Oct 2019 22:38:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E9=A1=B5,=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../im/zhaojun/common/config/RestConfig.java | 7 ++- .../common/constant/ZfileConstant.java | 2 +- .../common/controller/FileController.java | 49 ++++++++----------- .../zhaojun/common/service/FileService.java | 10 ++-- .../zhaojun/common/service/SystemService.java | 6 +-- .../zhaojun/local/service/LocalService.java | 7 +-- 6 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/main/java/im/zhaojun/common/config/RestConfig.java b/src/main/java/im/zhaojun/common/config/RestConfig.java index f7cfd3f..2a63d28 100644 --- a/src/main/java/im/zhaojun/common/config/RestConfig.java +++ b/src/main/java/im/zhaojun/common/config/RestConfig.java @@ -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; } } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/common/constant/ZfileConstant.java b/src/main/java/im/zhaojun/common/constant/ZfileConstant.java index f24b614..62f8269 100644 --- a/src/main/java/im/zhaojun/common/constant/ZfileConstant.java +++ b/src/main/java/im/zhaojun/common/constant/ZfileConstant.java @@ -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"; diff --git a/src/main/java/im/zhaojun/common/controller/FileController.java b/src/main/java/im/zhaojun/common/controller/FileController.java index 76b702e..721630e 100644 --- a/src/main/java/im/zhaojun/common/controller/FileController.java +++ b/src/main/java/im/zhaojun/common/controller/FileController.java @@ -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 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 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())); } } diff --git a/src/main/java/im/zhaojun/common/service/FileService.java b/src/main/java/im/zhaojun/common/service/FileService.java index 3d45082..bfda9de 100644 --- a/src/main/java/im/zhaojun/common/service/FileService.java +++ b/src/main/java/im/zhaojun/common/service/FileService.java @@ -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 diff --git a/src/main/java/im/zhaojun/common/service/SystemService.java b/src/main/java/im/zhaojun/common/service/SystemService.java index 03dfe5b..e95d56d 100644 --- a/src/main/java/im/zhaojun/common/service/SystemService.java +++ b/src/main/java/im/zhaojun/common/service/SystemService.java @@ -27,10 +27,10 @@ public class SystemService { List 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; diff --git a/src/main/java/im/zhaojun/local/service/LocalService.java b/src/main/java/im/zhaojun/local/service/LocalService.java index b32a255..0c1e63d 100644 --- a/src/main/java/im/zhaojun/local/service/LocalService.java +++ b/src/main/java/im/zhaojun/local/service/LocalService.java @@ -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; }