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());
}
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;
}
}
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();
}
Aggregations