Search in sources :

Example 1 with QiniuContent

use of com.wayn.common.core.domain.tool.QiniuContent in project waynboot-mall by wayn111.

the class QiniuController method upload.

@PostMapping("upload")
public R upload(@RequestParam MultipartFile file) throws IOException {
    QiniuConfig qiniuConfig = iQiniuConfigService.getById(1);
    if (qiniuConfig == null) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_NOT_EXISTS_ERROR);
    }
    if (StringUtils.isEmpty(qiniuConfig.getAccessKey())) {
        return R.error(ReturnCodeEnum.TOOL_QINIU_CONFIG_ERROR);
    }
    QiniuContent qiniuContent = iQiniuContentService.upload(file, qiniuConfig);
    return R.result(iQiniuContentService.save(qiniuContent)).add("id", qiniuContent.getContentId()).add("fileUrl", qiniuContent.getUrl());
}
Also used : QiniuConfig(com.wayn.common.core.domain.tool.QiniuConfig) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent)

Example 2 with QiniuContent

use of com.wayn.common.core.domain.tool.QiniuContent in project waynboot-mall by wayn111.

the class QiniuContentServiceImpl method upload.

@Override
public QiniuContent upload(MultipartFile file, QiniuConfig qiniuConfig) throws IOException {
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiniuUtil.getRegion(qiniuConfig.getRegion()));
    UploadManager uploadManager = new UploadManager(cfg);
    Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
    String upToken = auth.uploadToken(qiniuConfig.getBucket());
    String key = file.getOriginalFilename();
    if (getOne(new QueryWrapper<QiniuContent>().eq("name", FilenameUtils.getBaseName(key))) != null) {
        key = QiniuUtil.getKey(key);
    }
    Response response = uploadManager.put(file.getBytes(), key, upToken);
    DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
    // 存入数据库
    QiniuContent qiniuContent = new QiniuContent();
    qiniuContent.setSuffix(FilenameUtils.getExtension(key));
    qiniuContent.setBucket(qiniuConfig.getBucket());
    if (qiniuConfig.getType() == 0) {
        qiniuContent.setType("公开");
    } else {
        qiniuContent.setType("私有");
    }
    qiniuContent.setName(FilenameUtils.getBaseName(key));
    qiniuContent.setUrl(qiniuConfig.getHost() + "/" + putRet.key);
    qiniuContent.setSize(FileUtils.getSize(Integer.parseInt(file.getSize() + "")));
    qiniuContent.setCreateTime(new Date());
    return qiniuContent;
}
Also used : Response(com.qiniu.http.Response) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent) UploadManager(com.qiniu.storage.UploadManager) Date(java.util.Date)

Example 3 with QiniuContent

use of com.wayn.common.core.domain.tool.QiniuContent in project waynboot-mall by wayn111.

the class QiniuContentServiceImpl method syncQiniu.

@Override
public boolean syncQiniu(QiniuConfig config) {
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiniuUtil.getRegion(config.getRegion()));
    Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    // 文件名前缀
    String prefix = "";
    // 每次迭代的长度限制,最大1000,推荐值 1000
    int limit = 1000;
    // 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
    String delimiter = "";
    // 列举空间文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(config.getBucket(), prefix, limit, delimiter);
    while (fileListIterator.hasNext()) {
        // 处理获取的file list结果
        QiniuContent qiniuContent;
        FileInfo[] items = fileListIterator.next();
        for (FileInfo item : items) {
            if (getOne(new QueryWrapper<QiniuContent>().eq("name", FilenameUtils.getBaseName(item.key))) == null) {
                qiniuContent = new QiniuContent();
                qiniuContent.setSize(FileUtils.getSize(Integer.parseInt(item.fsize + "")));
                qiniuContent.setSuffix(FilenameUtils.getExtension(item.key));
                qiniuContent.setName(FilenameUtils.getBaseName(item.key));
                if (config.getType() == 0) {
                    qiniuContent.setType("公开");
                } else {
                    qiniuContent.setType("私有");
                }
                qiniuContent.setBucket(config.getBucket());
                qiniuContent.setUrl(config.getHost() + "/" + item.key);
                qiniuContent.setCreateTime(new Date());
                save(qiniuContent);
            }
        }
    }
    return true;
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent) Date(java.util.Date)

Example 4 with QiniuContent

use of com.wayn.common.core.domain.tool.QiniuContent in project waynboot-mall by wayn111.

the class QiniuContentServiceImpl method download.

@Override
public String download(Long contentId, QiniuConfig qiniuConfig) {
    QiniuContent content = getById(contentId);
    String finalUrl;
    String type = "公开";
    if (type.equals(content.getType())) {
        finalUrl = content.getUrl();
    } else {
        Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
        // 1小时,可以自定义链接过期时间
        long expireInSeconds = 3600;
        finalUrl = auth.privateDownloadUrl(content.getUrl(), expireInSeconds);
    }
    return finalUrl;
}
Also used : Auth(com.qiniu.util.Auth) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent)

Example 5 with QiniuContent

use of com.wayn.common.core.domain.tool.QiniuContent in project waynboot-mall by wayn111.

the class QiniuContentServiceImpl method delete.

@Override
public boolean delete(Long id, QiniuConfig config) throws QiniuException {
    QiniuContent content = getById(id);
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiniuUtil.getRegion(config.getRegion()));
    Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    Response response = bucketManager.delete(content.getBucket(), content.getName() + "." + content.getSuffix());
    if (!response.isOK()) {
        return false;
    }
    removeById(content);
    return true;
}
Also used : Response(com.qiniu.http.Response) Configuration(com.qiniu.storage.Configuration) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) QiniuContent(com.wayn.common.core.domain.tool.QiniuContent)

Aggregations

QiniuContent (com.wayn.common.core.domain.tool.QiniuContent)5 Auth (com.qiniu.util.Auth)4 Configuration (com.qiniu.storage.Configuration)3 Response (com.qiniu.http.Response)2 BucketManager (com.qiniu.storage.BucketManager)2 Date (java.util.Date)2 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 FileInfo (com.qiniu.storage.model.FileInfo)1 QiniuConfig (com.wayn.common.core.domain.tool.QiniuConfig)1