133 lines
4.0 KiB
Java
133 lines
4.0 KiB
Java
package com.designhub.controller;
|
|
|
|
import com.designhub.entity.S3Config;
|
|
import com.designhub.service.S3ConfigService;
|
|
import com.designhub.common.Result;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* S3配置管理Controller
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/s3/config")
|
|
public class S3ConfigController {
|
|
|
|
@Autowired
|
|
private S3ConfigService s3ConfigService;
|
|
|
|
/**
|
|
* 获取激活的S3配置
|
|
*/
|
|
@GetMapping("/active")
|
|
public Result<S3Config> getActiveConfig() {
|
|
try {
|
|
S3Config config = s3ConfigService.getActiveConfig();
|
|
// 隐藏敏感信息
|
|
if (config != null) {
|
|
config.setSecretAccessKey("******");
|
|
}
|
|
return Result.success(config);
|
|
} catch (Exception e) {
|
|
return Result.error("获取S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取所有S3配置
|
|
*/
|
|
@GetMapping("/list")
|
|
public Result<List<S3Config>> getAllConfigs() {
|
|
try {
|
|
List<S3Config> configs = s3ConfigService.getAllConfigs();
|
|
// 隐藏敏感信息
|
|
configs.forEach(config -> config.setSecretAccessKey("******"));
|
|
return Result.success(configs);
|
|
} catch (Exception e) {
|
|
return Result.error("获取S3配置列表失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据ID获取S3配置
|
|
*/
|
|
@GetMapping("/{id}")
|
|
public Result<S3Config> getConfigById(@PathVariable Long id) {
|
|
try {
|
|
S3Config config = s3ConfigService.getConfigById(id);
|
|
if (config == null) {
|
|
return Result.error("S3配置不存在");
|
|
}
|
|
// 隐藏敏感信息
|
|
config.setSecretAccessKey("******");
|
|
return Result.success(config);
|
|
} catch (Exception e) {
|
|
return Result.error("获取S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建S3配置
|
|
*/
|
|
@PostMapping
|
|
public Result<S3Config> createConfig(@RequestBody S3Config config) {
|
|
try {
|
|
S3Config created = s3ConfigService.createConfig(config);
|
|
created.setSecretAccessKey("******");
|
|
return Result.success(created);
|
|
} catch (Exception e) {
|
|
return Result.error("创建S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新S3配置
|
|
*/
|
|
@PutMapping("/{id}")
|
|
public Result<S3Config> updateConfig(@PathVariable Long id, @RequestBody S3Config config) {
|
|
try {
|
|
// 如果secretAccessKey为"******",说明没有修改,从数据库中获取原值
|
|
if ("******".equals(config.getSecretAccessKey())) {
|
|
S3Config existingConfig = s3ConfigService.getConfigById(id);
|
|
if (existingConfig != null) {
|
|
config.setSecretAccessKey(existingConfig.getSecretAccessKey());
|
|
}
|
|
}
|
|
|
|
S3Config updated = s3ConfigService.updateConfig(id, config);
|
|
updated.setSecretAccessKey("******");
|
|
return Result.success(updated);
|
|
} catch (Exception e) {
|
|
return Result.error("更新S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除S3配置
|
|
*/
|
|
@DeleteMapping("/{id}")
|
|
public Result<Void> deleteConfig(@PathVariable Long id) {
|
|
try {
|
|
s3ConfigService.deleteConfig(id);
|
|
return Result.success();
|
|
} catch (Exception e) {
|
|
return Result.error("删除S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 激活指定的S3配置
|
|
*/
|
|
@PutMapping("/{id}/activate")
|
|
public Result<Void> activateConfig(@PathVariable Long id) {
|
|
try {
|
|
s3ConfigService.activateConfig(id);
|
|
return Result.success();
|
|
} catch (Exception e) {
|
|
return Result.error("激活S3配置失败: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|