Search in sources :

Example 1 with GlobalFileException

use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.

the class ImageDownloadUtil method saveToCloudStorage.

/**
 * 将网络图片转存到云存储中
 *
 * @param imgUrl  网络图片地址
 * @param referer 为了预防某些网站做了权限验证,不加referer可能会403
 */
public static String saveToCloudStorage(String imgUrl, String referer) {
    log.debug("download img >> %s", imgUrl);
    String res = null;
    try (InputStream is = getInputStreamByUrl(imgUrl, referer)) {
        FileUploader uploader = new GlobalFileUploader();
        VirtualFile file = uploader.upload(is, FileUploadType.SIMPLE.getPath(), imgUrl, false);
        res = file.getFullFilePath();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GlobalFileException(e.getMessage());
    }
    return res;
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException)

Example 2 with GlobalFileException

use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.

the class ExceptionHandleController method handle.

@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResponseVO handle(Throwable e) {
    if (e instanceof ZhydException || e instanceof GlobalFileException) {
        return ResultUtil.error(e.getMessage());
    }
    if (e instanceof UndeclaredThrowableException) {
        e = ((UndeclaredThrowableException) e).getUndeclaredThrowable();
    }
    ResponseStatus responseStatus = ResponseStatus.getResponseStatus(e.getMessage());
    if (responseStatus != null) {
        log.error(responseStatus.getMessage());
        return ResultUtil.error(responseStatus.getCode(), responseStatus.getMessage());
    }
    // 打印异常栈
    e.printStackTrace();
    return ResultUtil.error(CommonConst.DEFAULT_ERROR_CODE, ResponseStatus.ERROR.getMessage());
}
Also used : ZhydException(com.zyd.blog.framework.exception.ZhydException) ResponseStatus(com.zyd.blog.business.enums.ResponseStatus) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with GlobalFileException

use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.

the class StreamUtil method clone.

/**
 * 复制InputStream
 *
 * @param is InputStream
 * @return
 */
public static InputStream clone(InputStream is) {
    if (null == is) {
        throw new GlobalFileException("无法获取文件流,文件不可用!");
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) > -1) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        return new ByteArrayInputStream(baos.toByteArray());
    } catch (IOException e) {
        throw new GlobalFileException("无法复制当前文件流!", e);
    }
}
Also used : GlobalFileException(com.zyd.blog.file.exception.GlobalFileException)

Example 4 with GlobalFileException

use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.

the class BaseApiClient method createNewFileName.

void createNewFileName(String key, String pathPrefix) {
    this.suffix = FileUtil.getSuffix(key);
    if (!FileUtil.isPicture(this.suffix)) {
        throw new GlobalFileException("[" + this.storageType + "] 非法的图片文件[" + key + "]!目前只支持以下图片格式:[jpg, jpeg, png, gif, bmp]");
    }
    String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
    this.newFileName = pathPrefix + (fileName + this.suffix);
}
Also used : GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) Date(java.util.Date)

Example 5 with GlobalFileException

use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.

the class BaseApiClient method uploadImg.

@Override
public VirtualFile uploadImg(File file) {
    this.check();
    if (file == null) {
        throw new QiniuApiException("[" + this.storageType + "]文件上传失败:文件不可为空");
    }
    try {
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        VirtualFile res = this.uploadImg(is, "temp" + FileUtil.getSuffix(file));
        VirtualFile imageInfo = ImageUtil.getInfo(file);
        return res.setSize(imageInfo.getSize()).setOriginalFileName(file.getName()).setWidth(imageInfo.getWidth()).setHeight(imageInfo.getHeight());
    } catch (FileNotFoundException e) {
        throw new GlobalFileException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) QiniuApiException(com.zyd.blog.file.exception.QiniuApiException)

Aggregations

GlobalFileException (com.zyd.blog.file.exception.GlobalFileException)8 VirtualFile (com.zyd.blog.file.entity.VirtualFile)3 FileUploader (com.zyd.blog.file.FileUploader)2 ZhydException (com.zyd.blog.framework.exception.ZhydException)2 GlobalFileUploader (com.zyd.blog.plugin.file.GlobalFileUploader)2 ResponseStatus (com.zyd.blog.business.enums.ResponseStatus)1 SysConfigService (com.zyd.blog.business.service.SysConfigService)1 AliyunOssApiClient (com.zyd.blog.file.AliyunOssApiClient)1 ApiClient (com.zyd.blog.file.ApiClient)1 LocalApiClient (com.zyd.blog.file.LocalApiClient)1 QiniuApiClient (com.zyd.blog.file.QiniuApiClient)1 OssApiException (com.zyd.blog.file.exception.OssApiException)1 QiniuApiException (com.zyd.blog.file.exception.QiniuApiException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Date (java.util.Date)1 Transactional (org.springframework.transaction.annotation.Transactional)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1