mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
支持在线浏览图片, 文本, 视频, 使用 HistoryAPI 页面不刷新优化用户体验.
This commit is contained in:
@@ -16,7 +16,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -10,6 +10,7 @@ import im.zhaojun.common.model.SiteConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.SystemConfigService;
|
||||
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;
|
||||
@@ -17,6 +18,7 @@ import javax.annotation.Resource;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api")
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
@@ -25,7 +27,7 @@ public class FileController {
|
||||
@Resource
|
||||
private SystemConfigService configService;
|
||||
|
||||
@GetMapping("/filelist")
|
||||
@GetMapping("/list")
|
||||
public ResultBean list(String path, String sortBy, boolean descending) throws Exception {
|
||||
List<FileItem> fileItems = fileService.fileList(URLUtil.decode(path));
|
||||
|
||||
@@ -99,7 +101,7 @@ public class FileController {
|
||||
return ResultBean.success();
|
||||
}
|
||||
|
||||
@GetMapping("clearCache")
|
||||
@GetMapping("/clearCache")
|
||||
public ResultBean clearCache() throws Exception {
|
||||
fileService.clearCache();
|
||||
return ResultBean.success();
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Controller
|
||||
public class IndexController {
|
||||
@@ -14,7 +15,12 @@ public class IndexController {
|
||||
private SystemConfigService systemConfigService;
|
||||
|
||||
@GetMapping("/")
|
||||
public ModelAndView index(ModelAndView modelAndView) {
|
||||
public String index() {
|
||||
return "redirect:/file/";
|
||||
}
|
||||
|
||||
@GetMapping("/file/**")
|
||||
public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {
|
||||
modelAndView.setViewName("index");
|
||||
modelAndView.addObject("systemConfig", systemConfigService.getSystemConfig());
|
||||
return modelAndView;
|
||||
|
||||
@@ -46,6 +46,9 @@ public interface FileService {
|
||||
return siteConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件内容.
|
||||
*/
|
||||
default String getTextContent(String path) throws Exception {
|
||||
return HttpUtil.get(URLDecoder.decode(getDownloadUrl(path), "utf8"));
|
||||
}
|
||||
@@ -68,6 +71,9 @@ public interface FileService {
|
||||
@Cacheable
|
||||
default ImageInfo getImageInfo(String url) throws Exception {
|
||||
url = URLUtil.decode(url);
|
||||
URL urlObject = new URL(url);
|
||||
String originPath = urlObject.getPath();
|
||||
url = url.replace(originPath, URLUtil.encode(originPath));
|
||||
InputStream inputStream = new URL(url).openStream();
|
||||
BufferedImage sourceImg = ImageIO.read(inputStream);
|
||||
return new ImageInfo(sourceImg.getWidth(), sourceImg.getHeight());
|
||||
|
||||
@@ -30,7 +30,7 @@ public class StringUtils {
|
||||
* @param path 路径
|
||||
* @return URL
|
||||
*/
|
||||
public static String concatDomainAndPath(String domain, String path) {
|
||||
public static String concatPath(String domain, String path) {
|
||||
if (path != null && path.length() > 1 && path.charAt(0) != '/') {
|
||||
path = '/' + path;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class LocalController {
|
||||
@GetMapping("/local-download")
|
||||
@ResponseBody
|
||||
public ResponseEntity<FileSystemResource> downAttachment(String fileName) throws IOException {
|
||||
return export(new File(StringUtils.concatDomainAndPath(localService.getFilePath(), URLUtil.decode(fileName))));
|
||||
return export(new File(StringUtils.concatPath(localService.getFilePath(), URLUtil.decode(fileName))));
|
||||
}
|
||||
|
||||
private ResponseEntity<FileSystemResource> export(File file) throws IOException {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package im.zhaojun.local.service;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
@@ -46,7 +47,7 @@ public class LocalService implements FileService {
|
||||
public List<FileItem> fileList(String path) throws Exception {
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
|
||||
String fullPath = StringUtils.concatDomainAndPath(filePath, path);
|
||||
String fullPath = StringUtils.concatPath(filePath, path);
|
||||
|
||||
File file = new File(fullPath);
|
||||
File[] files = file.listFiles();
|
||||
@@ -77,7 +78,7 @@ public class LocalService implements FileService {
|
||||
int port = request.getServerPort();
|
||||
// 项目发布名称
|
||||
String webApp = request.getContextPath();
|
||||
return StringUtils.concatDomainAndPath(networkProtocol + "://" + host + ":" + port + webApp, "local-download?fileName=" + path);
|
||||
return StringUtils.concatPath(networkProtocol + "://" + host + ":" + port + webApp, "local-download?fileName=" + path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,6 +90,11 @@ 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