mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
添加注释, 优化代码.
This commit is contained in:
@@ -10,9 +10,6 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 存储类型工厂类
|
||||
*/
|
||||
@Component
|
||||
public class StorageTypeFactory implements ApplicationContextAware {
|
||||
|
||||
@@ -20,13 +17,21 @@ public class StorageTypeFactory implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 项目启动时执行
|
||||
*/
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext act) throws BeansException {
|
||||
applicationContext = act;
|
||||
|
||||
// 获取 Spring 容器中所有 FileService 类型的类
|
||||
storageTypeEnumFileServiceMap = act.getBeansOfType(FileService.class);
|
||||
}
|
||||
|
||||
public static FileService getTrafficMode(StorageTypeEnum type) {
|
||||
/**
|
||||
* 获取指定存储类型 Service
|
||||
*/
|
||||
public static FileService getStorageTypeService(StorageTypeEnum type) {
|
||||
FileService result = null;
|
||||
for (FileService fileService : storageTypeEnumFileServiceMap.values()) {
|
||||
if (fileService.getStorageTypeEnum() == type) {
|
||||
|
||||
@@ -118,7 +118,7 @@ public class FileController {
|
||||
public ResultBean updateConfig() {
|
||||
SystemConfig systemConfig = systemConfigService.getSystemConfig();
|
||||
StorageTypeEnum storageStrategy = systemConfig.getStorageStrategy();
|
||||
fileService = StorageTypeFactory.getTrafficMode(storageStrategy);
|
||||
fileService = StorageTypeFactory.getStorageTypeService(storageStrategy);
|
||||
log.info("当前启用存储类型: {}", storageStrategy.getDescription());
|
||||
initSearchCache();
|
||||
return ResultBean.success();
|
||||
@@ -142,26 +142,6 @@ public class FileController {
|
||||
|
||||
@GetMapping("/search")
|
||||
public ResultBean search(@RequestParam("path") String name) throws Exception {
|
||||
return ResultBean.success(fileService.search(name));
|
||||
}
|
||||
|
||||
private void initSearchCache() {
|
||||
StorageTypeEnum storageStrategy = systemConfigService.getSystemConfig().getStorageStrategy();
|
||||
FileService fileService = StorageTypeFactory.getTrafficMode(storageStrategy);
|
||||
|
||||
ThreadUtil.execute(() -> {
|
||||
try {
|
||||
long startTime = System.currentTimeMillis();
|
||||
log.info("初始化 {} 文件列表", storageStrategy.getDescription());
|
||||
List<FileItem> fileItemList = fileService.selectAllFileList();
|
||||
long endTime = System.currentTimeMillis();
|
||||
log.info("完成 {} 缓存, 共缓存了 {} 个文件夹, 使用时间 {} 秒",
|
||||
storageStrategy.getDescription(),
|
||||
fileItemList.size(),
|
||||
(endTime - startTime) / 1000);
|
||||
} catch (Exception e) {
|
||||
log.info("初始化 " + storageStrategy.getDescription() + " 文件列表异常", e);
|
||||
}
|
||||
});
|
||||
return ResultBean.success(fileService.search(URLUtil.decode(name)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public class SystemConfigService {
|
||||
public FileService getCurrentFileService() {
|
||||
SystemConfig systemConfig = getSystemConfig();
|
||||
StorageTypeEnum storageStrategy = systemConfig.getStorageStrategy();
|
||||
return StorageTypeFactory.getTrafficMode(storageStrategy);
|
||||
return StorageTypeFactory.getStorageTypeService(storageStrategy);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user