MinIO Plus 错误码编码。

This commit is contained in:
刘小平 2024-05-26 23:37:31 +08:00
parent 4e702d1305
commit b89d60ee77
10 changed files with 142 additions and 97 deletions

View File

@ -11,4 +11,11 @@
<artifactId>minio-plus-common</artifactId>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,67 @@
package org.liuxp.minioplus.common.enums;
/**
* MinIO Plus 错误码
* @author contact@liuxp.me
* @since 2024/05/26
*/
public enum MinioPlusErrorCode {
FAIL(1000, "操作失败"),
FILE_EXIST_FAILED(1001,"文件不存在"),
FILE_PERMISSION_CHECK_FAILED(1002,"没有访问权限"),
FILE_PART_NUM_CHECK_FAILED(1003,"文件分块MD5校验码数量与实际分块不一致"),
FILE_SUFFIX_GET_FAILED(1004,"无法获取文件的拓展名"),
FILE_BYTES_FAILED(1005,"文件流不能为空"),
FILE_UPLOAD_FAILED(1006,"文件上传失败"),
FILE_PREVIEW_WRITE_FAILED(1007,"缩略图写入失败"),
CREATE_MULTIPART_UPLOAD_FAILED(2001, "获取上传编号失败"),
COMPLETE_MULTIPART_FAILED(2002, "合并分片失败"),
LIST_PARTS_FAILED(2003, "查询分片失败"),
CREATE_UPLOAD_URL_FAILED(2004, "获取对象上传URL失败"),
CREATE_DOWNLOAD_URL_FAILED(2005, "获取对象下载URL失败"),
CREATE_PREVIEW_URL_FAILED(2006, "获取预对象预览URL失败"),
WRITE_FAILED(2007, "文件写入失败"),
READ_FAILED(2008, "文件读取失败"),
DELETE_FAILED(2009, "删除失败");
/**
* 错误编码
*/
private final Integer code;
/**
* 错误提示信息
*/
private final String message;
/**
* 构造方法
*
* @param code 错误编码
* @param message 错误提示信息
*/
MinioPlusErrorCode(Integer code, String message) {
this.code = code;
this.message = message;
}
/**
* 取得错误编码
* @return 错误编码
*/
public Integer getCode() {
return code;
}
/**
* 取得错误提示信息
* @return 错误提示信息
*/
public String getMessage() {
return message;
}
}

View File

