【file】整理file的异常

pull/3/head
fengshuonan 2021-02-08 10:21:44 +08:00
parent 6d84980b6f
commit 7499b60000
9 changed files with 38 additions and 105 deletions

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.file.exception; package cn.stylefeng.roses.kernel.file.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.constants.FileConstants; import cn.stylefeng.roses.kernel.file.constants.FileConstants;
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum; import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
@ -12,16 +13,12 @@ import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
*/ */
public class FileException extends ServiceException { public class FileException extends ServiceException {
public FileException(String errorCode, String userTip) {
super(FileConstants.FILE_MODULE_NAME, errorCode, userTip);
}
public FileException(AbstractExceptionEnum exception) { public FileException(AbstractExceptionEnum exception) {
super(FileConstants.FILE_MODULE_NAME, exception); super(FileConstants.FILE_MODULE_NAME, exception);
} }
public FileException(AbstractExceptionEnum exception, String userTip) { public FileException(AbstractExceptionEnum exception, Object... params) {
super(FileConstants.FILE_MODULE_NAME, exception.getErrorCode(), userTip); super(FileConstants.FILE_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
} }
} }

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.file.util;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import cn.stylefeng.roses.kernel.file.exception.FileException; import cn.stylefeng.roses.kernel.file.exception.FileException;
import cn.stylefeng.roses.kernel.file.exception.enums.FileExceptionEnum; import cn.stylefeng.roses.kernel.file.exception.enums.FileExceptionEnum;
@ -43,8 +42,7 @@ public class DownloadUtil {
response.setContentType("application/octet-stream;charset=UTF-8"); response.setContentType("application/octet-stream;charset=UTF-8");
IoUtil.write(response.getOutputStream(), true, fileBytes); IoUtil.write(response.getOutputStream(), true, fileBytes);
} catch (IOException e) { } catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.DOWNLOAD_FILE_ERROR.getUserTip(), e.getMessage()); throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, e.getMessage());
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, userTip);
} }
} }
@ -66,8 +64,7 @@ public class DownloadUtil {
try { try {
fileName = URLEncoder.encode(file.getName(), CharsetUtil.UTF_8); fileName = URLEncoder.encode(file.getName(), CharsetUtil.UTF_8);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
String userTip = StrUtil.format(FileExceptionEnum.DOWNLOAD_FILE_ERROR.getUserTip(), e.getMessage()); throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, e.getMessage());
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, userTip);
} }
//下载文件 //下载文件
@ -91,8 +88,7 @@ public class DownloadUtil {
// 输出字节流 // 输出字节流
IoUtil.write(outputStream, true, fileBytes); IoUtil.write(outputStream, true, fileBytes);
} catch (IOException e) { } catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.WRITE_BYTES_ERROR.getUserTip(), e.getMessage()); throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, e.getMessage());
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, userTip);
} }
} }

View File

