优化文件配置,去掉 region 的配置,通过自动识别

This commit is contained in:
YunaiV 2022-03-19 18:02:20 +08:00
parent 34a7399a65
commit 813069abf4
21 changed files with 28 additions and 43 deletions

View File

@ -40,12 +40,6 @@ public class S3FileClientConfig implements FileClientConfig {
*/
@URL(message = "domain 必须是 URL 格式")
private String domain;
/**
* 区域
*/
// @NotNull(message = "region 不能为空")
@Deprecated
private String region;
/**
* 存储 Bucket
*/

View File

@ -21,7 +21,6 @@ public class S3FileClientTest {
config.setDomain(null);
// 默认 9000 endpoint
config.setEndpoint("http://127.0.0.1:9000");
config.setRegion("us-east-1");
// 执行上传
testExecuteUpload(config);

View File

@ -15,7 +15,7 @@ public interface FileApi {
* @param content 文件内容
* @return 文件路径
*/
default String createFile(byte[] content) {
default String createFile(byte[] content) throws Exception {
return createFile(IdUtil.fastUUID(), content);
}
@ -26,6 +26,6 @@ public interface FileApi {
* @param content 文件内容
* @return 文件路径
*/
String createFile(String path, byte[] content);
String createFile(String path, byte[] content) throws Exception;
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.infra.api.file;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -20,7 +19,7 @@ public class FileApiImpl implements FileApi {
private FileService fileService;
@Override
public String createFile(String path, byte[] content) {
public String createFile(String path, byte[] content) throws Exception {
return fileService.createFile(path, content);
}

View File

@ -82,7 +82,7 @@ public class FileConfigController {
@GetMapping("/test")
@ApiOperation("测试文件配置是否正确")
@PreAuthorize("@ss.hasPermission('infra:file-config:query')")
public CommonResult<String> testFileConfig(@RequestParam("id") Long id) {
public CommonResult<String> testFileConfig(@RequestParam("id") Long id) throws Exception {
String url = fileConfigService.testFileConfig(id);
return success(url);
}

View File

@ -23,7 +23,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -44,7 +43,7 @@ public class FileController {
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
})
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("path") String path) throws IOException {
@RequestParam("path") String path) throws Exception {
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
}
@ -52,7 +51,7 @@ public class FileController {
@ApiOperation("删除文件")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('infra:file:delete')")
public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) {
public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) throws Exception {
fileService.deleteFile(id);
return success(true);
}
@ -65,7 +64,7 @@ public class FileController {
})
public void getFileContent(HttpServletResponse response,
@PathVariable("configId") Long configId,
@PathVariable("path") String path) throws IOException {
@PathVariable("path") String path) throws Exception {
byte[] content = fileService.getFileContent(configId, path);
if (content == null) {
log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path);

View File

@ -82,7 +82,7 @@ public interface FileConfigService {
* @param id 编号
* @return 文件 URL
*/
String testFileConfig(Long id);
String testFileConfig(Long id) throws Exception;
/**
* 获得指定编号的文件客户端

View File

@ -225,7 +225,7 @@ public class FileConfigServiceImpl implements FileConfigService {
}
@Override
public String testFileConfig(Long id) {
public String testFileConfig(Long id) throws Exception {
// 校验存在
this.validateFileConfigExists(id);
// 上传文件

View File

@ -26,14 +26,14 @@ public interface FileService {
* @param content 文件内容
* @return 文件路径
*/
String createFile(String path, byte[] content);
String createFile(String path, byte[] content) throws Exception;
/**
* 删除文件
*
* @param id 编号
*/
void deleteFile(Long id);
void deleteFile(Long id) throws Exception;
/**
* 获得文件内容
@ -42,6 +42,6 @@ public interface FileService {
* @param path 文件路径
* @return 文件内容
*/
byte[] getFileContent(Long configId, String path);
byte[] getFileContent(Long configId, String path) throws Exception;
}

View File

@ -35,7 +35,7 @@ public class FileServiceImpl implements FileService {
}
@Override
public String createFile(String path, byte[] content) {
public String createFile(String path, byte[] content) throws Exception {
// 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient();
Assert.notNull(client, "客户端(master) 不能为空");
@ -53,7 +53,7 @@ public class FileServiceImpl implements FileService {
}
@Override
public void deleteFile(Long id) {
public void deleteFile(Long id) throws Exception {
// 校验存在
FileDO file = this.validateFileExists(id);
@ -75,7 +75,7 @@ public class FileServiceImpl implements FileService {
}
@Override
public byte[] getFileContent(Long configId, String path) {
public byte[] getFileContent(Long configId, String path) throws Exception {
FileClient client = fileConfigService.getFileClient(configId);
Assert.notNull(client, "客户端({}) 不能为空", configId);
return client.getContent(path);

View File

@ -228,7 +228,7 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testFileConfig() {
public void testFileConfig() throws Exception {
// mock 数据
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据

View File

@ -70,7 +70,7 @@ public class FileServiceTest extends BaseDbUnitTest {
}
@Test
public void testCreateFile_success() {
public void testCreateFile_success() throws Exception {
// 准备参数
String path = randomString();
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
@ -95,7 +95,7 @@ public class FileServiceTest extends BaseDbUnitTest {
}
@Test
public void testDeleteFile_success() {
public void testDeleteFile_success() throws Exception {
// mock 数据
FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("tudou.jpg"));
fileMapper.insert(dbFile);// @Sql: 先插入出一条存在的数据
@ -123,7 +123,7 @@ public class FileServiceTest extends BaseDbUnitTest {
}
@Test
public void testGetFileContent() {
public void testGetFileContent() throws Exception {
// 准备参数
Long configId = 10L;
String path = "tudou.jpg";

View File

@ -16,9 +16,8 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;
@ -44,7 +43,7 @@ public class AppUserController {
@PutMapping("/update-avatar")
@ApiOperation("修改用户头像")
@PreAuthenticated
public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
if (file.isEmpty()) {
throw exception(FILE_IS_EMPTY);
}

View File

@ -60,7 +60,7 @@ public interface MemberUserService {
* @param inputStream 头像文件
* @return 头像url
*/
String updateUserAvatar(Long userId, InputStream inputStream);
String updateUserAvatar(Long userId, InputStream inputStream) throws Exception;
/**
* 修改手机

View File

@ -100,7 +100,7 @@ public class MemberUserServiceImpl implements MemberUserService {
}
@Override
public String updateUserAvatar(Long userId, InputStream avatarFile) {
public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception {
this.checkUserExists(userId);
// 创建文件
String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));

View File

@ -74,7 +74,7 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest {
}
@Test
public void testUpdateAvatar_success(){
public void testUpdateAvatar_success() throws Exception {
// mock 数据
MemberUserDO dbUser = randomUserDO();
userMapper.insert(dbUser);

View File

@ -29,7 +29,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -99,7 +98,7 @@ public class UserProfileController {
@PutMapping("/update-avatar")
@ApiOperation("上传用户个人头像")
public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
if (file.isEmpty()) {
throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
}

View File

@ -64,7 +64,7 @@ public interface AdminUserService {
* @param id 用户 id
* @param avatarFile 头像文件
*/
String updateUserAvatar(Long id, InputStream avatarFile);
String updateUserAvatar(Long id, InputStream avatarFile) throws Exception;
/**
* 修改密码

View File

@ -118,7 +118,7 @@ public class AdminUserServiceImpl implements AdminUserService {
}
@Override
public String updateUserAvatar(Long id, InputStream avatarFile) {
public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception {
this.checkUserExists(id);
// 存储文件
String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));

View File

@ -196,7 +196,7 @@ public class UserServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testUpdateUserAvatar_success() {
public void testUpdateUserAvatar_success() throws Exception {
// mock 数据
AdminUserDO dbUser = randomAdminUserDO();
userMapper.insert(dbUser);

View File

@ -109,9 +109,6 @@
<el-form-item v-if="form.storage === 20" label="节点地址" prop="config.endpoint">
<el-input v-model="form.config.endpoint" placeholder="请输入节点地址" />
</el-form-item>
<el-form-item v-if="form.storage === 20" label="区域" prop="config.region">
<el-input v-model="form.config.region" placeholder="请输入区域" />
</el-form-item>
<el-form-item v-if="form.storage === 20" label="存储 bucket" prop="config.bucket">
<el-input v-model="form.config.bucket" placeholder="请输入 bucket" />
</el-form-item>
@ -190,7 +187,6 @@ export default {
password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
mode: [{ required: true, message: "连接模式不能为空", trigger: "change" }],
endpoint: [{ required: true, message: "节点地址不能为空", trigger: "blur" }],
region: [{ required: true, message: "区域名不能为空", trigger: "blur" }],
bucket: [{ required: true, message: "存储 bucket 不能为空", trigger: "blur" }],
accessKey: [{ required: true, message: "accessKey 不能为空", trigger: "blur" }],
accessSecret: [{ required: true, message: "accessSecret 不能为空", trigger: "blur" }],