@ -1,7 +1,6 @@
package org.liuxp.minioplus.model.enums;
package org.liuxp.minioplus.common.enums;
import cn.hutool.core.util.ArrayUtil;
import lombok.Getter;
import java.util.Arrays;
@ -11,7 +10,6 @@ import java.util.Arrays;
* @author contact@liuxp.me
* @since 2023/06/26
*/
@Getter
public enum StorageBucketEnums {
/**
@ -40,6 +38,18 @@ public enum StorageBucketEnums {
private final String[] types;
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String[] getTypes() {
return types;
}
/**
* 构造方法
*

View File

@ -1,11 +1,13 @@
package org.liuxp.minioplus.core.common.exception;
package org.liuxp.minioplus.common.exception;
import org.liuxp.minioplus.common.enums.MinioPlusErrorCode;
/**
* MinioPlus专用异常定义
* @author contact@liuxp.me
* @since 2023/08/14
* @since 2024/05/26
*/
public class MinioPlusBusinessException extends RuntimeException {
public class MinioPlusException extends RuntimeException {
private static final long serialVersionUID = 772046747932011086L;
@ -21,14 +23,19 @@ public class MinioPlusBusinessException extends RuntimeException {
this.errorCode = errorCode;
}
public MinioPlusBusinessException() {
public MinioPlusException() {
super();
}
public MinioPlusBusinessException(String message) {
public MinioPlusException(String message) {
super(message);
}
public MinioPlusException(MinioPlusErrorCode minioPlusErrorCode){
this.errorCode = minioPlusErrorCode.getCode();
this.errorMessage = minioPlusErrorCode.getMessage();
}
public String getErrorMessage() {
return errorMessage;
}
@ -37,7 +44,7 @@ public class MinioPlusBusinessException extends RuntimeException {
this.errorMessage = errorMessage;
}
public MinioPlusBusinessException(int errorCode, String errorMessage) {
public MinioPlusException(int errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

View File

@ -32,6 +32,10 @@
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.liuxp</groupId>
<artifactId>minio-plus-common</artifactId>
</dependency>
<dependency>
<groupId>org.liuxp</groupId>
<artifactId>minio-plus-config</artifactId>

View File

@ -19,9 +19,9 @@ import org.liuxp.minioplus.model.dto.FileMetadataInfoDTO;
import org.liuxp.minioplus.model.dto.FileMetadataInfoSaveDTO;
import org.liuxp.minioplus.model.dto.FileMetadataInfoUpdateDTO;
import org.liuxp.minioplus.core.common.context.MultipartUploadCreateDTO;
import org.liuxp.minioplus.model.enums.ResponseCodeEnum;
import org.liuxp.minioplus.model.enums.StorageBucketEnums;
import org.liuxp.minioplus.core.common.exception.MinioPlusBusinessException;
import org.liuxp.minioplus.common.enums.MinioPlusErrorCode;
import org.liuxp.minioplus.common.enums.StorageBucketEnums;
import org.liuxp.minioplus.common.exception.MinioPlusException;
import org.liuxp.minioplus.core.common.utils.MinioPlusCommonUtil;
import org.liuxp.minioplus.model.vo.CompleteResultVo;
import org.liuxp.minioplus.model.vo.FileCheckResultVo;
@ -278,8 +278,8 @@ public class StorageEngineServiceImpl implements StorageEngineService {
FileMetadataInfoVo metadata = metadataRepository.one(searchDto);
if(metadata == null){
log.error(fileKey+"不存在");
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),fileKey+"不存在");
log.error(fileKey+MinioPlusErrorCode.FILE_EXIST_FAILED.getMessage());
throw new MinioPlusException(MinioPlusErrorCode.FILE_EXIST_FAILED.getCode(),fileKey+MinioPlusErrorCode.FILE_EXIST_FAILED.getMessage());
}
if(Boolean.TRUE.equals(metadata.getIsFinished())){
@ -371,8 +371,8 @@ public class StorageEngineServiceImpl implements StorageEngineService {
return isCreateFile;
}catch(Exception e){
log.error("文件上传失败",e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"文件上传失败");
log.error(MinioPlusErrorCode.FILE_UPLOAD_FAILED.getMessage(),e);
throw new MinioPlusException(MinioPlusErrorCode.FILE_UPLOAD_FAILED);
}
}
@ -452,8 +452,8 @@ public class StorageEngineServiceImpl implements StorageEngineService {
byte[] largeImageBytes = largeImage.toByteArray();
minioRepository.write(StorageBucketEnums.IMAGE_PREVIEW.getCode(), MinioPlusCommonUtil.getObjectName(saveDTO.getFileMd5()), new ByteArrayInputStream(largeImageBytes), largeImageBytes.length, saveDTO.getFileMimeType());
}catch(Exception e){
log.error("缩略图写入失败",e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"缩略图写入失败");
log.error(MinioPlusErrorCode.FILE_PREVIEW_WRITE_FAILED.getMessage(),e);
throw new MinioPlusException(MinioPlusErrorCode.FILE_PREVIEW_WRITE_FAILED);
}
}
@ -561,12 +561,12 @@ public class StorageEngineServiceImpl implements StorageEngineService {
*/
private void authentication(FileMetadataInfoVo metadata, String fileKey, String userId){
if (null == metadata) {
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), fileKey + "不存在!");
throw new MinioPlusException(MinioPlusErrorCode.FILE_EXIST_FAILED.getCode(), fileKey + MinioPlusErrorCode.FILE_EXIST_FAILED.getMessage());
}
// 元数据信息存在判断权限
if(Boolean.TRUE.equals(metadata.getIsPrivate()) && !userId.equals(metadata.getCreateUser())){
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), fileKey + "用户userId="+userId+"没有访问权限!");
throw new MinioPlusException(MinioPlusErrorCode.FILE_PERMISSION_CHECK_FAILED.getCode(), fileKey + "用户"+userId+MinioPlusErrorCode.FILE_PERMISSION_CHECK_FAILED.getMessage());
}
}
@ -688,7 +688,7 @@ public class StorageEngineServiceImpl implements StorageEngineService {
Integer chunkNum = meteData.getPartNumber();
if(partMd5List==null || chunkNum != partMd5List.size()){
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"文件分块MD5校验码数量与实际分块不一致");
throw new MinioPlusException(MinioPlusErrorCode.FILE_PART_NUM_CHECK_FAILED);
}
// 校验文件完整性
@ -764,7 +764,7 @@ public class StorageEngineServiceImpl implements StorageEngineService {
.uploadId(meteData.getUploadTaskId())
.partNumberMarker(0)
.build());
}catch (MinioPlusBusinessException e){
}catch (MinioPlusException e){
log.error("获取分片信息失败partList返回空",e);
MultipartUploadCreateDTO multipartUploadCreateDTO = MultipartUploadCreateDTO.builder()
.bucketName(meteData.getStorageBucket())
@ -821,7 +821,7 @@ public class StorageEngineServiceImpl implements StorageEngineService {
// 获取文件后缀
String suffix = FileUtil.getSuffix(bo.getFullFileName());
if (CharSequenceUtil.isBlank(suffix)) {
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "无法获取文件的拓展名");
throw new MinioPlusException(MinioPlusErrorCode.FILE_SUFFIX_GET_FAILED);
}
// 文件key
fileKey = IdUtil.fastSimpleUUID();

View File

@ -7,8 +7,8 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.liuxp.minioplus.config.MinioPlusProperties;
import org.liuxp.minioplus.core.common.context.MultipartUploadCreateDTO;
import org.liuxp.minioplus.model.enums.ResponseCodeEnum;
import org.liuxp.minioplus.core.common.exception.MinioPlusBusinessException;
import org.liuxp.minioplus.common.enums.MinioPlusErrorCode;
import org.liuxp.minioplus.common.exception.MinioPlusException;
import org.liuxp.minioplus.core.repository.MinioRepository;
import org.springframework.stereotype.Repository;
@ -71,8 +71,8 @@ public class MinioRepositoryImpl implements MinioRepository {
try {
return this.getClient().createMultipartUpload(multipartUploadCreate.getBucketName(), multipartUploadCreate.getRegion(), multipartUploadCreate.getObjectName(), multipartUploadCreate.getHeaders(), multipartUploadCreate.getExtraQueryParams());
} catch (Exception e) {
log.error("文件分片获取上传编号失败:{}", e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "获取上传编号失败");
log.error(MinioPlusErrorCode.CREATE_MULTIPART_UPLOAD_FAILED.getMessage()+":{}", e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.CREATE_MULTIPART_UPLOAD_FAILED);
}
}
@ -87,8 +87,8 @@ public class MinioRepositoryImpl implements MinioRepository {
try {
return this.getClient().completeMultipartUpload(multipartUploadCreate.getBucketName(), multipartUploadCreate.getRegion(), multipartUploadCreate.getObjectName(), multipartUploadCreate.getUploadId(), multipartUploadCreate.getParts(), multipartUploadCreate.getHeaders(), multipartUploadCreate.getExtraQueryParams());
} catch (Exception e) {
log.error("合并分片失败,uploadId:{},ObjectName:{},失败原因:{},", multipartUploadCreate.getUploadId(), multipartUploadCreate.getObjectName(), e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "合并分片失败");
log.error(MinioPlusErrorCode.COMPLETE_MULTIPART_FAILED.getMessage()+",uploadId:{},ObjectName:{},失败原因:{},", multipartUploadCreate.getUploadId(), multipartUploadCreate.getObjectName(), e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.COMPLETE_MULTIPART_FAILED);
}
}
@ -104,8 +104,8 @@ public class MinioRepositoryImpl implements MinioRepository {
try {
return this.getClient().listParts(multipartUploadCreate.getBucketName(), multipartUploadCreate.getRegion(), multipartUploadCreate.getObjectName(), multipartUploadCreate.getMaxParts(), multipartUploadCreate.getPartNumberMarker(), multipartUploadCreate.getUploadId(), multipartUploadCreate.getHeaders(), multipartUploadCreate.getExtraQueryParams());
} catch (Exception e) {
log.error("查询分片失败:{}", e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "查询分片失败");
log.error(MinioPlusErrorCode.LIST_PARTS_FAILED.getMessage()+":{}", e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.LIST_PARTS_FAILED);
}
}
@ -130,8 +130,8 @@ public class MinioRepositoryImpl implements MinioRepository {
.extraQueryParams(queryParams)
.build());
} catch (Exception e) {
log.error("获取预签名URL失败:{}", e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "获取预签名URL失败");
log.error(MinioPlusErrorCode.CREATE_UPLOAD_URL_FAILED.getMessage()+":{}", e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.CREATE_UPLOAD_URL_FAILED);
}
}
@ -160,8 +160,8 @@ public class MinioRepositoryImpl implements MinioRepository {
.extraQueryParams(reqParams)
.build());
} catch (Exception e) {
log.error("获取预签名下载URL失败:{}", e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "获取预签名下载URL失败");
log.error(MinioPlusErrorCode.CREATE_DOWNLOAD_URL_FAILED.getMessage()+":{}", e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.CREATE_DOWNLOAD_URL_FAILED);
}
}
@ -182,8 +182,8 @@ public class MinioRepositoryImpl implements MinioRepository {
.extraQueryParams(reqParams)
.build());
} catch (Exception e) {
log.error("获取预签名预览URL失败:{}", e.getMessage(), e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(), "获取预签名预览URL失败");
log.error(MinioPlusErrorCode.CREATE_PREVIEW_URL_FAILED.getMessage()+":{}", e.getMessage(), e);
throw new MinioPlusException(MinioPlusErrorCode.CREATE_PREVIEW_URL_FAILED);
}
}
@ -209,8 +209,8 @@ public class MinioRepositoryImpl implements MinioRepository {
.build());
} catch (Exception e) {
log.error("文件写入失败",e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"文件写入失败");
log.error(MinioPlusErrorCode.WRITE_FAILED.getMessage(),e);
throw new MinioPlusException(MinioPlusErrorCode.WRITE_FAILED);
}
return true;
@ -224,8 +224,8 @@ public class MinioRepositoryImpl implements MinioRepository {
// 文件流转换为字节码
return IoUtil.readBytes(inputStream);
} catch (Exception e) {
log.error("文件读取失败",e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"文件读取失败");
log.error(MinioPlusErrorCode.READ_FAILED.getMessage(),e);
throw new MinioPlusException(MinioPlusErrorCode.READ_FAILED);
}
}
@ -235,8 +235,8 @@ public class MinioRepositoryImpl implements MinioRepository {
try {
this.getClient().removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
log.error("删除失败",e);
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"删除失败");
log.error(MinioPlusErrorCode.DELETE_FAILED.getMessage(),e);
throw new MinioPlusException(MinioPlusErrorCode.DELETE_FAILED);
}
}

