🐛 修复阿里云腾讯云私有空间时, 密码文件无法加载的 BUG

This commit is contained in:
zhaojun1998
2020-04-20 22:37:12 +08:00
parent 547e688d38
commit 15f8fbb49b
6 changed files with 27 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ public class StorageStrategyConfig {
private String pathStyle;
private boolean isPrivate;
private Boolean isPrivate;
private String accessKey;

View File

@@ -2,6 +2,7 @@ package im.zhaojun.zfile.service;
import im.zhaojun.zfile.context.StorageTypeContext;
import im.zhaojun.zfile.exception.InitializeException;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.dto.DriveConfigDTO;
import im.zhaojun.zfile.model.dto.StorageStrategyConfig;
import im.zhaojun.zfile.model.entity.DriveConfig;
@@ -19,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
/**
* 驱动器 Service 类
@@ -87,7 +89,11 @@ public class DriveConfigService {
try {
declaredField = STORAGE_STRATEGY_CONFIG_CLASS.getDeclaredField(key);
declaredField.setAccessible(true);
declaredField.set(storageStrategyConfig, value);
if (Objects.equals(StorageConfigConstant.IS_PRIVATE, key)) {
declaredField.set(storageStrategyConfig, Boolean.valueOf(value));
} else {
declaredField.set(storageStrategyConfig, value);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
if (log.isDebugEnabled()) {
log.debug("通过反射, 将字段 {" + key + "}注入 DriveConfigDTO 时出现异常:", e);

View File

@@ -29,11 +29,11 @@ public abstract class AbstractBaseFileService implements BaseFileService {
*/
protected boolean isInitialized = false;
/**
* 基路径
*/
protected String basePath;
/**
* 驱动器 ID
*/

View File

@@ -93,6 +93,7 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService
* @return S3 对象访问地址
*/
public String s3ObjectUrl(String path) {
basePath = basePath == null ? "" : basePath;
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + "/" + path));
// 如果不是私有空间, 且指定了加速域名, 则直接返回下载地址.

View File

@@ -57,7 +57,7 @@ public class AliyunServiceImpl extends AbstractS3BaseFileService implements Base
isInitialized = testConnection();
}
} catch (Exception e) {
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过", e);
}
}
@@ -75,6 +75,7 @@ public class AliyunServiceImpl extends AbstractS3BaseFileService implements Base
add(new StorageConfig("domain", "Bucket 域名 / CDN 加速域名"));
add(new StorageConfig("endPoint", "区域"));
add(new StorageConfig("basePath", "基路径"));
add(new StorageConfig("isPrivate", "是否是私有空间"));
}};
}
}

View File

@@ -3,9 +3,12 @@ package im.zhaojun.zfile.util;
import im.zhaojun.zfile.exception.PreviewException;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author zhaojun
*/
@@ -30,8 +33,17 @@ public class HttpUtil {
* 获取远程文件大小
*/
public static Long getRemoteFileSize(String url) {
HttpHeaders httpHeaders = new RestTemplate().headForHeaders(url);
return httpHeaders.getContentLength();
long size = 0;
URL urlObject;
try {
urlObject = new URL(url);
URLConnection conn = urlObject.openConnection();
size = conn.getContentLength();
} catch (IOException e) {
e.printStackTrace();
}
return size;
}
}