Skip to content

Commit

Permalink
refactor: 完善存储库业务逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Dec 28, 2023
1 parent 3399bc8 commit 44227ea
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public String socialLogin(AuthUser authUser) {
userSocial.setOpenId(openId);
this.sendSystemMsg(user);
} else {
user = BeanUtil.toBean(userService.get(userSocial.getUserId()), UserDO.class);
user = BeanUtil.copyProperties(userService.get(userSocial.getUserId()), UserDO.class);
}
this.checkUserStatus(user);
userSocial.setMetaJson(JSONUtil.toJsonStr(authUser));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;

import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.model.query.StorageQuery;
import top.charles7c.continew.admin.system.model.req.StorageReq;
import top.charles7c.continew.admin.system.model.resp.StorageResp;
import top.charles7c.continew.admin.system.service.StorageService;
Expand All @@ -39,15 +41,17 @@
* @since 2023/12/24 22:31
*/
@Slf4j
@Component
//@Component
@RequiredArgsConstructor
public class FileStorageConfigLoader implements ApplicationRunner {

private final StorageService storageService;

@Override
public void run(ApplicationArguments args) {
List<StorageResp> storageList = storageService.list(null, null);
StorageQuery query = new StorageQuery();
query.setStatus(DisEnableStatusEnum.ENABLE.getValue());
List<StorageResp> storageList = storageService.list(query, null);
if (CollUtil.isEmpty(storageList)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public interface FileService extends BaseService<FileResp, FileDetailResp, FileQuery, FileReq> {

/**
* 上传
* 上传到默认存储库
*
* @param file
* 文件信息
Expand All @@ -45,7 +45,7 @@ default void upload(MultipartFile file) {
}

/**
* 上传
* 上传到指定存储库
*
* @param file
* 文件信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public interface StorageService extends BaseService<StorageResp, StorageDetailRe
/**
* 卸载存储库
*
* @param code
* 编码
* @param req
* 存储库信息
*/
void unload(String code);
void unload(StorageReq req);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;

import top.charles7c.continew.admin.system.mapper.FileMapper;
import top.charles7c.continew.admin.system.model.entity.FileDO;
Expand All @@ -36,8 +37,11 @@
import top.charles7c.continew.admin.system.model.req.FileReq;
import top.charles7c.continew.admin.system.model.resp.FileDetailResp;
import top.charles7c.continew.admin.system.model.resp.FileResp;
import top.charles7c.continew.admin.system.model.resp.StorageDetailResp;
import top.charles7c.continew.admin.system.service.FileService;
import top.charles7c.continew.admin.system.service.StorageService;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.util.URLUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;

Expand Down Expand Up @@ -88,4 +92,13 @@ public void finish() {
public Long countByStorageIds(List<Long> storageIds) {
return baseMapper.lambdaQuery().in(FileDO::getStorageId, storageIds).count();
}

@Override
protected void fill(Object baseObj) {
if (baseObj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) {
StorageDetailResp storage = storageService.get(fileResp.getStorageId());
fileResp.setUrl(URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl()));
}
super.fill(baseObj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.util.UrlPathHelper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;

import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.enums.StorageTypeEnum;
Expand All @@ -51,6 +54,7 @@
import top.charles7c.continew.admin.system.model.resp.StorageResp;
import top.charles7c.continew.admin.system.service.FileService;
import top.charles7c.continew.admin.system.service.StorageService;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
Expand Down Expand Up @@ -89,7 +93,7 @@ public void update(StorageReq req, Long id) {
CheckUtils.throwIf(
Boolean.TRUE.equals(oldStorage.getIsDefault()) && DisEnableStatusEnum.DISABLE.equals(req.getStatus()),
"[{}] 是默认存储库,不允许禁用", oldStorage.getName());
this.unload(oldStorage.getCode());
this.unload(BeanUtil.copyProperties(oldStorage, StorageReq.class));
this.load(req);
super.update(req, id);
}
Expand All @@ -98,7 +102,7 @@ public void update(StorageReq req, Long id) {
public void delete(List<Long> ids) {
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储库存在文件关联,请删除文件后重试");
List<StorageDO> storageList = baseMapper.lambdaQuery().in(StorageDO::getId, ids).list();
storageList.forEach(s -> this.unload(s.getCode()));
storageList.forEach(s -> this.unload(BeanUtil.copyProperties(s, StorageReq.class)));
super.delete(ids);
}

Expand All @@ -123,9 +127,9 @@ public void load(StorageReq req) {
FileStorageProperties.LocalPlusConfig config = new FileStorageProperties.LocalPlusConfig();
config.setPlatform(req.getCode());
config.setStoragePath(bucketName);
config.setDomain(req.getDomain());
fileStorageList
.addAll(FileStorageServiceBuilder.buildLocalPlusFileStorage(Collections.singletonList(config)));
this.registerResource(MapUtil.of(URLUtil.url(req.getDomain()).getPath(), bucketName), false);
}
case S3 -> {
String accessKey = req.getAccessKey();
Expand All @@ -149,11 +153,12 @@ public void load(StorageReq req) {
}

@Override
public void unload(String code) {
public void unload(StorageReq req) {
CopyOnWriteArrayList<FileStorage> fileStorageList = fileStorageService.getFileStorageList();
FileStorage fileStorage = fileStorageService.getFileStorage(code);
FileStorage fileStorage = fileStorageService.getFileStorage(req.getCode());
fileStorageList.remove(fileStorage);
fileStorage.close();
this.registerResource(MapUtil.of(URLUtil.url(req.getDomain()).getPath(), req.getBucketName()), true);
}

/**
Expand All @@ -174,8 +179,10 @@ private boolean isCodeExists(String code, Long id) {
*
* @param registerMapping
* 静态资源映射列表
* @param isCancelRegister
* 是否取消注册映射
*/
private void registerResource(Map<String, String> registerMapping) {
private void registerResource(Map<String, String> registerMapping, boolean isCancelRegister) {
final UrlPathHelper urlPathHelper = applicationContext.getBean("mvcUrlPathHelper", UrlPathHelper.class);
final ContentNegotiationManager contentNegotiationManager =
applicationContext.getBean("mvcContentNegotiationManager", ContentNegotiationManager.class);
Expand All @@ -187,17 +194,22 @@ private void registerResource(Map<String, String> registerMapping) {
(Map<String, Object>)ReflectUtil.getFieldValue(resourceHandlerMapping, "handlerMap");
final ResourceHandlerRegistry resourceHandlerRegistry =
new ResourceHandlerRegistry(applicationContext, servletContext, contentNegotiationManager, urlPathHelper);
// 重新注册静态资源映射
// 重新注册相同 Pattern 的静态资源映射
for (Map.Entry<String, String> entry : registerMapping.entrySet()) {
String pathPattern = StrUtil.appendIfMissing(entry.getKey(), "/**");
String resourceLocations = StrUtil.appendIfMissing(entry.getValue(), "/");
// 移除之前注册过的
String resourceLocations = StrUtil.appendIfMissing(entry.getValue(), StringConstants.SLASH);
// 移除之前注册过的相同 Pattern 映射
handlerMap.remove(pathPattern);
// 重新注册
resourceHandlerRegistry.addResourceHandler(pathPattern).addResourceLocations("file:" + resourceLocations);
if (!isCancelRegister) {
// 重新注册映射
resourceHandlerRegistry.addResourceHandler(pathPattern)
.addResourceLocations("file:" + resourceLocations);
}
}
if (!isCancelRegister) {
final Map<String, ?> additionalUrlMap =
ReflectUtil.<SimpleUrlHandlerMapping>invoke(resourceHandlerRegistry, "getHandlerMapping").getUrlMap();
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
}
final Map<String, ?> additionalUrlMap =
ReflectUtil.<SimpleUrlHandlerMapping>invoke(resourceHandlerRegistry, "getHandlerMapping").getUrlMap();
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ VALUES
INSERT IGNORE INTO `sys_storage`
(`id`, `name`, `code`, `type`, `access_key`, `secret_key`, `endpoint`, `bucket_name`, `domain`, `description`, `is_default`, `sort`, `status`, `create_user`, `create_time`, `update_user`, `update_time`)
VALUES
(1, '本地存储', 'local', 2, NULL, NULL, NULL, '/data/file/', '', '本地存储', b'1', 1, 1, 1, NOW(), NULL, NULL);
(1, '本地存储-开发环境', 'local-dev', 2, NULL, NULL, NULL, 'C:/continew-admin/data/file', 'http://localhost:8000/file', '本地存储-开发环境', b'0', 1, 2, 1, NOW(), NULL, NULL),
(2, '本地存储', 'local', 2, NULL, NULL, NULL, '../data/file/', 'http://api.charles7c.top/file', '本地存储', b'1', 1, 1, 1, NOW(), NULL, NULL);

0 comments on commit 44227ea

Please sign in to comment.