use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class FileOperationImpl method uploadInputStream.
@Override
public String uploadInputStream(InputStream in, String fileName) throws Exception {
try (InputStream fileStream = in) {
// 从线程池获取ftp
String name = IFileOperation.genFileName(fileName);
String path = IFileOperation.genFilePath(ftpConfig.getPath());
String fullPath = path + name;
// 判断是否为ftp 上传
boolean upload = false;
if (ftpConfig.isLocal()) {
// 本地上传
FileUtil.mkdir(path);
// copy 图片
FileUtil.writeFromStream(fileStream, path + name);
upload = true;
} else {
// ftp 上传
upload = ftpClientProxy.uploadFile(path, name, fileStream);
}
if (upload) {
return this.delPathPrefix(fullPath);
}
}
throw new BusinessException("文件上传失败:" + fileName);
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class FileOperationImpl method uploadImg.
@Override
public String uploadImg(InputStream in, String fileName) throws Exception {
// 操作流
try (InputStream fileStream = in;
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
String mimeType = FileUtil.getMimeType(fileName);
if (!mimeType.startsWith("image")) {
throw new BusinessException("无效的图片类型:" + mimeType);
}
// 加水印
//
ImgUtil.pressText(//
fileStream, //
out, // 文字
ftpConfig.getLogo(), // 文字
Color.decode(ftpConfig.getColor()), // 字体
new Font(ftpConfig.getFontName(), ftpConfig.getFontStyle(), ftpConfig.getFontSize()), // x坐标修正值。 默认在中间,偏移量相对于中间偏移
ftpConfig.getX(), // y坐标修正值。 默认在中间,偏移量相对于中间偏移
ftpConfig.getY(), // 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
ftpConfig.getAlpha());
// 将outputStream转成inputstream
byte[] array = out.toByteArray();
ByteArrayInputStream stream = IoUtil.toStream(array);
// 上传文件
return this.uploadInputStream(stream, fileName);
}
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class FtpClientProxy method retrieveFileStream.
/**
* 下载 remote文件流
* @param remote 远程文件
* @return 字节数据
* @throws Exception 下载时抛出的异常
*/
public byte[] retrieveFileStream(String remote) throws Exception {
FTPClient client = null;
InputStream in = null;
boolean nullIO = false;
try {
client = ftpClientPool.borrowObject();
// 判断文件是否存在
in = client.retrieveFileStream(remote);
if (in == null) {
nullIO = true;
throw new BusinessException("无效的文件名:" + remote);
}
byte[] bytes = IOUtils.toByteArray(in);
return bytes;
} finally {
IoUtil.close(in);
if (!nullIO && !client.completePendingCommand()) {
client.logout();
client.disconnect();
ftpClientPool.getPool().invalidateObject(client);
}
ftpClientPool.returnObject(client);
}
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class EmojiFilterProxy method around.
// 环绕拦截
@Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object around(ProceedingJoinPoint pdj) throws Throwable {
Method method = this.getMethod(pdj);
// 获取method 中的注解
EmojiFilter emojiFilter = method.getAnnotation(EmojiFilter.class);
// 获取参数
List<Object> args = Stream.of(pdj.getArgs()).filter(o -> !isInclude(o)).collect(Collectors.toList());
// 开始过滤
if ((emojiFilter == null || emojiFilter.value()) && !CollUtil.isEmpty(args)) {
// 参数json化
String jsonStr = JSONUtil.toJsonStr(args);
// 提取emoji
List<String> list = EmojiUtil.extractEmojis(jsonStr);
if (!CollUtil.isEmpty(list)) {
String emoji = list.stream().collect(Collectors.joining());
// 抛出异常
throw new BusinessException(StrUtil.format("[非法字符:emoji:->{}]", emoji));
}
}
return pdj.proceed();
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class HintShardingColumnProxy method shardingCodeThrow.
private ShardingColumnModel shardingCodeThrow(String orgCode, boolean validateOrgCode) {
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
ShardingColumnModel m = convert.getShardingColumnModel(orgCode);
//
String shardingCode = m.getShardingCode();
// 匹配数据源
boolean matches = true;
if (StrUtil.isBlank(shardingCode)) {
matches = false;
} else if (prop.isEnable()) {
// 判断shardingCode 是否包含符合的数据库
String ds = SpringUtil.getBean(ShardingExtraConfig.class).getDsByColumnValue(shardingCode);
if (StrUtil.isBlank(ds)) {
matches = false;
}
}
// 数据源大于1执行校验
if (prop.getDsNum() > 1) {
// 匹配失败
if (!matches) {
throw new BusinessException("未匹配到数据源");
}
if (validateOrgCode && !m.isOrgExist()) {
throw new BusinessException("无效的code");
}
}
// 设置默认值
if (StrUtil.isBlank(shardingCode)) {
shardingCode = ShardingConstant.DEFAULT_VALUE;
m.setShardingCode(shardingCode);
}
return m;
}
Aggregations