Search in sources :

Example 6 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(MultipartFile file) {
    this.check();
    if (file == null) {
        throw new OssApiException("[" + this.storageType + "]文件上传失败:文件不可为空");
    }
    try {
        VirtualFile res = this.uploadImg(file.getInputStream(), file.getOriginalFilename());
        VirtualFile imageInfo = ImageUtil.getInfo(file);
        return res.setSize(imageInfo.getSize()).setOriginalFileName(file.getOriginalFilename()).setWidth(imageInfo.getWidth()).setHeight(imageInfo.getHeight());
    } catch (IOException e) {
        throw new GlobalFileException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) OssApiException(com.zyd.blog.file.exception.OssApiException)

Example 7 with GlobalFileException

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

the class BaseFileUploader method getApiClient.

ApiClient getApiClient(String uploadType) {
    SysConfigService configService = SpringContextHolder.getBean(SysConfigService.class);
    Map<String, Object> config = configService.getConfigs();
    String storageType = null;
    if (null == config || StringUtils.isEmpty((storageType = (String) config.get(ConfigKeyEnum.STORAGE_TYPE.getKey())))) {
        throw new ZhydException("[文件服务]当前系统暂未配置文件服务相关的内容!");
    }
    ApiClient res = null;
    switch(storageType) {
        case "local":
            String localFileUrl = (String) config.get(ConfigKeyEnum.LOCAL_FILE_URL.getKey()), localFilePath = (String) config.get(ConfigKeyEnum.LOCAL_FILE_PATH.getKey());
            res = new LocalApiClient().init(localFileUrl, localFilePath, uploadType);
            break;
        case "qiniu":
            String accessKey = (String) config.get(ConfigKeyEnum.QINIU_ACCESS_KEY.getKey()), secretKey = (String) config.get(ConfigKeyEnum.QINIU_SECRET_KEY.getKey()), qiniuBucketName = (String) config.get(ConfigKeyEnum.QINIU_BUCKET_NAME.getKey()), baseUrl = (String) config.get(ConfigKeyEnum.QINIU_BASE_PATH.getKey());
            res = new QiniuApiClient().init(accessKey, secretKey, qiniuBucketName, baseUrl, uploadType);
            break;
        case "aliyun":
            String endpoint = (String) config.get(ConfigKeyEnum.ALIYUN_ENDPOINT.getKey()), accessKeyId = (String) config.get(ConfigKeyEnum.ALIYUN_ACCESS_KEY.getKey()), accessKeySecret = (String) config.get(ConfigKeyEnum.ALIYUN_ACCESS_KEY_SECRET.getKey()), url = (String) config.get(ConfigKeyEnum.ALIYUN_FILE_URL.getKey()), aliYunBucketName = (String) config.get(ConfigKeyEnum.ALIYUN_BUCKET_NAME.getKey());
            res = new AliyunOssApiClient().init(endpoint, accessKeyId, accessKeySecret, url, aliYunBucketName, uploadType);
            break;
        case "youpaiyun":
            break;
        default:
            break;
    }
    if (null == res) {
        throw new GlobalFileException("[文件服务]当前系统暂未配置文件服务相关的内容!");
    }
    return res;
}
Also used : ZhydException(com.zyd.blog.framework.exception.ZhydException) LocalApiClient(com.zyd.blog.file.LocalApiClient) QiniuApiClient(com.zyd.blog.file.QiniuApiClient) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) SysConfigService(com.zyd.blog.business.service.SysConfigService) AliyunOssApiClient(com.zyd.blog.file.AliyunOssApiClient) QiniuApiClient(com.zyd.blog.file.QiniuApiClient) AliyunOssApiClient(com.zyd.blog.file.AliyunOssApiClient) LocalApiClient(com.zyd.blog.file.LocalApiClient) ApiClient(com.zyd.blog.file.ApiClient)

Example 8 with GlobalFileException

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

the class BizFileServiceImpl method upload.

@Override
@Transactional(rollbackFor = Exception.class)
public int upload(MultipartFile[] file) {
    if (null == file || file.length == 0) {
        throw new GlobalFileException("请至少选择一张图片!");
    }
    for (MultipartFile multipartFile : file) {
        FileUploader uploader = new GlobalFileUploader();
        uploader.upload(multipartFile, FileUploadType.COMMON.getPath(), true);
    }
    return file.length;
}
Also used : GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) MultipartFile(org.springframework.web.multipart.MultipartFile) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) Transactional(org.springframework.transaction.annotation.Transactional)

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