diff --git a/continew-admin-system/src/main/java/top/charles7c/continew/admin/auth/service/impl/LoginServiceImpl.java b/continew-admin-system/src/main/java/top/charles7c/continew/admin/auth/service/impl/LoginServiceImpl.java index 12a57d0a..350e1890 100644 --- a/continew-admin-system/src/main/java/top/charles7c/continew/admin/auth/service/impl/LoginServiceImpl.java +++ b/continew-admin-system/src/main/java/top/charles7c/continew/admin/auth/service/impl/LoginServiceImpl.java @@ -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)); diff --git a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/config/FileStorageConfigLoader.java b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/config/FileStorageConfigLoader.java index 22af9679..955f8c70 100644 --- a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/config/FileStorageConfigLoader.java +++ b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/config/FileStorageConfigLoader.java @@ -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 storageList = storageService.list(null, null); + StorageQuery query = new StorageQuery(); + query.setStatus(DisEnableStatusEnum.ENABLE.getValue()); + List storageList = storageService.list(query, null); if (CollUtil.isEmpty(storageList)) { return; } diff --git a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/FileService.java b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/FileService.java index d0daa2c3..6debb373 100644 --- a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/FileService.java +++ b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/FileService.java @@ -35,7 +35,7 @@ import top.charles7c.continew.starter.extension.crud.base.BaseService; public interface FileService extends BaseService { /** - * 上传 + * 上传到默认存储库 * * @param file * 文件信息 @@ -45,7 +45,7 @@ public interface FileService extends BaseService 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); + } } \ No newline at end of file diff --git a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/StorageServiceImpl.java b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/StorageServiceImpl.java index 98c4189e..989eaca3 100644 --- a/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/StorageServiceImpl.java +++ b/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/StorageServiceImpl.java @@ -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 ids) { CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储库存在文件关联,请删除文件后重试"); List 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 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 registerMapping) { + private void registerResource(Map 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)ReflectUtil.getFieldValue(resourceHandlerMapping, "handlerMap"); final ResourceHandlerRegistry resourceHandlerRegistry = new ResourceHandlerRegistry(applicationContext, servletContext, contentNegotiationManager, urlPathHelper); - // 重新注册静态资源映射 + // 重新注册相同 Pattern 的静态资源映射 for (Map.Entry 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 additionalUrlMap = + ReflectUtil.invoke(resourceHandlerRegistry, "getHandlerMapping").getUrlMap(); + ReflectUtil.invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap); } - final Map additionalUrlMap = - ReflectUtil.invoke(resourceHandlerRegistry, "getHandlerMapping").getUrlMap(); - ReflectUtil.invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap); } } \ No newline at end of file diff --git a/continew-admin-webapi/src/main/resources/db/changelog/v2.2.0/continew-admin_data.sql b/continew-admin-webapi/src/main/resources/db/changelog/v2.2.0/continew-admin_data.sql index bf29ff69..18da95fc 100644 --- a/continew-admin-webapi/src/main/resources/db/changelog/v2.2.0/continew-admin_data.sql +++ b/continew-admin-webapi/src/main/resources/db/changelog/v2.2.0/continew-admin_data.sql @@ -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); \ No newline at end of file +(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); \ No newline at end of file