Search in sources :

Example 1 with QiNiuContent

use of com.dimple.project.tool.domain.QiNiuContent 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 2 with QiNiuContent

use of com.dimple.project.tool.domain.QiNiuContent 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 3 with QiNiuContent

use of com.dimple.project.tool.domain.QiNiuContent 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 4 with QiNiuContent

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

Aggregations

CustomException (com.dimple.common.exception.CustomException)4 QiNiuConfig (com.dimple.project.common.domain.QiNiuConfig)4 QiNiuContent (com.dimple.project.tool.domain.QiNiuContent)4 Auth (com.qiniu.util.Auth)4 Configuration (com.qiniu.storage.Configuration)3 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