refactor: 完善存储库业务逻辑
This commit is contained in:
parent
3399bc8dde
commit
44227eab8f
@ -140,7 +140,7 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
userSocial.setOpenId(openId);
|
userSocial.setOpenId(openId);
|
||||||
this.sendSystemMsg(user);
|
this.sendSystemMsg(user);
|
||||||
} else {
|
} else {
|
||||||
user = BeanUtil.toBean(userService.get(userSocial.getUserId()), UserDO.class);
|
user = BeanUtil.copyProperties(userService.get(userSocial.getUserId()), UserDO.class);
|
||||||
}
|
}
|
||||||
this.checkUserStatus(user);
|
this.checkUserStatus(user);
|
||||||
userSocial.setMetaJson(JSONUtil.toJsonStr(authUser));
|
userSocial.setMetaJson(JSONUtil.toJsonStr(authUser));
|
||||||
|
@ -28,6 +28,8 @@ import org.springframework.stereotype.Component;
|
|||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
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.req.StorageReq;
|
||||||
import top.charles7c.continew.admin.system.model.resp.StorageResp;
|
import top.charles7c.continew.admin.system.model.resp.StorageResp;
|
||||||
import top.charles7c.continew.admin.system.service.StorageService;
|
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
|
* @since 2023/12/24 22:31
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
//@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class FileStorageConfigLoader implements ApplicationRunner {
|
public class FileStorageConfigLoader implements ApplicationRunner {
|
||||||
|
|
||||||
@ -47,7 +49,9 @@ public class FileStorageConfigLoader implements ApplicationRunner {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) {
|
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)) {
|
if (CollUtil.isEmpty(storageList)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ import top.charles7c.continew.starter.extension.crud.base.BaseService;
|
|||||||
public interface FileService extends BaseService<FileResp, FileDetailResp, FileQuery, FileReq> {
|
public interface FileService extends BaseService<FileResp, FileDetailResp, FileQuery, FileReq> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传
|
* 上传到默认存储库
|
||||||
*
|
*
|
||||||
* @param file
|
* @param file
|
||||||
* 文件信息
|
* 文件信息
|
||||||
@ -45,7 +45,7 @@ public interface FileService extends BaseService<FileResp, FileDetailResp, FileQ
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传
|
* 上传到指定存储库
|
||||||
*
|
*
|
||||||
* @param file
|
* @param file
|
||||||
* 文件信息
|
* 文件信息
|
||||||
|
@ -58,8 +58,8 @@ public interface StorageService extends BaseService<StorageResp, StorageDetailRe
|
|||||||
/**
|
/**
|
||||||
* 卸载存储库
|
* 卸载存储库
|
||||||
*
|
*
|
||||||
* @param code
|
* @param req
|
||||||
* 编码
|
* 存储库信息
|
||||||
*/
|
*/
|
||||||
void unload(String code);
|
void unload(StorageReq req);
|
||||||
}
|
}
|
@ -28,6 +28,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
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.mapper.FileMapper;
|
||||||
import top.charles7c.continew.admin.system.model.entity.FileDO;
|
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.req.FileReq;
|
||||||
import top.charles7c.continew.admin.system.model.resp.FileDetailResp;
|
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.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.FileService;
|
||||||
import top.charles7c.continew.admin.system.service.StorageService;
|
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.core.util.validate.CheckUtils;
|
||||||
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
|
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) {
|
public Long countByStorageIds(List<Long> storageIds) {
|
||||||
return baseMapper.lambdaQuery().in(FileDO::getStorageId, storageIds).count();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -38,8 +38,11 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
|
|||||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
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.ReflectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
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.common.enums.DisEnableStatusEnum;
|
||||||
import top.charles7c.continew.admin.system.enums.StorageTypeEnum;
|
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.model.resp.StorageResp;
|
||||||
import top.charles7c.continew.admin.system.service.FileService;
|
import top.charles7c.continew.admin.system.service.FileService;
|
||||||
import top.charles7c.continew.admin.system.service.StorageService;
|
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.CheckUtils;
|
||||||
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
||||||
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
|
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
|
||||||
@ -89,7 +93,7 @@ public class StorageServiceImpl
|
|||||||
CheckUtils.throwIf(
|
CheckUtils.throwIf(
|
||||||
Boolean.TRUE.equals(oldStorage.getIsDefault()) && DisEnableStatusEnum.DISABLE.equals(req.getStatus()),
|
Boolean.TRUE.equals(oldStorage.getIsDefault()) && DisEnableStatusEnum.DISABLE.equals(req.getStatus()),
|
||||||
"[{}] 是默认存储库,不允许禁用", oldStorage.getName());
|
"[{}] 是默认存储库,不允许禁用", oldStorage.getName());
|
||||||
this.unload(oldStorage.getCode());
|
this.unload(BeanUtil.copyProperties(oldStorage, StorageReq.class));
|
||||||
this.load(req);
|
this.load(req);
|
||||||
super.update(req, id);
|
super.update(req, id);
|
||||||
}
|
}
|
||||||
@ -98,7 +102,7 @@ public class StorageServiceImpl
|
|||||||
public void delete(List<Long> ids) {
|
public void delete(List<Long> ids) {
|
||||||
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储库存在文件关联,请删除文件后重试");
|
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储库存在文件关联,请删除文件后重试");
|
||||||
List<StorageDO> storageList = baseMapper.lambdaQuery().in(StorageDO::getId, ids).list();
|
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);
|
super.delete(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,9 +127,9 @@ public class StorageServiceImpl
|
|||||||
FileStorageProperties.LocalPlusConfig config = new FileStorageProperties.LocalPlusConfig();
|
FileStorageProperties.LocalPlusConfig config = new FileStorageProperties.LocalPlusConfig();
|
||||||
config.setPlatform(req.getCode());
|
config.setPlatform(req.getCode());
|
||||||
config.setStoragePath(bucketName);
|
config.setStoragePath(bucketName);
|
||||||
config.setDomain(req.getDomain());
|
|
||||||
fileStorageList
|
fileStorageList
|
||||||
.addAll(FileStorageServiceBuilder.buildLocalPlusFileStorage(Collections.singletonList(config)));
|
.addAll(FileStorageServiceBuilder.buildLocalPlusFileStorage(Collections.singletonList(config)));
|
||||||
|
this.registerResource(MapUtil.of(URLUtil.url(req.getDomain()).getPath(), bucketName), false);
|
||||||
}
|
}
|
||||||
case S3 -> {
|
case S3 -> {
|
||||||
String accessKey = req.getAccessKey();
|
String accessKey = req.getAccessKey();
|
||||||
@ -149,11 +153,12 @@ public class StorageServiceImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unload(String code) {
|
public void unload(StorageReq req) {
|
||||||
CopyOnWriteArrayList<FileStorage> fileStorageList = fileStorageService.getFileStorageList();
|
CopyOnWriteArrayList<FileStorage> fileStorageList = fileStorageService.getFileStorageList();
|
||||||
FileStorage fileStorage = fileStorageService.getFileStorage(code);
|
FileStorage fileStorage = fileStorageService.getFileStorage(req.getCode());
|
||||||
fileStorageList.remove(fileStorage);
|
fileStorageList.remove(fileStorage);
|
||||||
fileStorage.close();
|
fileStorage.close();
|
||||||
|
this.registerResource(MapUtil.of(URLUtil.url(req.getDomain()).getPath(), req.getBucketName()), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,8 +179,10 @@ public class StorageServiceImpl
|
|||||||
*
|
*
|
||||||
* @param registerMapping
|
* @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 UrlPathHelper urlPathHelper = applicationContext.getBean("mvcUrlPathHelper", UrlPathHelper.class);
|
||||||
final ContentNegotiationManager contentNegotiationManager =
|
final ContentNegotiationManager contentNegotiationManager =
|
||||||
applicationContext.getBean("mvcContentNegotiationManager", ContentNegotiationManager.class);
|
applicationContext.getBean("mvcContentNegotiationManager", ContentNegotiationManager.class);
|
||||||
@ -187,17 +194,22 @@ public class StorageServiceImpl
|
|||||||
(Map<String, Object>)ReflectUtil.getFieldValue(resourceHandlerMapping, "handlerMap");
|
(Map<String, Object>)ReflectUtil.getFieldValue(resourceHandlerMapping, "handlerMap");
|
||||||
final ResourceHandlerRegistry resourceHandlerRegistry =
|
final ResourceHandlerRegistry resourceHandlerRegistry =
|
||||||
new ResourceHandlerRegistry(applicationContext, servletContext, contentNegotiationManager, urlPathHelper);
|
new ResourceHandlerRegistry(applicationContext, servletContext, contentNegotiationManager, urlPathHelper);
|
||||||
// 重新注册静态资源映射
|
// 重新注册相同 Pattern 的静态资源映射
|
||||||
for (Map.Entry<String, String> entry : registerMapping.entrySet()) {
|
for (Map.Entry<String, String> entry : registerMapping.entrySet()) {
|
||||||
String pathPattern = StrUtil.appendIfMissing(entry.getKey(), "/**");
|
String pathPattern = StrUtil.appendIfMissing(entry.getKey(), "/**");
|
||||||
String resourceLocations = StrUtil.appendIfMissing(entry.getValue(), "/");
|
String resourceLocations = StrUtil.appendIfMissing(entry.getValue(), StringConstants.SLASH);
|
||||||
// 移除之前注册过的
|
// 移除之前注册过的相同 Pattern 映射
|
||||||
handlerMap.remove(pathPattern);
|
handlerMap.remove(pathPattern);
|
||||||
// 重新注册
|
if (!isCancelRegister) {
|
||||||
resourceHandlerRegistry.addResourceHandler(pathPattern).addResourceLocations("file:" + resourceLocations);
|
// 重新注册映射
|
||||||
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,4 +20,5 @@ VALUES
|
|||||||
INSERT IGNORE INTO `sys_storage`
|
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`)
|
(`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
|
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);
|
Loading…
Reference in New Issue
Block a user