Skip to content

Commit

Permalink
🔨 重构代码, 去除无效引用。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed Mar 8, 2020
1 parent de2f7e4 commit 430aee2
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 113 deletions.
30 changes: 24 additions & 6 deletions src/main/java/im/zhaojun/zfile/cache/ZFileCache.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package im.zhaojun.zfile.cache;

import cn.hutool.core.util.StrUtil;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -23,9 +22,6 @@ public class ZFileCache {

public Date lastCacheAutoRefreshDate;

@Resource
private SystemConfigService systemConfigService;

public synchronized void put(String key, List<FileItemDTO> value) {
fileCache.put(key, value);
}
Expand All @@ -42,11 +38,17 @@ public int cacheCount() {
return fileCache.size();
}

public List<FileItemDTO> find(String key, boolean ignoreCase) {
public List<FileItemDTO> find(String key, boolean ignoreCase, boolean searchContainEncryptedFile) {
List<FileItemDTO> result = new ArrayList<>();

Collection<List<FileItemDTO>> values = fileCache.values();
for (List<FileItemDTO> fileItemList : values) {

// 如果开启了 "搜索包含加密文件" 选项, 则直接返回 true.
if (!searchContainEncryptedFile && isEncryptedFolder(fileItemList)) {
continue;
}

for (FileItemDTO fileItemDTO : fileItemList) {
boolean testResult;

Expand Down Expand Up @@ -91,4 +93,20 @@ public Date getLastCacheAutoRefreshDate() {
public void setLastCacheAutoRefreshDate(Date lastCacheAutoRefreshDate) {
this.lastCacheAutoRefreshDate = lastCacheAutoRefreshDate;
}


/**
* 不是加密文件夹
* @param list 文件夹中的内容
* @return 返回此文件夹是否加密.
*/
private boolean isEncryptedFolder(List<FileItemDTO> list) {
// 遍历文件判断是否包含
for (FileItemDTO fileItemDTO : list) {
if (Objects.equals(ZFileConstant.PASSWORD_FILE_NAME, fileItemDTO.getName())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package im.zhaojun.zfile.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
Expand All @@ -17,12 +15,9 @@
*/
public class ContentTypeTextToTextJson implements ClientHttpRequestInterceptor {

private static final Logger LOG = LoggerFactory.getLogger(ContentTypeTextToTextJson.class);

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
URI uri = request.getURI();
ClientHttpResponse response = execution.execute(request, body);
HttpHeaders headers = response.getHeaders();
headers.put("Content-Type", Collections.singletonList("application/text"));
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/im/zhaojun/zfile/config/GlobalScheduleTask.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package im.zhaojun.zfile.config;

import im.zhaojun.zfile.cache.ZFileCache;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,12 +22,6 @@
@Slf4j
public class GlobalScheduleTask {

@Resource
private ZFileCache zFileCache;

@Resource
private StorageConfigService storageConfigService;

@Resource
private OneDriveServiceImpl oneDriveServiceImpl;

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/im/zhaojun/zfile/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ZipUtil;
import im.zhaojun.zfile.config.StorageTypeFactory;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.support.SystemMonitorInfo;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.StorageStrategyDTO;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
import im.zhaojun.zfile.model.support.SystemMonitorInfo;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
import im.zhaojun.zfile.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;

/**
* 后台管理
Expand All @@ -44,8 +47,6 @@ public class AdminController {
@Resource
private FileAsyncCacheService fileAsyncCacheService;

private ScheduledExecutorService scheduledExecutorService;

/**
* 获取系统配置
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import im.zhaojun.zfile.cache.ZFileCache;
import im.zhaojun.zfile.model.dto.CacheConfigDTO;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
import im.zhaojun.zfile.service.support.FileCacheService;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -47,7 +47,6 @@ public ResultBean disableCache() {

@GetMapping("/config")
public ResultBean cacheConfig() {
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
CacheConfigDTO cacheConfigDTO = new CacheConfigDTO();
cacheConfigDTO.setEnableCache(systemConfigService.getEnableCache());
cacheConfigDTO.setCacheFinish(fileAsyncCacheService.isCacheFinish());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
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.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.HandlerMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.util.Objects;

/**
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/im/zhaojun/zfile/core/FileListCacheAop.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public class FileListCacheAop {
@Resource
private SystemConfigService systemConfigService;

@Pointcut("execution(public * im.zhaojun.zfile.service.base.AbstractBaseFileService.fileList(..))")
public void pointcut() {
}

@Around(value = "pointcut()")
@Around(value = "execution(public * im.zhaojun.zfile.service.base.AbstractBaseFileService.fileList(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
List<FileItemDTO> result;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/im/zhaojun/zfile/model/dto/ResultBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class ResultBean implements Serializable {

private Object data;

private int total;

private ResultBean() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package im.zhaojun.zfile.model.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.repository.SystemConfigRepository;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.support.FileCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand All @@ -33,9 +32,6 @@ public class SystemConfigService {
@Resource
private SystemConfigRepository systemConfigRepository;

@Resource
private FileCacheService fileCacheService;

private Class<SystemConfigDTO> systemConfigClazz = SystemConfigDTO.class;

public SystemConfigDTO getSystemConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

import cn.hutool.core.util.BooleanUtil;
import im.zhaojun.zfile.cache.ZFileCache;
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
import im.zhaojun.zfile.service.support.FileCacheService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
import im.zhaojun.zfile.service.support.FileCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
* @author zhaojun
*/
@Slf4j
public abstract class AbstractBaseFileService extends FileCacheService implements BaseFileService {

private static final String SYSTEM_CONFIG_CACHE_PREFIX = "zfile-cache:";

@Value("${zfile.cache.timeout}")
protected Long timeout;

Expand Down Expand Up @@ -106,29 +102,11 @@ public boolean getIsInitialized() {
* @return 包含该文件名的所有文件或文件夹
*/
public List<FileItemDTO> search(String name) {
boolean searchIgnoreCase = systemConfigService.getSearchIgnoreCase();
return zFileCache.find(name, searchIgnoreCase);
}

/**
* 不是加密文件夹
* @param list 文件夹中的内容
* @return 返回此文件夹是否加密.
*/
private boolean isNotEncryptedFolder(List<FileItemDTO> list) {
// 如果开启了 "搜索包含加密文件" 选项, 则直接返回 true.
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
if (BooleanUtil.isFalse(systemConfig.getSearchContainEncryptedFile())) {
return true;
}

// 遍历文件判断是否包含
for (FileItemDTO fileItemDTO : list) {
if (Objects.equals(ZFileConstant.PASSWORD_FILE_NAME, fileItemDTO.getName())) {
return false;
}
}
return true;
boolean searchIgnoreCase = BooleanUtil.isTrue(systemConfig.getSearchIgnoreCase());
boolean searchContainEncryptedFile = BooleanUtil.isTrue(systemConfig.getSearchContainEncryptedFile());
return zFileCache.find(name, searchIgnoreCase, searchContainEncryptedFile);
}

/**
Expand Down
28 changes: 8 additions & 20 deletions src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.ftp.Ftp;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.FileTypeEnum;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.service.base.BaseFileService;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.util.StringUtils;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -127,20 +131,4 @@ public FileItemDTO getFileItem(String path) {
fileItemDTO.setUrl(getDownloadUrl(path));
return fileItemDTO;
}

private void ifDisConnectionReConnection() {
FTPClient ftpClient = ftp.getClient();
try {
// 验证FTP服务器是否登录成功
int replyCode = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("断开了连接, 已尝试重新连接.");
ftpClient.connect(host, Integer.parseInt(port));
ftpClient.login(username, password);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package im.zhaojun.zfile.service.impl;

import im.zhaojun.zfile.config.GlobalScheduleTask;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.base.BaseFileService;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.service.base.AbstractOneDriveServiceBase;
import im.zhaojun.zfile.service.base.BaseFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -22,9 +21,6 @@
@Slf4j
public class OneDriveChinaServiceImpl extends AbstractOneDriveServiceBase implements BaseFileService {

@Resource
private GlobalScheduleTask globalScheduleTask;

@Resource
private StorageConfigService storageConfigService;

Expand Down
Loading

0 comments on commit 430aee2

Please sign in to comment.