Search in sources :

Example 1 with QiniuConfig

use of com.wayn.common.core.domain.tool.QiniuConfig in project waynboot-mall by wayn111.

the class QiniuController method upload.

@PostMapping("upload")
public R upload(@RequestParam MultipartFile file) throws IOException {
    QiniuConfig qiniuConfig = iQiniuConfigService.getById(1);
    if (qiniuConfig == null) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_NOT_EXISTS_ERROR);
    }
    if (StringUtils.isEmpty(qiniuConfig.getAccessKey())) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_CONFIG_ERROR);
    }
    QiniuContent qiniuContent = iQiniuContentService.upload(file, qiniuConfig);
    return R.result(iQiniuContentService.save(qiniuContent)).add("id", qiniuContent.getContentId()).add("fileUrl", qiniuContent.getUrl());
}
Also used : QiniuConfig(com.wayn.common.core.domain.tool.QiniuConfig) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent)

Example 2 with QiniuConfig

use of com.wayn.common.core.domain.tool.QiniuConfig in project waynboot-mall by wayn111.

the class UploadServiceImpl method uploadFile.

/**
 * 七牛云上传文件
 *
 * @param fileName 文件名
 * @return 上传后的文件访问路径
 * @throws QiniuException 七牛异常
 */
@Override
public String uploadFile(String fileName) {
    QiniuConfig qiniuConfig = iQiniuConfigService.getById(1);
    if (qiniuConfig != null && qiniuConfig.getEnable()) {
        File file = new File(WaynConfig.getUploadDir() + File.separator + fileName);
        Configuration cfg = new Configuration(QiniuUtil.getRegion(qiniuConfig.getRegion()));
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
        String upToken = auth.uploadToken(qiniuConfig.getBucket());
        Response response;
        try {
            response = uploadManager.put(file, file.getName(), upToken);
            int retry = 0;
            while (response.needRetry() && retry < 3) {
                response = uploadManager.put(file, file.getName(), upToken);
                retry++;
            }
            if (response.isOK()) {
                JSONObject jsonObject = JSONObject.parseObject(response.bodyString());
                String yunFileName = jsonObject.getString("key");
                String yunFilePath = qiniuConfig.getHost() + "/" + yunFileName;
                FileUtils.deleteQuietly(file);
                log.info("【文件上传至七牛云】绝对路径:{}", yunFilePath);
                return yunFilePath;
            } else {
                log.error("【文件上传至七牛云】失败,{}", JSONObject.toJSONString(response));
                FileUtils.deleteQuietly(file);
                throw new BusinessException("文件上传至七牛云失败");
            }
        } catch (QiniuException e) {
            FileUtils.deleteQuietly(file);
            throw new BusinessException("文件上传至七牛云失败");
        }
    } else {
        String requestUrl = HttpUtil.getRequestContext(ServletUtils.getRequest());
        return requestUrl + "/upload/" + fileName;
    }
}
Also used : Response(com.qiniu.http.Response) BusinessException(com.wayn.common.exception.BusinessException) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) JSONObject(com.alibaba.fastjson.JSONObject) Auth(com.qiniu.util.Auth) QiniuConfig(com.wayn.common.core.domain.tool.QiniuConfig) File(java.io.File) UploadManager(com.qiniu.storage.UploadManager)

Example 3 with QiniuConfig

use of com.wayn.common.core.domain.tool.QiniuConfig in project waynboot-mall by wayn111.

the class QiniuController method syncQiniu.

@GetMapping("syncQiniu")
public R syncQiniu() {
    QiniuConfig qiniuConfig = iQiniuConfigService.getById(1);
    if (qiniuConfig == null) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_NOT_EXISTS_ERROR);
    }
    if (StringUtils.isEmpty(qiniuConfig.getAccessKey())) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_CONFIG_ERROR);
    }
    iQiniuContentService.syncQiniu(qiniuConfig);
    return R.success();
}
Also used : QiniuConfig(com.wayn.common.core.domain.tool.QiniuConfig)

Aggregations

QiniuConfig (com.wayn.common.core.domain.tool.QiniuConfig)3 JSONObject (com.alibaba.fastjson.JSONObject)1 QiniuException (com.qiniu.common.QiniuException)1 Response (com.qiniu.http.Response)1 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 Auth (com.qiniu.util.Auth)1 QiniuContent (com.wayn.common.core.domain.tool.QiniuContent)1 BusinessException (com.wayn.common.exception.BusinessException)1 File (java.io.File)1