Skip to content

Commit fcc3cb9

Browse files
committed
fix(system/storage): 修复对象存储域名配置
1 parent 4e44507 commit fcc3cb9

File tree

5 files changed

+63
-61
lines changed

5 files changed

+63
-61
lines changed

continew-common/src/main/java/top/continew/admin/common/constant/RegexConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public class RegexConstants {
5959
*/
6060
public static final String PACKAGE_NAME = "^(?:[a-zA-Z_][a-zA-Z0-9_]*\\.)*[a-zA-Z_][a-zA-Z0-9_]*$";
6161

62+
/**
63+
* HTTP 域名 URL 正则
64+
*/
65+
public static final String HTTP_DOMAIN_URL = "^(https?:\\/\\/)([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(\\/[^\\s]*)?$";
66+
6267
private RegexConstants() {
6368
}
6469
}

continew-server/src/main/java/top/continew/admin/ContiNewAdminApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public void run(ApplicationArguments args) {
7979
log.info("----------------------------------------------");
8080
log.info("{} service started successfully.", projectProperties.getName());
8181
log.info("Profile: {}", SpringUtil.getProperty("spring.profiles.active"));
82-
log.info("项目版本: v{} (ContiNew Starter: v{})", projectProperties.getVersion(), SpringUtil.getProperty("project.starter"));
82+
log.info("项目版本: v{} (ContiNew Starter: v{})", projectProperties.getVersion(), SpringUtil
83+
.getProperty("project.starter"));
8384
log.info("API 地址: {}", baseUrl);
8485
Knife4jProperties knife4jProperties = SpringUtil.getBean(Knife4jProperties.class);
8586
if (!knife4jProperties.isProduction()) {

continew-system/src/main/java/top/continew/admin/system/enums/StorageTypeEnum.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package top.continew.admin.system.enums;
1818

19+
import cn.hutool.core.util.ReUtil;
1920
import cn.hutool.core.util.StrUtil;
2021
import lombok.Getter;
2122
import lombok.RequiredArgsConstructor;
23+
import top.continew.admin.common.constant.RegexConstants;
2224
import top.continew.admin.system.model.req.StorageReq;
2325
import top.continew.admin.system.validation.ValidationGroup;
2426
import top.continew.starter.core.constant.StringConstants;
@@ -62,8 +64,8 @@ public void pretreatment(StorageReq req) {
6264
@Override
6365
public void validate(StorageReq req) {
6466
ValidationUtils.validate(req, ValidationGroup.Storage.OSS.class);
65-
ValidationUtils.throwIf(StrUtil.isNotBlank(req.getDomain()) && !URLUtils.isHttpUrl(req
66-
.getDomain()), "域名格式不正确");
67+
ValidationUtils.throwIf(StrUtil.isNotBlank(req.getDomain()) && !ReUtil
68+
.isMatch(RegexConstants.HTTP_DOMAIN_URL, req.getDomain()), "域名格式不正确");
6769
}
6870
};
6971

continew-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import top.continew.starter.extension.crud.service.BaseServiceImpl;
4949

5050
import java.io.File;
51-
import java.io.IOException;
5251
import java.util.List;
5352
import java.util.Map;
5453
import java.util.stream.Collectors;
@@ -91,65 +90,13 @@ public void beforeDelete(List<Long> ids) {
9190
}
9291

9392
@Override
94-
public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException {
95-
// 校验文件格式
96-
String extName = FileNameUtil.extName(file.getOriginalFilename());
97-
return getFileInfo(file, parentPath, storageCode, extName);
98-
}
99-
100-
/**
101-
* 上传文件并返回上传后的文件信息
102-
*
103-
* @param file
104-
* @param parentPath
105-
* @param storageCode
106-
* @param extName
107-
* @return
108-
*/
109-
private FileInfo getFileInfo(Object file, String parentPath, String storageCode, String extName) {
110-
List<String> allExtensions = FileTypeEnum.getAllExtensions();
111-
CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
112-
.join(StringConstants.COMMA, allExtensions));
113-
// 构建上传预处理对象
114-
StorageDO storage = storageService.getByCode(storageCode);
115-
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode());
116-
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
117-
.setPlatform(storage.getCode())
118-
.setHashCalculatorSha256(true)
119-
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
120-
.setPath(this.pretreatmentPath(parentPath));
121-
// 图片文件生成缩略图
122-
if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) {
123-
uploadPretreatment.setIgnoreThumbnailException(true, true);
124-
uploadPretreatment.thumbnail(img -> img.size(100, 100));
125-
}
126-
uploadPretreatment.setProgressMonitor(new ProgressListener() {
127-
@Override
128-
public void start() {
129-
log.info("开始上传");
130-
}
131-
132-
@Override
133-
public void progress(long progressSize, Long allSize) {
134-
log.info("已上传 [{}],总大小 [{}]", progressSize, allSize);
135-
}
136-
137-
@Override
138-
public void finish() {
139-
log.info("上传结束");
140-
}
141-
});
142-
// 创建父级目录
143-
this.createParentDir(parentPath, storage);
144-
// 上传
145-
return uploadPretreatment.upload();
93+
public FileInfo upload(MultipartFile file, String parentPath, String storageCode) {
94+
return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getOriginalFilename()));
14695
}
14796