@ -37,8 +37,6 @@ import static cn.stylefeng.roses.kernel.file.constants.FileConstants.*;
* 1.ID<br> * 1.ID<br>
* *
* 2.UICODE<br> * 2.UICODE<br>
* <p>
* 线banner~
* *
* @author majianguo * @author majianguo
* @date 2020/12/27 13:39 * @date 2020/12/27 13:39
@ -62,7 +60,6 @@ public class SysFileInfoController {
return new SuccessResponseData(fileUploadInfoResult); return new SuccessResponseData(fileUploadInfoResult);
} }
/** /**
* tinymce * tinymce
* *
@ -257,4 +254,4 @@ public class SysFileInfoController {
return new SuccessResponseData(sysFileInfoService.detail(sysFileInfoRequest)); return new SuccessResponseData(sysFileInfoService.detail(sysFileInfoRequest));
} }
} }

View File

@ -61,8 +61,7 @@ public class FileInfoFactory {
bytes = file.getBytes(); bytes = file.getBytes();
fileOperatorApi.storageFile(DEFAULT_BUCKET_NAME, finalFileName, bytes); fileOperatorApi.storageFile(DEFAULT_BUCKET_NAME, finalFileName, bytes);
} catch (IOException e) { } catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.ERROR_FILE.getUserTip(), e.getMessage()); throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
throw new FileException(FileExceptionEnum.ERROR_FILE, userTip);
} }
// 计算文件大小kb // 计算文件大小kb

View File

@ -205,8 +205,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
String fileOriginName = sysFileInfoResponse.getFileOriginName(); String fileOriginName = sysFileInfoResponse.getFileOriginName();
// 判断公有文件下载时是否包含私有文件 // 判断公有文件下载时是否包含私有文件
if (secretFlag.equals(YesOrNotEnum.N.getCode()) && !secretFlag.equals(sysFileInfoResponse.getSecretFlag())) { if (secretFlag.equals(YesOrNotEnum.N.getCode()) && !secretFlag.equals(sysFileInfoResponse.getSecretFlag())) {
String userTip = StrUtil.format(FileExceptionEnum.SECRET_FLAG_INFO_ERROR.getUserTip(), fileOriginName); throw new FileException(FileExceptionEnum.SECRET_FLAG_INFO_ERROR, fileOriginName);
throw new FileException(FileExceptionEnum.SECRET_FLAG_INFO_ERROR, userTip);
} }
byte[] fileBytes = fileOperatorApi.getFileBytes(DEFAULT_BUCKET_NAME, sysFileInfoResponse.getFileObjectName()); byte[] fileBytes = fileOperatorApi.getFileBytes(DEFAULT_BUCKET_NAME, sysFileInfoResponse.getFileObjectName());
@ -299,7 +298,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
if (ObjectUtil.isEmpty(fileInfo)) { if (ObjectUtil.isEmpty(fileInfo)) {
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip(); String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip();
String errorMessage = StrUtil.format(userTip, "文件:" + fileInfo.getFileId() + "未找到!"); String errorMessage = StrUtil.format(userTip, "文件:" + fileInfo.getFileId() + "未找到!");
throw new FileException(FILE_NOT_FOUND.getErrorCode(), errorMessage); throw new FileException(FILE_NOT_FOUND, errorMessage);
} }
// 把之前的文件刷回 // 把之前的文件刷回
@ -405,8 +404,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
// 输出字节流 // 输出字节流
IoUtil.write(outputStream, true, fileBytes); IoUtil.write(outputStream, true, fileBytes);
} catch (IOException e) { } catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.WRITE_BYTES_ERROR.getUserTip(), e.getMessage()); throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, e.getMessage());
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, userTip);
} }
} else { } else {
// 不支持别的文件预览 // 不支持别的文件预览
@ -423,8 +421,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
private SysFileInfo querySysFileInfo(SysFileInfoRequest sysFileInfoRequest) { private SysFileInfo querySysFileInfo(SysFileInfoRequest sysFileInfoRequest) {
SysFileInfo sysFileInfo = this.getById(sysFileInfoRequest.getFileId()); SysFileInfo sysFileInfo = this.getById(sysFileInfoRequest.getFileId());
if (ObjectUtil.isEmpty(sysFileInfo) || sysFileInfo.getDelFlag().equals(YesOrNotEnum.Y.getCode())) { if (ObjectUtil.isEmpty(sysFileInfo) || sysFileInfo.getDelFlag().equals(YesOrNotEnum.Y.getCode())) {
String userTip = StrUtil.format(FileExceptionEnum.NOT_EXISTED.getUserTip(), sysFileInfoRequest.getFileId()); throw new FileException(FileExceptionEnum.NOT_EXISTED, sysFileInfoRequest.getFileId());
throw new FileException(FileExceptionEnum.NOT_EXISTED, userTip);
} }
return sysFileInfo; return sysFileInfo;
} }

View File

@ -1,7 +1,6 @@
package cn.stylefeng.roses.kernel.file.aliyun; package cn.stylefeng.roses.kernel.file.aliyun;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.FileOperatorApi; import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum; import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.exception.FileException; import cn.stylefeng.roses.kernel.file.exception.FileException;
@ -69,9 +68,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return ossClient.doesBucketExist(bucketName); return ossClient.doesBucketExist(bucketName);
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -87,9 +84,7 @@ public class AliyunFileOperator implements FileOperatorApi {
} }
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -99,9 +94,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return ossClient.doesObjectExist(bucketName, key); return ossClient.doesObjectExist(bucketName, key);
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -111,9 +104,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.putObject(bucketName, key, new ByteArrayInputStream(bytes)); ossClient.putObject(bucketName, key, new ByteArrayInputStream(bytes));
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -124,9 +115,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.putObject(putObjectRequest); ossClient.putObject(putObjectRequest);
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -139,9 +128,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return IoUtil.readBytes(objectContent); return IoUtil.readBytes(objectContent);
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} finally { } finally {
IoUtil.close(objectContent); IoUtil.close(objectContent);
} }
@ -160,9 +147,7 @@ public class AliyunFileOperator implements FileOperatorApi {
} }
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -172,9 +157,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey); ossClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey);
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -186,9 +169,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return url.toString(); return url.toString();
} catch (OSSException | ClientException e) { } catch (OSSException | ClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }

View File

@ -114,9 +114,7 @@ public class LocalFileOperator implements FileOperatorApi {
if (!FileUtil.exist(absoluteFile)) { if (!FileUtil.exist(absoluteFile)) {
// 组装返回信息 // 组装返回信息
String errorMessage = StrUtil.format("bucket={},key={}", bucketName, key); String errorMessage = StrUtil.format("bucket={},key={}", bucketName, key);
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip(); throw new FileException(FileExceptionEnum.FILE_NOT_FOUND, errorMessage);
String finalUserTip = StrUtil.format(userTip, errorMessage);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND.getErrorCode(), finalUserTip);
} else { } else {
return FileUtil.readBytes(absoluteFile); return FileUtil.readBytes(absoluteFile);
} }
@ -135,9 +133,7 @@ public class LocalFileOperator implements FileOperatorApi {
if (!FileUtil.exist(originFile)) { if (!FileUtil.exist(originFile)) {
// 组装返回信息 // 组装返回信息
String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey); String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey);
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip(); throw new FileException(FileExceptionEnum.FILE_NOT_FOUND, errorMessage);
String finalUserTip = StrUtil.format(userTip, errorMessage);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND.getErrorCode(), finalUserTip);
} else { } else {
// 拷贝文件 // 拷贝文件

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.file.minio;
import cn.hutool.core.io.FileTypeUtil; import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.file.FileOperatorApi; import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.constants.FileConstants; import cn.stylefeng.roses.kernel.file.constants.FileConstants;
@ -64,9 +63,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient = new MinioClient(endpoint, accessKey, secretKey); minioClient = new MinioClient(endpoint, accessKey, secretKey);
} catch (InvalidEndpointException | InvalidPortException e) { } catch (InvalidEndpointException | InvalidPortException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -86,9 +83,7 @@ public class MinIoFileOperator implements FileOperatorApi {
return minioClient.bucketExists(bucketName); return minioClient.bucketExists(bucketName);
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -129,9 +124,7 @@ public class MinIoFileOperator implements FileOperatorApi {
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
} }
@ -151,9 +144,7 @@ public class MinIoFileOperator implements FileOperatorApi {
return IoUtil.readBytes(inputStream); return IoUtil.readBytes(inputStream);
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -172,9 +163,7 @@ public class MinIoFileOperator implements FileOperatorApi {
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -184,9 +173,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey); minioClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey);
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -209,9 +196,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient.removeObject(bucketName, key); minioClient.removeObject(bucketName, key);
} catch (Exception e) { } catch (Exception e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }

View File

@ -2,7 +2,6 @@ package cn.stylefeng.roses.kernel.file.tencent;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ExecutorBuilder; import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.FileOperatorApi; import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum; import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.exception.FileException; import cn.stylefeng.roses.kernel.file.exception.FileException;
@ -63,7 +62,7 @@ public class TenFileOperator implements FileOperatorApi {
// 4.线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM同地域上传 COS的情况下设置成16或32即可可较充分的利用网络资源 // 4.线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM同地域上传 COS的情况下设置成16或32即可可较充分的利用网络资源
// 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。 // 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。
//ExecutorService threadPool = Executors.newFixedThreadPool(32); 线程池不允许使用Executors去创建而是通过ThreadPoolExecutor的方式这样的处理方式让写的同学更加明确线程池的运行规则规避资源耗尽的风险。 //ExecutorService threadPool = Executors.newFixedThreadPool(32); 线程池不允许使用Executors去创建而是通过ThreadPoolExecutor的方式这样的处理方式让写的同学更加明确线程池的运行规则规避资源耗尽的风险。
ExecutorService threadPool = ExecutorBuilder.create().build(); ExecutorService threadPool = ExecutorBuilder.create().build();
@ -93,9 +92,7 @@ public class TenFileOperator implements FileOperatorApi {
return cosClient.doesBucketExist(bucketName); return cosClient.doesBucketExist(bucketName);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -111,9 +108,7 @@ public class TenFileOperator implements FileOperatorApi {
} }
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -144,9 +139,7 @@ public class TenFileOperator implements FileOperatorApi {
cosClient.putObject(bucketName, key, new ByteArrayInputStream(bytes), objectMetadata); cosClient.putObject(bucketName, key, new ByteArrayInputStream(bytes), objectMetadata);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} finally { } finally {
IoUtil.close(byteArrayInputStream); IoUtil.close(byteArrayInputStream);
} }
@ -169,9 +162,7 @@ public class TenFileOperator implements FileOperatorApi {
cosClient.putObject(bucketName, key, inputStream, objectMetadata); cosClient.putObject(bucketName, key, inputStream, objectMetadata);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} finally { } finally {
IoUtil.close(inputStream); IoUtil.close(inputStream);
} }
@ -187,9 +178,7 @@ public class TenFileOperator implements FileOperatorApi {
return IoUtil.readBytes(cosObjectInput); return IoUtil.readBytes(cosObjectInput);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} finally { } finally {
IoUtil.close(cosObjectInput); IoUtil.close(cosObjectInput);
} }
@ -218,9 +207,7 @@ public class TenFileOperator implements FileOperatorApi {
transferManager.copy(copyObjectRequest, cosClient, null); transferManager.copy(copyObjectRequest, cosClient, null);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} }
} }
@ -234,9 +221,7 @@ public class TenFileOperator implements FileOperatorApi {
url = cosClient.generatePresignedUrl(presignedUrlRequest); url = cosClient.generatePresignedUrl(presignedUrlRequest);
} catch (CosClientException e) { } catch (CosClientException e) {
// 组装提示信息 // 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip(); throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
} }
return url.toString(); return url.toString();
} }