diff --git a/src/main/java/im/zhaojun/zfile/module/storage/controller/base/StorageSourceController.java b/src/main/java/im/zhaojun/zfile/module/storage/controller/base/StorageSourceController.java index b6896bb..decc72e 100644 --- a/src/main/java/im/zhaojun/zfile/module/storage/controller/base/StorageSourceController.java +++ b/src/main/java/im/zhaojun/zfile/module/storage/controller/base/StorageSourceController.java @@ -3,6 +3,7 @@ package im.zhaojun.zfile.module.storage.controller.base; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.github.xiaoymin.knife4j.annotations.ApiSort; import im.zhaojun.zfile.module.storage.model.entity.StorageSource; +import im.zhaojun.zfile.module.storage.model.request.admin.CopyStorageSourceRequest; import im.zhaojun.zfile.module.storage.model.request.base.SaveStorageSourceRequest; import im.zhaojun.zfile.module.storage.model.result.StorageSourceAdminResult; import im.zhaojun.zfile.module.storage.service.StorageSourceService; @@ -24,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import javax.validation.Valid; import java.util.List; /** @@ -146,4 +148,14 @@ public class StorageSourceController { storageSourceService.updateById(storageSource); return AjaxJson.getSuccess(); } + + + @ApiOperationSupport(order = 10) + @ApiOperation(value = "复制存储源", notes = "复制存储源配置") + @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class) + @PostMapping("/storage/copy") + public AjaxJson copyStorage(@RequestBody @Valid CopyStorageSourceRequest copyStorageSourceRequest) { + Integer id = storageSourceService.copy(copyStorageSourceRequest); + return AjaxJson.getSuccessData(id); + } } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/module/storage/model/request/admin/CopyStorageSourceRequest.java b/src/main/java/im/zhaojun/zfile/module/storage/model/request/admin/CopyStorageSourceRequest.java new file mode 100644 index 0000000..da216a1 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/module/storage/model/request/admin/CopyStorageSourceRequest.java @@ -0,0 +1,33 @@ +package im.zhaojun.zfile.module.storage.model.request.admin; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * 复制存储源请求参数 + * + * @author zhaojun + */ +@Data +@ApiModel(description = "复制存储源请求请求类") +public class CopyStorageSourceRequest { + + @ApiModelProperty(value = "存储源 ID", required = true, example = "1") + @NotNull(message = "存储源 id 不能为空") + private Integer fromId; + + @ApiModelProperty(value = "复制后存储源名称", required = true, example = "1") + @NotBlank(message = "复制后存储源名称不能为空") + private String toName; + + @ApiModelProperty(value = "复制后存储源别名", required = true, example = "1") + @NotBlank(message = "复制后存储源别名不能为空") + private String toKey; + + + +} diff --git a/src/main/java/im/zhaojun/zfile/module/storage/service/StorageSourceService.java b/src/main/java/im/zhaojun/zfile/module/storage/service/StorageSourceService.java index 2784317..34c5874 100644 --- a/src/main/java/im/zhaojun/zfile/module/storage/service/StorageSourceService.java +++ b/src/main/java/im/zhaojun/zfile/module/storage/service/StorageSourceService.java @@ -6,10 +6,12 @@ import cn.hutool.core.util.StrUtil; import im.zhaojun.zfile.core.exception.StorageSourceException; import im.zhaojun.zfile.core.exception.file.InvalidStorageSourceException; import im.zhaojun.zfile.core.util.CodeMsg; +import im.zhaojun.zfile.module.filter.model.entity.FilterConfig; import im.zhaojun.zfile.module.filter.service.FilterConfigService; import im.zhaojun.zfile.module.link.service.ShortLinkService; import im.zhaojun.zfile.module.log.service.DownloadLogService; import im.zhaojun.zfile.module.password.model.dto.VerifyResultDTO; +import im.zhaojun.zfile.module.password.model.entity.PasswordConfig; import im.zhaojun.zfile.module.password.service.PasswordConfigService; import im.zhaojun.zfile.module.readme.model.entity.ReadmeConfig; import im.zhaojun.zfile.module.readme.service.ReadmeConfigService; @@ -21,11 +23,13 @@ import im.zhaojun.zfile.module.storage.model.dto.StorageSourceDTO; import im.zhaojun.zfile.module.storage.model.entity.StorageSource; import im.zhaojun.zfile.module.storage.model.entity.StorageSourceConfig; import im.zhaojun.zfile.module.storage.model.enums.StorageTypeEnum; +import im.zhaojun.zfile.module.storage.model.request.admin.CopyStorageSourceRequest; import im.zhaojun.zfile.module.storage.model.request.admin.UpdateStorageSortRequest; import im.zhaojun.zfile.module.storage.model.request.base.FileListConfigRequest; import im.zhaojun.zfile.module.storage.model.request.base.SaveStorageSourceRequest; import im.zhaojun.zfile.module.storage.model.result.StorageSourceConfigResult; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; @@ -389,6 +393,77 @@ public class StorageSourceService { // 获取指定存储源路径下的 readme 信息 return storageSourceConvert.entityToConfigResult(storageSource, readmeByPath); } - + + @Transactional(rollbackFor = Exception.class) + public Integer copy(CopyStorageSourceRequest copyStorageSourceRequest) { + String toKey = copyStorageSourceRequest.getToKey(); + boolean existByStorageKey = storageSourceService.existByStorageKey(toKey); + if (existByStorageKey) { + throw new RuntimeException("存储源别名已存在: " + toKey); + } + Integer fromStorageId = copyStorageSourceRequest.getFromId(); + // 复制存储源 + StorageSource storageSource = storageSourceService.findById(fromStorageId); + if (storageSource == null) { + throw new InvalidStorageSourceException("通过存储源 id 未找到存储源, id: " + fromStorageId); + } + StorageSource newStorageSource = new StorageSource(); + BeanUtils.copyProperties(storageSource, newStorageSource); + newStorageSource.setId(null); + newStorageSource.setKey(copyStorageSourceRequest.getToKey()); + newStorageSource.setName(copyStorageSourceRequest.getToName()); + StorageSource dbSaveResult = storageSourceService.saveOrUpdate(newStorageSource); + log.info("复制存储源成功,复制后 id: {}, name: {}, key: {}, type: {}", + dbSaveResult.getId(), dbSaveResult.getName(), + dbSaveResult.getKey(), dbSaveResult.getType().getDescription()); + + // 复制存储源基础参数 + Integer newStorageId = dbSaveResult.getId(); + List storageSourceConfigList = storageSourceConfigService.selectStorageConfigByStorageId(fromStorageId); + storageSourceConfigList.forEach(storageSourceConfig -> { + storageSourceConfig.setId(null); + storageSourceConfig.setStorageId(newStorageId); + }); + storageSourceConfigService.saveBatch(newStorageId, storageSourceConfigList); + log.info("保存复制后的存储源参数成功,尝试根据参数初始化存储源, id: {}, name: {}, config size: {}", + dbSaveResult.getId(), dbSaveResult.getName(), storageSourceConfigList.size()); + + // 初始化存储源 + storageSourceContext.init(dbSaveResult); + log.info("初始化存储源成功, id: {}, name: {}, config size: {}", + dbSaveResult.getId(), dbSaveResult.getName(), storageSourceConfigList.size()); + + // 复制存储源过滤配置 + List filterConfigList = filterConfigService.findByStorageId(fromStorageId); + filterConfigList.forEach(filterConfig -> { + filterConfig.setId(null); + filterConfig.setStorageId(newStorageId); + }); + filterConfigService.batchSave(newStorageId, filterConfigList); + log.info("复制存储源过滤配置成功, id: {}, name: {}, config size: {}", + dbSaveResult.getId(), dbSaveResult.getName(), storageSourceConfigList.size()); + + // 复制存储源密码配置 + List passwordConfigList = passwordConfigService.findByStorageId(fromStorageId); + passwordConfigList.forEach(passwordConfig -> { + passwordConfig.setId(null); + passwordConfig.setStorageId(newStorageId); + }); + passwordConfigService.batchSave(newStorageId, passwordConfigList); + log.info("复制存储源密码配置成功, id: {}, name: {}, config size: {}", + dbSaveResult.getId(), dbSaveResult.getName(), storageSourceConfigList.size()); + + // 复制存储源 readme 配置 + List readmeConfigList = readmeConfigService.findByStorageId(fromStorageId); + readmeConfigList.forEach(readmeConfig -> { + readmeConfig.setId(null); + readmeConfig.setStorageId(newStorageId); + }); + readmeConfigService.batchSave(newStorageId, readmeConfigList); + log.info("复制存储源 readme 配置成功, id: {}, name: {}, config size: {}", + dbSaveResult.getId(), dbSaveResult.getName(), storageSourceConfigList.size()); + + return dbSaveResult.getId(); + } } \ No newline at end of file