OneDrive 基础路径支持

This commit is contained in:
zhaojun1998
2020-01-31 11:21:36 +08:00
parent f7bb147b71
commit f5724dc9ab
7 changed files with 30 additions and 21 deletions

View File

@@ -42,6 +42,8 @@ public abstract class AbstractFileService extends FileCacheService implements Fi
protected boolean isInitialized = false;
protected String basePath;
@Resource
private SystemConfigService systemConfigService;

View File

@@ -29,8 +29,6 @@ public abstract class AbstractS3FileService extends AbstractFileService {
protected String path;
protected String basePath;
protected String bucketName;
protected String domain;
@@ -58,7 +56,7 @@ public abstract class AbstractS3FileService extends AbstractFileService {
*/
public List<FileItemDTO> s3FileList(String path) {
path = StringUtils.removeFirstSeparator(path);
String fullPath = StringUtils.removeFirstSeparator(getFullPath());
String fullPath = StringUtils.removeFirstSeparator(StringUtils.getFullPath(basePath, path));
List<FileItemDTO> fileItemList = new ArrayList<>();
ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest(bucketName, fullPath, "", "/", 1000));
@@ -112,16 +110,6 @@ public abstract class AbstractS3FileService extends AbstractFileService {
return URLUtil.decode(defaultUrl);
}
/**
* 获取 basePath + path 的全路径地址.
* @return basePath + path 的全路径地址.
*/
public String getFullPath() {
String basePath = ObjectUtil.defaultIfNull(this.basePath, "");
String path = ObjectUtil.defaultIfNull(this.path, "");
return StringUtils.removeDuplicateSeparator(basePath + "/" + path);
}
@Override
public FileItemDTO getFileItem(String path) {
List<FileItemDTO> list = fileList(path);

View File

@@ -1,5 +1,7 @@
package im.zhaojun.common.util;
import cn.hutool.core.util.ObjectUtil;
/**
* @author zhaojun
*/
@@ -88,4 +90,14 @@ public class StringUtils {
public static boolean isNotNullOrEmpty(String s) {
return !isNullOrEmpty(s);
}
/**
* 获取 basePath + path 的全路径地址.
* @return basePath + path 的全路径地址.
*/
public static String getFullPath(String basePath, String path) {
basePath = ObjectUtil.defaultIfNull(basePath, "");
path = ObjectUtil.defaultIfNull(path, "");
return StringUtils.removeDuplicateSeparator(basePath + "/" + path);
}
}

View File

@@ -40,6 +40,7 @@ public class OneDriveServiceChinaImpl extends AbstractFileService implements Fil
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
@@ -55,7 +56,7 @@ public class OneDriveServiceChinaImpl extends AbstractFileService implements Fil
@Override
public List<FileItemDTO> fileList(String path) {
return oneDriveChinaService.list(path);
return oneDriveChinaService.list(basePath, path);
}
@Override

View File

@@ -83,7 +83,10 @@ public abstract class AbstractOneDriveService {
return oneDriveRestTemplate.getForObject(DRIVER_INFO_URL, String.class);
}
public List<FileItemDTO> list(String path) {
public List<FileItemDTO> list(String basePath, String path) {
path = StringUtils.removeFirstSeparator(path);
String fullPath = StringUtils.getFullPath(basePath, path);
List<FileItemDTO> result = new ArrayList<>();
String nextLink = null;
@@ -93,14 +96,14 @@ public abstract class AbstractOneDriveService {
if (nextLink != null) {
requestUrl = nextLink;
}else if ("/".equalsIgnoreCase(path)) {
}else if ("/".equalsIgnoreCase(fullPath) || "".equalsIgnoreCase(fullPath)) {
requestUrl = DRIVER_ROOT_URL;
} else {
requestUrl = DRIVER_ITEMS_URL;
}
path = StringUtils.removeLastSeparator(path);
fullPath = StringUtils.removeLastSeparator(fullPath);
ResponseEntity<String> responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, getGraphEndPoint(), path);
ResponseEntity<String> responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, getGraphEndPoint(), fullPath);
String body = responseEntity.getBody();
JSONObject root = JSON.parseObject(body);

View File

@@ -40,6 +40,7 @@ public class OneDriveServiceImpl extends AbstractFileService implements FileServ
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
@@ -55,7 +56,7 @@ public class OneDriveServiceImpl extends AbstractFileService implements FileServ
@Override
public List<FileItemDTO> fileList(String path) {
return oneDriveService.list(path);
return oneDriveService.list(basePath, path);
}
@Override