use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.
the class QiNiuServiceImpl method upload.
@Override
public QiNiuContent upload(MultipartFile file) {
// 获取七牛云信息
QiNiuConfig qiNiuConfig = getQiNiuConfig();
if (!qiNiuConfig.check()) {
throw new CustomException("七牛云配置信息不完整,请先填写七牛云配置信息");
}
// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(QiNiuUtils.getRegion(qiNiuConfig.getZone()));
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
// 生成上传文件Token
String upToken = auth.uploadToken(qiNiuConfig.getBucket());
QiNiuContent qiNiuContent = new QiNiuContent();
try {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String key = FileUtils.getFileNameNoExtension(file.getOriginalFilename()) + df.format(new Date()) + "." + FileUtils.getExtensionName(file.getOriginalFilename());
Response response = uploadManager.put(file.getBytes(), key, upToken);
// 解析
DefaultPutRet defaultPutRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
// 将结果存入数据库
qiNiuContent.setSuffix(FileUtils.getExtensionName(defaultPutRet.key));
qiNiuContent.setBucket(qiNiuConfig.getBucket());
qiNiuContent.setType(qiNiuConfig.getType());
qiNiuContent.setName(FileUtils.getFileNameNoExtension(defaultPutRet.key));
qiNiuContent.setUrl("http://" + qiNiuConfig.getHost() + "/" + defaultPutRet.key);
qiNiuContent.setSize(FileUtils.getSizeString(Integer.parseInt(file.getSize() + "")));
qiNiuContentMapper.insertContent(qiNiuContent);
} catch (Exception e) {
throw new CustomException(e.getMessage());
}
return qiNiuContent;
}
use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.
the class QiNiuServiceImpl method getDownloadUrl.
@Override
public String getDownloadUrl(Long id) {
QiNiuConfig qiNiuConfig = getQiNiuConfig();
if (!qiNiuConfig.check()) {
throw new CustomException("七牛云配置信息不完整,请先填写七牛云配置信息");
}
QiNiuContent qiNiuContent = qiNiuContentMapper.selectContentById(id);
if (Objects.isNull(qiNiuConfig)) {
throw new CustomException("对应文件不存在,建议同步数据后再试");
}
if ("公开".equals(qiNiuConfig.getType())) {
return qiNiuContent.getUrl();
} else {
Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
// 1小时,可以自定义链接过期时间
long expireInSeconds = 3600;
return auth.privateDownloadUrl(qiNiuContent.getUrl(), expireInSeconds);
}
}
use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.
the class QiNiuServiceImpl method deleteQiNiuContent.
@Override
public int deleteQiNiuContent(String ids) {
QiNiuConfig qiNiuConfig = getQiNiuConfig();
if (!qiNiuConfig.check()) {
throw new CustomException("七牛云配置信息不完整,请先填写七牛云配置信息");
}
Long[] idArray = ConvertUtils.toLongArray(ids);
// 查询
List<QiNiuContent> qiNiuContentList = qiNiuContentMapper.selectContentByIds(idArray);
for (QiNiuContent qiNiuContent : qiNiuContentList) {
if (Objects.isNull(qiNiuContent) || StringUtils.isEmpty(qiNiuContent.getName())) {
throw new CustomException("数据异常");
}
// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(QiNiuUtils.getRegion(qiNiuConfig.getZone()));
Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(qiNiuContent.getBucket(), qiNiuContent.getName());
} catch (QiniuException e) {
log.error("删除七牛云图片出错,{},", e.getMessage(), e);
// 出错后删除本地数据库文件
}
}
return qiNiuContentMapper.deleteContentByIds(idArray);
}
use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.
the class QuartzJobServiceImpl method deleteQuartzJob.
@Override
public int deleteQuartzJob(Long id) {
QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
if (Objects.isNull(quartzJob)) {
throw new CustomException("当前任务不存在");
}
quartzManage.deleteJob(quartzJob);
String username = SecurityUtils.getUsername();
return quartzJobMapper.deleteQuartzJobById(id, username);
}
use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.
the class QuartzJobServiceImpl method updateQuartzJobStatus.
@Override
public int updateQuartzJobStatus(Long id) {
QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
if (Objects.isNull(quartzJob)) {
throw new CustomException("当前任务不存在");
}
// 如果当前为运行状态
if (quartzJob.getStatus().booleanValue()) {
quartzManage.pauseJob(quartzJob);
} else {
quartzManage.resumeJob(quartzJob);
}
quartzJob.setStatus(!quartzJob.getStatus());
return quartzJobMapper.updateQuartzJob(quartzJob);
}
Aggregations