Search in sources :

Example 11 with CustomException

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;
}
Also used : Response(com.qiniu.http.Response) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig) QiNiuContent(com.dimple.project.tool.domain.QiNiuContent) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) CustomException(com.dimple.common.exception.CustomException) SimpleDateFormat(java.text.SimpleDateFormat) UploadManager(com.qiniu.storage.UploadManager) Date(java.util.Date) QiniuException(com.qiniu.common.QiniuException) CustomException(com.dimple.common.exception.CustomException)

Example 12 with CustomException

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);
    }
}
Also used : Auth(com.qiniu.util.Auth) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig) QiNiuContent(com.dimple.project.tool.domain.QiNiuContent) CustomException(com.dimple.common.exception.CustomException)

Example 13 with CustomException

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);
}
Also used : QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig) QiNiuContent(com.dimple.project.tool.domain.QiNiuContent) CustomException(com.dimple.common.exception.CustomException)

Example 14 with CustomException

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);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Example 15 with CustomException

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);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Aggregations

CustomException (com.dimple.common.exception.CustomException)19 QiNiuConfig (com.dimple.project.common.domain.QiNiuConfig)4 QiNiuContent (com.dimple.project.tool.domain.QiNiuContent)4 Auth (com.qiniu.util.Auth)4 CronTrigger (org.quartz.CronTrigger)4 JobKey (org.quartz.JobKey)4 QuartzJob (com.dimple.project.tool.domain.QuartzJob)3 Configuration (com.qiniu.storage.Configuration)3 Date (java.util.Date)3 TriggerKey (org.quartz.TriggerKey)3 LocalStorage (com.dimple.project.tool.domain.LocalStorage)2 QiniuException (com.qiniu.common.QiniuException)2 BucketManager (com.qiniu.storage.BucketManager)2 CronTriggerImpl (org.quartz.impl.triggers.CronTriggerImpl)2 CaptchaException (com.dimple.common.exception.user.CaptchaException)1 CaptchaExpireException (com.dimple.common.exception.user.CaptchaExpireException)1 UserPasswordNotMatchException (com.dimple.common.exception.user.UserPasswordNotMatchException)1 CacheExpire (com.dimple.framework.config.redis.CacheExpire)1 LoginUser (com.dimple.framework.security.LoginUser)1 Config (com.dimple.project.system.domain.Config)1