新增备份恢复接口

This commit is contained in:
besscroft
2023-03-09 22:37:30 +08:00
parent 65bab9b215
commit 4913ddc097
8 changed files with 78 additions and 5 deletions

View File

@@ -23,4 +23,6 @@ public interface StorageConverterMapper {
StorageInfoVo StorageToInfoVo(Storage storage); StorageInfoVo StorageToInfoVo(Storage storage);
Storage StorageInfoVoToStorage(StorageInfoVo storageInfoVo);
} }

View File

@@ -20,7 +20,7 @@ public class OpenApiConfiguration {
return new OpenAPI() return new OpenAPI()
.info(new Info().title("DiyFile") .info(new Info().title("DiyFile")
.description("一款好看的在线文件列表程序") .description("一款好看的在线文件列表程序")
.version("v0.3.0") .version("v0.3.1")
.license(new License().name("MIT license").url("https://github.com/besscroft/diyfile/blob/main/LICENSE"))) .license(new License().name("MIT license").url("https://github.com/besscroft/diyfile/blob/main/LICENSE")))
.externalDocs(new ExternalDocumentation() .externalDocs(new ExternalDocumentation()
.description("DiyFile 文档") .description("DiyFile 文档")

View File

@@ -12,9 +12,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @Description 系统监控 * @Description 系统监控
@@ -57,4 +56,17 @@ public class MonitorController {
} }
} }
@SaCheckRole(
value = {
RoleConstants.PLATFORM_SUPER_ADMIN,
},
mode = SaMode.OR
)
@Operation(summary = "恢复数据")
@PostMapping("/restoreData")
public AjaxResult restoreData(@ModelAttribute MultipartFile file) {
systemConfigService.restoreData(file);
return AjaxResult.success("恢复数据成功!");
}
} }

View File

@@ -102,4 +102,10 @@ public interface StorageService extends IService<Storage> {
*/ */
Long getDefaultStorageId(); Long getDefaultStorageId();
/**
* 保存存储列表
* @param storageInfoVoList 存储列表
*/
void saveStorageInfoVoList(List<StorageInfoVo> storageInfoVoList);
} }

View File

@@ -3,6 +3,7 @@ package com.besscroft.diyfile.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.besscroft.diyfile.common.entity.SystemConfig; import com.besscroft.diyfile.common.entity.SystemConfig;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.multipart.MultipartFile;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -64,4 +65,6 @@ public interface SystemConfigService extends IService<SystemConfig> {
*/ */
String getBackupJsonString() throws JsonProcessingException; String getBackupJsonString() throws JsonProcessingException;
void restoreData(MultipartFile file);
} }

View File

@@ -214,4 +214,28 @@ public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> impl
return vo.getId(); return vo.getId();
} }
@Override
@CacheEvict(value = {
CacheConstants.DEFAULT_STORAGE,
CacheConstants.STORAGE_ID,
CacheConstants.STORAGE_KEY,
CacheConstants.ENABLE_STORAGE,
CacheConstants.STATISTICS
}, allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void saveStorageInfoVoList(List<StorageInfoVo> storageInfoVoList) {
for (StorageInfoVo storageInfoVo : storageInfoVoList) {
Storage storage = StorageConverterMapper.INSTANCE.StorageInfoVoToStorage(storageInfoVo);
if (Objects.isNull(storage)) throw new DiyFileException("存储信息导入失败!");
storage.setId(null);
this.save(storage);
List<StorageConfig> configList = storageInfoVo.getConfigList();
for (StorageConfig config : configList) {
config.setId(null);
config.setStorageId(storage.getId());
}
storageConfigService.saveBatch(configList);
}
}
} }

View File

@@ -13,6 +13,7 @@ import com.besscroft.diyfile.mapper.UserMapper;
import com.besscroft.diyfile.service.StorageService; import com.besscroft.diyfile.service.StorageService;
import com.besscroft.diyfile.service.SystemConfigService; import com.besscroft.diyfile.service.SystemConfigService;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
@@ -21,7 +22,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -107,4 +111,26 @@ public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, Sys
return objectMapper.writeValueAsString(map); return objectMapper.writeValueAsString(map);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void restoreData(MultipartFile file) {
try {
InputStream inputStream = file.getInputStream();
Map<String, Object> map = objectMapper.readValue(inputStream, new TypeReference<>() {
});
if (map.containsKey("systemConfig")) {
List<SystemConfig> list = objectMapper.convertValue(map.get("systemConfig"), new TypeReference<>() {
});
this.saveOrUpdateBatch(list);
}
if (map.containsKey("storageInfo")) {
List<StorageInfoVo> list = objectMapper.convertValue(map.get("storageInfo"), new TypeReference<>() {
});
storageService.saveStorageInfoVoList(list);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }

View File

@@ -17,7 +17,7 @@
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>
<revision>0.3.0</revision> <revision>0.3.1</revision>
<java.version>17</java.version> <java.version>17</java.version>
<maven.compiler.release>17</maven.compiler.release> <maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${java.version}</maven.compiler.source>