Search in sources :

Example 11 with BusinessException

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);
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 12 with BusinessException

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);
    }
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Font(java.awt.Font)

Example 13 with BusinessException

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);
    }
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) InputStream(java.io.InputStream) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 14 with BusinessException

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();
}
Also used : EmojiFilter(com.chao.cloud.common.extra.emoji.annotation.EmojiFilter) EmojiUtil(cn.hutool.extra.emoji.EmojiUtil) Collectors(java.util.stream.Collectors) Around(org.aspectj.lang.annotation.Around) EmojiFilter(com.chao.cloud.common.extra.emoji.annotation.EmojiFilter) JSONUtil(cn.hutool.json.JSONUtil) List(java.util.List) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Stream(java.util.stream.Stream) BaseProxy(com.chao.cloud.common.base.BaseProxy) Aspect(org.aspectj.lang.annotation.Aspect) BusinessException(com.chao.cloud.common.exception.BusinessException) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) BusinessException(com.chao.cloud.common.exception.BusinessException) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 15 with BusinessException

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;
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) ShardingProperties(com.chao.cloud.common.extra.sharding.annotation.ShardingProperties)

Aggregations

BusinessException (com.chao.cloud.common.exception.BusinessException)19 Method (java.lang.reflect.Method)5 InputStream (java.io.InputStream)4 Around (org.aspectj.lang.annotation.Around)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 WxError (me.chanjar.weixin.common.error.WxError)2 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 JSONObject (org.json.JSONObject)2 CollUtil (cn.hutool.core.collection.CollUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 EmojiUtil (cn.hutool.extra.emoji.EmojiUtil)1 JSONObject (cn.hutool.json.JSONObject)1 JSONUtil (cn.hutool.json.JSONUtil)1 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 BaseProxy (com.chao.cloud.common.base.BaseProxy)1 Response (com.chao.cloud.common.entity.Response)1