14897
@Override
149-
public FileInfo upload(File file, String parentPath, String storageCode) throws IOException {
150-
// 校验文件格式
151-
String extName = FileNameUtil.extName(file.getName());
152-
return getFileInfo(file, parentPath, storageCode, extName);
98+
public FileInfo upload(File file, String parentPath, String storageCode) {
99+
return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getName()));
153100
}
154101

155102
@Override
@@ -251,6 +198,54 @@ protected void fill(Object obj) {
251198
}
252199
}
253200

201+
/**
202+
* 上传文件并返回上传后的文件信息
203+
*
204+
* @param file 文件
205+
* @param parentPath 上级目录
206+
* @param storageCode 存储引擎编码
207+
* @param extName 文件扩展名
208+
* @return 文件信息
209+
*/
210+
private FileInfo upload(Object file, String parentPath, String storageCode, String extName) {
211+
List<String> allExtensions = FileTypeEnum.getAllExtensions();
212+
CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
213+
.join(StringConstants.COMMA, allExtensions));
214+
// 构建上传预处理对象
215+
StorageDO storage = storageService.getByCode(storageCode);
216+
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode());
217+
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
218+
.setPlatform(storage.getCode())
219+
.setHashCalculatorSha256(true)
220+
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
221+
.setPath(this.pretreatmentPath(parentPath));
222+
// 图片文件生成缩略图
223+
if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) {
224+
uploadPretreatment.setIgnoreThumbnailException(true, true);
225+
uploadPretreatment.thumbnail(img -> img.size(100, 100));
226+
}
227+
uploadPretreatment.setProgressMonitor(new ProgressListener() {
228+
@Override
229+
public void start() {
230+
log.info("开始上传");
231+
}
232+
233+
@Override
234+
public void progress(long progressSize, Long allSize) {
235+
log.info("已上传 [{}],总大小 [{}]", progressSize, allSize);
236+
}
237+
238+
@Override
239+
public void finish() {
240+
log.info("上传结束");
241+
}
242+
});
243+
// 创建父级目录
244+
this.createParentDir(parentPath, storage);
245+
// 上传
246+
return uploadPretreatment.upload();
247+
}
248+
254249
/**
255250
* 处理路径
256251
*

continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ public void load(StorageDO storage) {
198198
config.setSecretKey(storage.getSecretKey());
199199
config.setEndPoint(storage.getEndpoint());
200200
config.setBucketName(storage.getBucketName());
201-
config.setDomain(StrUtil.emptyIfNull(storage.getDomain()));
202201
fileStorageList.addAll(FileStorageServiceBuilder.buildAmazonS3FileStorage(Collections
203202
.singletonList(config), null));
204203
}

0 commit comments

Comments
 (0)