Search in sources :

Example 1 with CustomException

use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.

the class LocalStorageServiceImpl method upload.

@Override
public int upload(String name, MultipartFile multipartFile) {
    // 检查大小
    // 获取后缀
    String suffix = FileUtils.getExtensionName(multipartFile.getOriginalFilename());
    String type = FileUtils.getFileType(suffix);
    File file = FileUtils.upload(multipartFile, DimpleBlogConfig.getProfile() + type + File.separator);
    if (Objects.isNull(file)) {
        throw new CustomException("上传失败");
    }
    // 防止异常出错
    try {
        name = StringUtils.isBlank(name) ? FileUtils.getFileNameNoExtension(multipartFile.getOriginalFilename()) : name;
        LocalStorage localStorage = new LocalStorage(file.getName(), name, suffix, file.getPath(), type, FileUtils.getSizeString(multipartFile.getSize()));
        localStorage.setCreateBy(SecurityUtils.getUsername());
        return localStorageMapper.insertLocalStorage(localStorage);
    } catch (Exception e) {
        FileUtils.del(file);
        throw e;
    }
}
Also used : LocalStorage(com.dimple.project.tool.domain.LocalStorage) CustomException(com.dimple.common.exception.CustomException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) CustomException(com.dimple.common.exception.CustomException)

Example 2 with CustomException

use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.

the class QiNiuServiceImpl method synchronize.

@Override
@Transactional
public int synchronize() {
    QiNiuConfig qiNiuConfig = getQiNiuConfig();
    if (!qiNiuConfig.check()) {
        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);
    // 文件名前缀
    String prefix = "";
    // 每次迭代的长度限制,最大1000,推荐值 1000
    int limit = 1000;
    // 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
    String delimiter = "";
    // 列举空间文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(qiNiuConfig.getBucket(), prefix, limit, delimiter);
    int count = 0;
    while (fileListIterator.hasNext()) {
        // 处理获取的file list结果
        QiNiuContent qiNiuContent;
        // 删除所有数据
        qiNiuContentMapper.clearContent();
        FileInfo[] items = fileListIterator.next();
        if (Objects.nonNull(items)) {
            String username = SecurityUtils.getUsername();
            for (FileInfo item : items) {
                qiNiuContent = new QiNiuContent();
                qiNiuContent.setSize(FileUtils.getSizeString(Integer.parseInt(item.fsize + "")));
                qiNiuContent.setSuffix(FileUtils.getExtensionName(item.key));
                qiNiuContent.setName(FileUtils.getFileNameNoExtension(item.key));
                qiNiuContent.setType(qiNiuConfig.getType());
                qiNiuContent.setBucket(qiNiuConfig.getBucket());
                qiNiuContent.setUrl("http://" + qiNiuConfig.getHost() + "/" + item.key);
                qiNiuContent.setCreateBy(username);
                qiNiuContent.setCreateTime(convertUnixTime(item.putTime + ""));
                count += qiNiuContentMapper.insertContent(qiNiuContent);
            }
        }
    }
    return count;
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with CustomException

use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.

the class QuartzJobServiceImpl method executeQuartzJobById.

@Override
public void executeQuartzJobById(Long id) {
    QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
    if (Objects.isNull(quartzJob)) {
        throw new CustomException("当前任务不存在");
    }
    quartzManage.runAJobNow(quartzJob);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Example 4 with CustomException

use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.

the class QuartzManage method deleteJob.

/**
 * 删除一个job
 */
public void deleteJob(QuartzJob quartzJob) {
    try {
        JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
        scheduler.pauseJob(jobKey);
        scheduler.deleteJob(jobKey);
    } catch (Exception e) {
        log.error("删除定时任务失败", e);
        throw new CustomException("删除定时任务失败");
    }
}
Also used : JobKey(org.quartz.JobKey) CustomException(com.dimple.common.exception.CustomException) CustomException(com.dimple.common.exception.CustomException)

Example 5 with CustomException

use of com.dimple.common.exception.CustomException in project DimpleBlog by martin-chips.

the class QuartzManage method addJob.

/**
 * 添加任务
 *
 * @param quartzJob
 */
public void addJob(QuartzJob quartzJob) {
    try {
        log.info("开始创建任务{}", quartzJob.getJobName());
        // 构建job信息
        JobDetail jobDetail = JobBuilder.newJob(ExecutionJob.class).withIdentity(JOB_NAME + quartzJob.getId()).build();
        // 通过触发器名和cron 表达式创建 Trigger
        Trigger cronTrigger = newTrigger().withIdentity(JOB_NAME + quartzJob.getId()).startNow().withSchedule(CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression())).build();
        cronTrigger.getJobDataMap().put(QuartzJob.JOB_KEY, quartzJob);
        // 重置启动时间
        ((CronTriggerImpl) cronTrigger).setStartTime(new Date());
        // 执行定时任务
        scheduler.scheduleJob(jobDetail, cronTrigger);
        // 暂停任务
        if (!quartzJob.getStatus().booleanValue()) {
            pauseJob(quartzJob);
        }
    } catch (Exception e) {
        log.error("创建定时任务失败", e);
        throw new CustomException("创建定时任务失败");
    }
}
Also used : JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) CronTriggerImpl(org.quartz.impl.triggers.CronTriggerImpl) CustomException(com.dimple.common.exception.CustomException) Date(java.util.Date) 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