🎨 优化代码结构, 调整包名, 方法名.

This commit is contained in:
zhaojun1998
2020-06-25 17:05:59 +08:00
parent 4585f22817
commit ee5fb54ebb
26 changed files with 84 additions and 83 deletions

View File

@@ -19,7 +19,7 @@ import java.util.List;
*/
@Aspect
@Component
public class FileListCacheAop {
public class FileListCacheAspect {
@Resource
private ZFileCache zFileCache;

View File

@@ -32,7 +32,7 @@ public class MyTimedCache<K, V> extends TimedCache<K, V> {
driveContext = SpringContextHolder.getBean(DriveContext.class);
}
DriveCacheKey cacheKey = (DriveCacheKey) key;
AbstractBaseFileService baseFileService = driveContext.getDriveService(cacheKey.getDriveId());
AbstractBaseFileService baseFileService = driveContext.get(cacheKey.getDriveId());
try {
baseFileService.fileList(cacheKey.getKey());
} catch (Exception e) {

View File

@@ -28,15 +28,21 @@ import java.util.concurrent.ConcurrentMap;
@Component
public class ZFileCache {
/**
* 缓存过期时间
*/
@Value("${zfile.cache.timeout}")
private long timeout;
/**
* 缓存自动刷新间隔
*/
@Value("${zfile.cache.auto-refresh.interval}")
private long autoRefreshInterval;
/**
* 缓存 map 对象.
* 文件/文件对象缓存.
*
* ConcurrentMap<Integer, ConcurrentHashMap<String, List<FileItemDTO>>>
* ConcurrentMap<driveId, ConcurrentHashMap<key, value>>

View File

@@ -30,7 +30,6 @@ public class OneDriveConfig {
@Resource
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
/**
* OneDrive 请求 RestTemplate, 会在请求头中添加 Bearer: Authorization {token} 信息, 用于 API 认证.
*/

View File

@@ -12,35 +12,49 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* 驱动器上下文环境
* 每个驱动器对应一个 Service, 其中初始化好了与对象存储的连接信息.
* 此驱动器上下文环境用户缓存每个 Service, 避免重复创建连接.
* @author zhaojun
*/
@Component
@DependsOn("springContextHolder")
public class DriveContext implements ApplicationContextAware {
/**
* Map<Integer, AbstractBaseFileService>
* Map<驱动器 ID, 驱动器连接 Service>
*/
private static Map<Integer, AbstractBaseFileService> drivesServiceMap = new ConcurrentHashMap<>();
private static Map<StorageTypeEnum, Class<AbstractBaseFileService>> storageTypeEnumClassMap = new ConcurrentHashMap<>();
@Resource
private DriveConfigService driveConfigService;
/**
* 项目启动时, 自动调用所有驱动器进行初始化.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
List<DriveConfig> list = driveConfigService.list();
for (DriveConfig driveConfig : list) {
init(driveConfig.getId());
}
}
/**
* 初始化指定驱动器的 Service, 添加到上下文环境中.
*
* @param driveId
* 驱动器 ID.
*/
public void initDrive(Integer driveId) {
public void init(Integer driveId) {
AbstractBaseFileService baseFileService = getBeanByDriveId(driveId);
if (baseFileService != null) {
baseFileService.init(driveId);
@@ -57,7 +71,7 @@ public class DriveContext implements ApplicationContextAware {
*
* @return 驱动器对应的 Service
*/
public AbstractBaseFileService getDriveService(Integer driveId) {
public AbstractBaseFileService get(Integer driveId) {
return drivesServiceMap.get(driveId);
}
@@ -68,7 +82,7 @@ public class DriveContext implements ApplicationContextAware {
* @param driveId
* 驱动器 ID
*/
public void destroyDrive(Integer driveId) {
public void destroy(Integer driveId) {
drivesServiceMap.remove(driveId);
}
@@ -92,16 +106,4 @@ public class DriveContext implements ApplicationContextAware {
return null;
}
/**
* 项目启动时, 自动调用所有驱动器进行初始化.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
List<DriveConfig> list = driveConfigService.list();
for (DriveConfig driveConfig : list) {
initDrive(driveConfig.getId());
}
}
}

View File

@@ -6,6 +6,7 @@ import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;

View File

@@ -1,11 +1,11 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import cn.hutool.core.util.URLUtil;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.enums.FileTypeEnum;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.context.DriveContext;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
@@ -20,7 +20,7 @@ import java.util.Objects;
* @author Zhao Jun
*/
@Controller
public class PageController {
public class DirectLinkController {
@Resource
private DriveContext driveContext;
@@ -44,7 +44,7 @@ public class PageController {
filePath = "/" + filePath;
}
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
AbstractBaseFileService fileService = driveContext.get(driveId);
FileItemDTO fileItem = fileService.getFileItem(filePath);
String url = fileItem.getUrl();
@@ -67,4 +67,5 @@ public class PageController {
return "redirect:" + url;
}
}
}
}

View File

@@ -1,32 +1,19 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.util.AudioHelper;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.util.AudioUtil;
import im.zhaojun.zfile.util.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 公共 Controller
* 文件解析 Controller
* @author zhaojun
*/
@RestController
@RequestMapping("/common")
public class CommonController {
/**
* 返回系统支持的所有存储策略
*
* @return 存储策略
*/
@GetMapping("/support-strategy")
public ResultBean supportStrategy() {
return ResultBean.successData(StorageTypeEnum.values());
}
public class FIleParseController {
/**
* 获取文件内容, 仅限用于 txt, md, ini 等普通文本文件.
@@ -52,7 +39,7 @@ public class CommonController {
*/
@GetMapping("/audio-info")
public ResultBean getAudioInfo(String url) throws Exception {
return ResultBean.success(AudioHelper.getAudioInfo(url));
return ResultBean.success(AudioUtil.getAudioInfo(url));
}
}

View File

@@ -1,14 +1,14 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.SystemFrontConfigDTO;
import im.zhaojun.zfile.model.support.FilePageModel;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.util.FileComparator;
import im.zhaojun.zfile.util.HttpUtil;
import im.zhaojun.zfile.util.StringUtils;
@@ -83,8 +83,8 @@ public class FileController {
@RequestParam(defaultValue = "/") String path,
@RequestParam(required = false) String password,
@RequestParam(defaultValue = "1") Integer page) throws Exception {
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + path + "/"));
AbstractBaseFileService fileService = driveContext.get(driveId);
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator(ZFileConstant.PATH_SEPARATOR + path + ZFileConstant.PATH_SEPARATOR));
for (FileItemDTO fileItemDTO : fileItemList) {
if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())) {
@@ -94,7 +94,7 @@ public class FileController {
} catch (HttpClientErrorException httpClientErrorException) {
log.debug("尝试重新获取密码文件缓存中链接后仍失败", httpClientErrorException);
try {
String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + "/" + fileItemDTO.getName());
String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + ZFileConstant.PATH_SEPARATOR + fileItemDTO.getName());
FileItemDTO fileItem = fileService.getFileItem(fullPath);
expectedPasswordContent = HttpUtil.getTextContent(fileItem.getUrl());
} catch (Exception e) {
@@ -128,8 +128,8 @@ public class FileController {
public ResultBean getConfig(@PathVariable(name = "driveId") Integer driveId, String path) {
SystemFrontConfigDTO systemConfig = systemConfigService.getSystemFrontConfig(driveId);
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
String fullPath = StringUtils.removeDuplicateSeparator(path + "/" + ZFileConstant.README_FILE_NAME);
AbstractBaseFileService fileService = driveContext.get(driveId);
String fullPath = StringUtils.removeDuplicateSeparator(path + ZFileConstant.PATH_SEPARATOR + ZFileConstant.README_FILE_NAME);
try {
FileItemDTO fileItem = fileService.getFileItem(fullPath);
String readme = HttpUtil.getTextContent(fileItem.getUrl());
@@ -214,7 +214,8 @@ public class FileController {
*/
@GetMapping("/directlink/{driveId}")
public ResultBean directlink(@PathVariable(name = "driveId") Integer driveId, String path) {
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
AbstractBaseFileService fileService = driveContext.get(driveId);
return ResultBean.successData(fileService.getFileItem(path));
}
}
}

View File

@@ -42,8 +42,8 @@ public class LocalController {
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
AntPathMatcher apm = new AntPathMatcher();
String filePath = apm.extractPathWithinPattern(bestMatchPattern, path);
LocalServiceImpl localService = (LocalServiceImpl) driveContext.getDriveService(driveId);
return FileUtil.export(new File(StringUtils.concatPath(localService.getFilePath(), filePath)));
LocalServiceImpl localService = (LocalServiceImpl) driveContext.get(driveId);
return FileUtil.export(new File(StringUtils.removeDuplicateSeparator(localService.getFilePath() + ZFileConstant.PATH_SEPARATOR + filePath)));
}
}

View File

@@ -1,9 +1,9 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.install;
import cn.hutool.crypto.SecureUtil;
import im.zhaojun.zfile.controller.admin.AdminController;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.util.StringUtils;

View File

@@ -1,4 +1,4 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.onedrive;
import im.zhaojun.zfile.model.support.OneDriveToken;
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
@@ -15,7 +15,7 @@ import javax.annotation.Resource;
*/
@Controller
@RequestMapping("/onedrive")
public class OneDriveController {
public class OneDriveCallbackController {
@Resource
private OneDriveServiceImpl oneDriveServiceImpl;

View File

@@ -24,4 +24,4 @@ public class AudioInfoDTO {
return audioInfoDTO;
}
}
}

View File

@@ -28,4 +28,5 @@ public enum FileTypeEnum {
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -63,4 +63,4 @@ public enum StorageTypeEnum {
return enumMap.get(value.toLowerCase());
}
}
}

View File

@@ -1,4 +1,4 @@
package im.zhaojun.zfile.model.dto;
package im.zhaojun.zfile.model.support;
import java.io.Serializable;

View File

@@ -1,12 +1,12 @@
package im.zhaojun.zfile.schedule;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.base.AbstractOneDriveServiceBase;
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
import im.zhaojun.zfile.context.DriveContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -54,7 +54,7 @@ public class OneDriveTokenRefreshSchedule {
String name = driveConfig.getName();
try {
AbstractOneDriveServiceBase driveService = (AbstractOneDriveServiceBase) driveContext.getDriveService(driveConfig.getId());
AbstractOneDriveServiceBase driveService = (AbstractOneDriveServiceBase) driveContext.get(driveConfig.getId());
driveService.refreshOneDriveToken();
log.info("刷新驱动器 {}, {} key 时间: {}", name, storageType.getDescription(), LocalDateTime.now());
} catch (Exception e) {

View File

@@ -1,7 +1,7 @@
package im.zhaojun.zfile.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.support.ResultBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.BadCredentialsException;

View File

@@ -181,9 +181,9 @@ public class DriveConfigService {
}
storageConfigRepository.saveAll(storageConfigList);
driveContext.initDrive(driveConfig.getId());
driveContext.init(driveConfig.getId());
AbstractBaseFileService driveService = driveContext.getDriveService(driveConfig.getId());
AbstractBaseFileService driveService = driveContext.get(driveConfig.getId());
if (driveService.getIsUnInitialized()) {
throw new InitializeException("初始化异常, 请检查配置是否正确.");
}
@@ -200,7 +200,7 @@ public class DriveConfigService {
public void deleteById(Integer id) {
driverConfigRepository.deleteById(id);
storageConfigRepository.deleteByDriveId(id);
driveContext.destroyDrive(id);
driveContext.destroy(id);
}
@@ -282,7 +282,7 @@ public class DriveConfigService {
*/
public void refreshCache(Integer driveId, String key) throws Exception {
zFileCache.remove(driveId, key);
AbstractBaseFileService baseFileService = driveContext.getDriveService(driveId);
AbstractBaseFileService baseFileService = driveContext.get(driveId);
baseFileService.fileList(key);
}

View File

@@ -6,7 +6,6 @@ import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
@@ -120,7 +119,7 @@ public abstract class AbstractBaseFileService implements BaseFileService {
/**
* 获取初始化当前存储策略, 所需要的参数信息 (表单填写)
* 获取初始化当前存储策略, 所需要的参数信息 (用于表单填写)
*
* @return 初始化所需的参数列表
*/

View File

@@ -20,7 +20,6 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

View File

@@ -7,6 +7,7 @@ import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import im.zhaojun.zfile.exception.NotExistFileException;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.enums.FileTypeEnum;
import im.zhaojun.zfile.service.StorageConfigService;
@@ -94,7 +95,7 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService
*/
public String s3ObjectUrl(String path) {
basePath = basePath == null ? "" : basePath;
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + "/" + path));
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + ZFileConstant.PATH_SEPARATOR + path));
// 如果不是私有空间, 且指定了加速域名, 则直接返回下载地址.
if (BooleanUtil.isFalse(isPrivate) && StringUtils.isNotNullOrEmpty(domain)) {

View File

@@ -3,6 +3,7 @@ package im.zhaojun.zfile.service.impl;
import im.zhaojun.zfile.exception.NotExistFileException;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.constant.SystemConfigConstant;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.entity.SystemConfig;
@@ -63,6 +64,7 @@ public class LocalServiceImpl extends AbstractBaseFileService implements BaseFil
}
}
@Override
public List<FileItemDTO> fileList(String path) throws FileNotFoundException {
List<FileItemDTO> fileItemList = new ArrayList<>();
@@ -99,7 +101,7 @@ public class LocalServiceImpl extends AbstractBaseFileService implements BaseFil
@Override
public String getDownloadUrl(String path) {
SystemConfig usernameConfig = systemConfigRepository.findByKey(SystemConfigConstant.DOMAIN);
return StringUtils.removeDuplicateSeparator(usernameConfig.getValue() + "/file/" + driveId + "/" + path);
return StringUtils.removeDuplicateSeparator(usernameConfig.getValue() + "/file/" + driveId + ZFileConstant.PATH_SEPARATOR + path);
}
public String getFilePath() {

View File

@@ -6,6 +6,7 @@ import com.UpYun;
import com.upyun.UpException;
import im.zhaojun.zfile.exception.NotExistFileException;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.FileTypeEnum;
@@ -122,7 +123,7 @@ public class UpYunServiceImpl extends AbstractBaseFileService implements BaseFil
int lastDelimiterIndex = path.lastIndexOf("/");
String name = path.substring(lastDelimiterIndex + 1);
Map<String, String> fileInfo = upYun.getFileInfo(StringUtils.removeDuplicateSeparator(basePath + "/" + path));
Map<String, String> fileInfo = upYun.getFileInfo(StringUtils.removeDuplicateSeparator(basePath + ZFileConstant.PATH_SEPARATOR + path));
if (fileInfo == null) {
throw new NotExistFileException();
@@ -138,7 +139,7 @@ public class UpYunServiceImpl extends AbstractBaseFileService implements BaseFil
fileItemDTO.setType(FileTypeEnum.FOLDER);
} else {
fileItemDTO.setType(FileTypeEnum.FILE);
fileItemDTO.setUrl(getDownloadUrl(StringUtils.removeDuplicateSeparator(basePath + "/" + path)));
fileItemDTO.setUrl(getDownloadUrl(StringUtils.removeDuplicateSeparator(basePath + ZFileConstant.PATH_SEPARATOR + path)));
}
return fileItemDTO;
} catch (IOException | UpException e) {

View File

@@ -24,9 +24,9 @@ import java.net.URL;
* 音频解析工具类
* @author zhaojun
*/
public class AudioHelper {
public class AudioUtil {
private static final Logger log = LoggerFactory.getLogger(AudioHelper.class);
private static final Logger log = LoggerFactory.getLogger(AudioUtil.class);
public static AudioInfoDTO getAudioInfo(String url) throws Exception {
String query = new URL(URLUtil.decode(url)).getQuery();

View File

@@ -1,6 +1,7 @@
package im.zhaojun.zfile.util;
import cn.hutool.core.util.ObjectUtil;
import im.zhaojun.zfile.model.constant.ZFileConstant;
/**
* @author zhaojun
@@ -98,6 +99,6 @@ public class StringUtils {
public static String getFullPath(String basePath, String path) {
basePath = ObjectUtil.defaultIfNull(basePath, "");
path = ObjectUtil.defaultIfNull(path, "");
return StringUtils.removeDuplicateSeparator(basePath + "/" + path);
return StringUtils.removeDuplicateSeparator(basePath + ZFileConstant.PATH_SEPARATOR + path);
}
}