Search in sources :

Example 1 with QiNiuConfig

use of com.dimple.project.common.domain.QiNiuConfig in project DimpleBlog by martin-chips.

the class QiNiuServiceImpl method updateQiNiuConfig.

@Override
public int updateQiNiuConfig(QiNiuConfig qiNiuConfig) {
    String qiNiuConfigString = JSON.toJSONString(qiNiuConfig);
    Config config = new Config();
    config.setConfigKey(ConfigKey.CONFIG_KEY_QI_NIU);
    config.setConfigValue(qiNiuConfigString);
    config.setUpdateBy(SecurityUtils.getUsername());
    return configService.updateConfigByConfigKey(config);
}
Also used : Config(com.dimple.project.system.domain.Config) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig)

Example 2 with QiNiuConfig

use of com.dimple.project.common.domain.QiNiuConfig 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 QiNiuConfig

use of com.dimple.project.common.domain.QiNiuConfig in project DimpleBlog by martin-chips.

the class QiNiuServiceImpl method getQiNiuConfig.

@Override
public QiNiuConfig getQiNiuConfig() {
    Config config = configService.selectConfigByKey(ConfigKey.CONFIG_KEY_QI_NIU);
    // 七牛云Config的配置Json
    String configValue = config.getConfigValue();
    if (StringUtils.isEmpty(configValue)) {
        return new QiNiuConfig();
    }
    return JSON.parseObject(configValue, QiNiuConfig.class);
}
Also used : Config(com.dimple.project.system.domain.Config) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig)

Example 4 with QiNiuConfig

use of com.dimple.project.common.domain.QiNiuConfig 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 5 with QiNiuConfig

use of com.dimple.project.common.domain.QiNiuConfig 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)

Aggregations

QiNiuConfig (com.dimple.project.common.domain.QiNiuConfig)6 CustomException (com.dimple.common.exception.CustomException)4 QiNiuContent (com.dimple.project.tool.domain.QiNiuContent)4 Auth (com.qiniu.util.Auth)4 Configuration (com.qiniu.storage.Configuration)3 Config (com.dimple.project.system.domain.Config)2 QiniuException (com.qiniu.common.QiniuException)2 BucketManager (com.qiniu.storage.BucketManager)2 Response (com.qiniu.http.Response)1 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 FileInfo (com.qiniu.storage.model.FileInfo)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Transactional (org.springframework.transaction.annotation.Transactional)1