Skip to content

Commit

Permalink
🐛 修复 FTP 加载 BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed Feb 22, 2020
1 parent a759d9f commit 595a00f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
36 changes: 28 additions & 8 deletions src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -37,17 +38,25 @@ public class FtpServiceImpl extends AbstractFileService implements FileService {

private String domain;

private String host;

private String port;

private String username;

private String password;

@Override
public void init() {
try {
Map<String, StorageConfig> stringStorageConfigMap =
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
String host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue();
String port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue();
String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue();
port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue();
username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();

super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();;
if (Objects.isNull(host) || Objects.isNull(port) || Objects.isNull(username) || Objects.isNull(password)) {
isInitialized = false;
} else {
Expand All @@ -61,8 +70,9 @@ public void init() {
}

@Override
public List<FileItemDTO> fileList(String path) {
FTPFile[] ftpFiles = ftp.lsFiles(path);
public synchronized List<FileItemDTO> fileList(String path) throws IOException {
String fullPath = StringUtils.getFullPath(basePath, path);
FTPFile[] ftpFiles = ftp.lsFiles(fullPath);

List<FileItemDTO> fileItemList = new ArrayList<>();

Expand All @@ -83,7 +93,17 @@ public List<FileItemDTO> fileList(String path) {

@Override
public String getDownloadUrl(String path) {
return URLUtil.complateUrl(domain, path);
String fullPath = StringUtils.getFullPath(basePath, path);
if (StringUtils.isNullOrEmpty(domain)) {
return "ftp://"
+ URLUtil.encodeQuery(username)
+ ":"
+ URLUtil.encodeQuery(password)
+ "@"
+ host + ":" + port + fullPath;
}

return URLUtil.complateUrl(domain, fullPath);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/db/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (20, 'host', '域
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (21, 'port', '端口', 'ftp');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (22, 'username', '用户名', 'ftp');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (23, 'password', '密码', 'ftp');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (24, 'domain', '域名', 'ftp');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (24, 'domain', '加速域名', 'ftp');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (25, 'secretId', 'SecretId', 'tencent');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (26, 'secretKey', 'SecretKey', 'tencent');
INSERT INTO STORAGE_CONFIG (`ID`, `k`, `TITLE`, `TYPE`) VALUES (27, 'bucket-name', '云存储服务名称', 'tencent');
Expand Down

0 comments on commit 595a00f

Please sign in to comment.