use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class AliyunOssService method upload.
@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
if (StrUtil.isNotEmpty(prefix)) {
if (StrUtil.isNotEmpty(prefixTotal)) {
prefixTotal += "/" + prefix;
} else {
prefixTotal = prefix;
}
}
String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
OSS ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
try {
if (ossClient.doesObjectExist(config.getBucketName(), objectKey)) {
// 文件已存在,则需要对文件重命名
objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
}
ossClient.putObject(config.getBucketName(), objectKey, inputStream);
} catch (OSSException | com.aliyun.oss.ClientException e) {
throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
} finally {
ossClient.shutdown();
}
return config.getDomain() + objectKey;
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class AliyunOssService method generatePresignedUrl.
@Override
public String generatePresignedUrl(String objectName, long expire) {
OSS ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
try {
// 设置URL过期时间。
Date expiration = DateUtil.offsetSecond(DateUtil.date(), (int) expire);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(config.getBucketName(), objectName, expiration);
return url.toString();
} catch (com.aliyun.oss.ClientException e) {
throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
} finally {
ossClient.shutdown();
}
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class HuaweiCloudOssService method upload.
@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
if (StrUtil.isNotEmpty(prefix)) {
if (StrUtil.isNotEmpty(prefixTotal)) {
prefixTotal += "/" + prefix;
} else {
prefixTotal = prefix;
}
}
String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
ObsClient ossClient = null;
try {
ossClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
if (ossClient.doesObjectExist(config.getBucketName(), objectKey)) {
// 文件已存在,则需要对文件重命名
objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
}
ossClient.putObject(config.getBucketName(), objectKey, inputStream);
} catch (ObsException e) {
throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
} finally {
// ObsClient在调用ObsClient.close方法关闭后不能再次使用
if (ossClient != null) {
try {
ossClient.close();
} catch (IOException e) {
log.error("huaweicloud obs close error", e);
}
}
}
return config.getDomain() + objectKey;
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class LogOperationAspect method saveLog.
/**
* 保存日志
*/
private void saveLog(ProceedingJoinPoint joinPoint, String params, long time, Integer state, Exception e) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
LogBody logEntity = new LogBody();
// 日志记录类型
String storeType = "db";
String logType = "operation";
try {
Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
LogOperation annotation = method.getAnnotation(LogOperation.class);
if (annotation != null) {
// 注解上的描述
logEntity.setOperation(annotation.value());
logType = annotation.type();
storeType = annotation.storeType();
}
} catch (NoSuchMethodException ne) {
ne.printStackTrace();
}
logEntity.setStoreType(storeType);
logEntity.setState(state);
logEntity.setRequestTime(time);
logEntity.setType(logType);
// 保存错误信息
if (e != null) {
logEntity.setContent(e instanceof OnexException ? e.toString() : ExceptionUtil.stacktraceToString(e));
}
// 请求参数
Dict requestParams = Dict.create().set("params", params);
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
if (null != request) {
logEntity.setUri(request.getRequestURI());
// 记录内容
requestParams.set("ip", HttpContextUtils.getIpAddr(request));
requestParams.set("ua", request.getHeader(HttpHeaders.USER_AGENT));
requestParams.set("queryString", request.getQueryString());
requestParams.set("url", request.getRequestURL());
requestParams.set("method", request.getMethod());
requestParams.set("contentType", request.getContentType());
/*if (request instanceof OnexHttpServletRequestWrapper) {
try {
requestParams.set("params", IoUtil.read(request.getInputStream()).toString());
} catch (IOException ie) {
log.error("读取流失败", e);
}
}*/
}
logEntity.setRequestParams(requestParams);
logService.saveLog(logEntity);
}
use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.
the class AliyunOssService method download.
@Override
public InputStream download(String objectKey) {
OSS ossClient = null;
OSSObject ossObject = null;
try {
ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
ossObject = ossClient.getObject(config.getBucketName(), objectKey);
return ossObject.getObjectContent();
} catch (OSSException | com.aliyun.oss.ClientException e) {
throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
if (ossObject != null) {
try {
ossObject.close();
} catch (IOException e) {
log.error("aliyun oss close error", e);
}
}
}
}
Aggregations