refactor: 完善存储库业务逻辑

This commit is contained in:
Charles7c 2023-12-29 00:22:47 +08:00
parent 3399bc8dde
commit 44227eab8f
7 changed files with 53 additions and 23 deletions

View File

@ -140,7 +140,7 @@ public class LoginServiceImpl implements LoginService {
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));

View File

@ -28,6 +28,8 @@ import org.springframework.stereotype.Component;
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;
@ -39,7 +41,7 @@ import top.charles7c.continew.admin.system.service.StorageService;
* @since 2023/12/24 22:31
*/
@Slf4j
@Component
//@Component
@RequiredArgsConstructor
public class FileStorageConfigLoader implements ApplicationRunner {
@ -47,7 +49,9 @@ public class FileStorageConfigLoader implements ApplicationRunner {
@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;
}

View File

@ -35,7 +35,7 @@ import top.charles7c.continew.starter.extension.crud.base.BaseService;
public interface FileService extends BaseService<FileResp, FileDetailResp, FileQuery, FileReq> {
/**
* 上传
* 上传到默认存储库
*
* @param file
* 文件信息
@ -45,7 +45,7 @@ public interface FileService extends BaseService<FileResp, FileDetailResp, FileQ
}
/**
* 上传
* 上传到指定存储库
*
* @param file
* 文件信息

View File

@ -58,8 +58,8 @@ public interface StorageService extends BaseService<StorageResp, StorageDetailRe
/**
* 卸载存储库
*
* @param code
* 编码
* @param req
* 存储库信息
*/
void unload(String code);
void unload(StorageReq req);
}

View File

@ -28,6 +28,7 @@ import org.springframework.stereotype.Service;
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;
@ -36,8 +37,11 @@ import top.charles7c.continew.admin.system.model.query.FileQuery;
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;
@ -88,4 +92,13 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
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);
}
}

View File

@ -38,8 +38,11 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
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;
@ -51,6 +54,7 @@ import top.charles7c.continew.admin.system.model.resp.StorageDetailResp;
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;
@ -89,7 +93,7 @@ public class StorageServiceImpl
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);
}
@ -98,7 +102,7 @@ public class StorageServiceImpl
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);
}
@ -123,9 +127,9 @@ public class StorageServiceImpl
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();
@ -149,11 +153,12 @@ public class StorageServiceImpl
}
@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);
}
/**
@ -174,8 +179,10 @@ public class StorageServiceImpl
*
* @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);
@ -187,17 +194,22 @@ public class StorageServiceImpl
(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);
}
}

View File

@ -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);