mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
🎉 初始化提交
This commit is contained in:
120
src/main/java/im/zhaojun/common/controller/FileController.java
Normal file
120
src/main/java/im/zhaojun/common/controller/FileController.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package im.zhaojun.common.controller;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.common.config.StorageTypeFactory;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
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 org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
private FileService fileService;
|
||||
|
||||
@Resource
|
||||
private SystemConfigService configService;
|
||||
|
||||
@GetMapping("/filelist")
|
||||
public ResultBean list(String path, String sortBy, boolean descending) throws Exception {
|
||||
List<FileItem> fileItems = fileService.fileList(path);
|
||||
|
||||
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
||||
fileItems.sort((o1, o2) -> {
|
||||
FileTypeEnum o1Type = o1.getType();
|
||||
FileTypeEnum o2Type = o2.getType();
|
||||
|
||||
if (o1Type.equals(o2Type)) {
|
||||
switch (sortBy) {
|
||||
case "name": return o1.getName().compareTo(o2.getName());
|
||||
case "time": return o1.getTime().compareTo(o2.getTime());
|
||||
case "size": return o1.getSize().compareTo(o2.getSize());
|
||||
default: return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
}
|
||||
|
||||
if (o1Type.equals(FileTypeEnum.FOLDER)) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
if (descending) {
|
||||
Collections.reverse(fileItems);
|
||||
}
|
||||
|
||||
return ResultBean.successData(fileItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载链接
|
||||
* @param path 路径
|
||||
* @return 下载链接
|
||||
*/
|
||||
@GetMapping("/downloadUrl")
|
||||
public ResultBean getDownloadUrl(String path) throws Exception {
|
||||
return ResultBean.successData(fileService.getDownloadUrl(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类容, 仅限用于, txt, md, ini 等普通文本文件.
|
||||
* @param path 文件路径
|
||||
* @return 文件内容
|
||||
*/
|
||||
@GetMapping("/getContent")
|
||||
public ResultBean getContent(String path) throws Exception {
|
||||
return ResultBean.successData(fileService.getTextContent(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统配置信息和当前页的标题, 文件头, 文件尾信息
|
||||
* @param path 路径
|
||||
*/
|
||||
@GetMapping("/getConfig")
|
||||
public ResultBean getConfig(String path) throws Exception {
|
||||
SiteConfig config = fileService.getConfig(path);
|
||||
config.setSystemConfig(configService.getSystemConfig());
|
||||
return ResultBean.successData(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新存储策略, 使用 @PostConstruct 注解, 以用于第一次启动时, 根据数据库的配置值, 读取默认的存储策略.
|
||||
*/
|
||||
@PostConstruct
|
||||
@GetMapping("/updateStorageStrategy")
|
||||
public ResultBean updateConfig() {
|
||||
StorageTypeEnum storageStrategy = configService.getSystemConfig().getStorageStrategy();
|
||||
fileService = StorageTypeFactory.getTrafficMode(storageStrategy);
|
||||
return ResultBean.success();
|
||||
}
|
||||
|
||||
@GetMapping("clearCache")
|
||||
public ResultBean clearCache() throws Exception {
|
||||
fileService.clearCache();
|
||||
return ResultBean.success();
|
||||
}
|
||||
|
||||
@GetMapping("/getImageInfo")
|
||||
public ResultBean getImageInfo(String url) throws Exception {
|
||||
return ResultBean.success(fileService.getImageInfo(url));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user