View File

@ -12,9 +12,9 @@ import org.liuxp.minioplus.config.MinioPlusProperties;
import org.liuxp.minioplus.model.dto.FileMetadataInfoDTO;
import org.liuxp.minioplus.model.dto.FileMetadataInfoSaveDTO;
import org.liuxp.minioplus.model.dto.FileSaveDTO;
import org.liuxp.minioplus.model.enums.ResponseCodeEnum;
import org.liuxp.minioplus.model.enums.StorageBucketEnums;
import org.liuxp.minioplus.core.common.exception.MinioPlusBusinessException;
import org.liuxp.minioplus.common.enums.MinioPlusErrorCode;
import org.liuxp.minioplus.common.enums.StorageBucketEnums;
import org.liuxp.minioplus.common.exception.MinioPlusException;
import org.liuxp.minioplus.core.common.utils.ContentTypeUtil;
import org.liuxp.minioplus.core.common.utils.MinioPlusCommonUtil;
import org.liuxp.minioplus.model.vo.FileMetadataInfoVo;
@ -144,7 +144,7 @@ public class StorageServiceImpl implements StorageService {
FileMetadataInfoSaveDTO buildSaveDto(FileSaveDTO fileSaveDTO, byte[] fileBytes){
if(null==fileBytes){
throw new MinioPlusBusinessException(ResponseCodeEnum.FAIL.getCode(),"文件流不能为空");
throw new MinioPlusException(MinioPlusErrorCode.FILE_BYTES_FAILED);
}
// 计算文件MD5值
String md5 = SecureUtil.md5().digestHex(fileBytes);

View File

@ -1,45 +0,0 @@
package org.liuxp.minioplus.model.enums;
/**
* 返回给前端的code编码对应内容的枚举
*/
public enum ResponseCodeEnum {
/**
* 调用成功
*/
SUCCESS(0, "调用成功"),
/**
* 调用失败
*/
FAIL(-1, "调用失败");
/**
* 返回给前端的状态编码 0表示成功
*/
private final Integer code;
/**
* 编码对应的解释
*/
private final String message;
/**
* 构造方法
*
* @param code
* @param message
*/
ResponseCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}

View File

@ -166,11 +166,6 @@
<artifactId>minio-plus-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.liuxp</groupId>
<artifactId>minio-plus-spring-mvc</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</dependencyManagement>