Search in sources :

Example 1 with QiniuContent

use of me.zhengjie.domain.QiniuContent in project eladmin by elunez.

the class QiNiuServiceImpl method synchronize.

@Override
@Transactional(rollbackFor = Exception.class)
public void synchronize(QiniuConfig config) {
    if (config.getId() == null) {
        throw new BadRequestException("请先添加相应配置,再操作");
    }
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
    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 (qiniuContentRepository.findByKey(FileUtil.getFileNameNoEx(item.key)) == null) {
                qiniuContent = new QiniuContent();
                qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize + "")));
                qiniuContent.setSuffix(FileUtil.getExtensionName(item.key));
                qiniuContent.setKey(FileUtil.getFileNameNoEx(item.key));
                qiniuContent.setType(config.getType());
                qiniuContent.setBucket(config.getBucket());
                qiniuContent.setUrl(config.getHost() + "/" + item.key);
                qiniuContentRepository.save(qiniuContent);
            }
        }
    }
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) BadRequestException(me.zhengjie.exception.BadRequestException) QiniuContent(me.zhengjie.domain.QiniuContent) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with QiniuContent

use of me.zhengjie.domain.QiniuContent in project eladmin by elunez.

the class QiNiuServiceImpl method upload.

@Override
@Transactional(rollbackFor = Exception.class)
public QiniuContent upload(MultipartFile file, QiniuConfig qiniuConfig) {
    FileUtil.checkSize(maxSize, file.getSize());
    if (qiniuConfig.getId() == null) {
        throw new BadRequestException("请先添加相应配置,再操作");
    }
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtil.getRegion(qiniuConfig.getZone()));
    UploadManager uploadManager = new UploadManager(cfg);
    Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
    String upToken = auth.uploadToken(qiniuConfig.getBucket());
    try {
        String key = file.getOriginalFilename();
        if (qiniuContentRepository.findByKey(key) != null) {
            key = QiNiuUtil.getKey(key);
        }
        Response response = uploadManager.put(file.getBytes(), key, upToken);
        // 解析上传成功的结果
        DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
        QiniuContent content = qiniuContentRepository.findByKey(FileUtil.getFileNameNoEx(putRet.key));
        if (content == null) {
            // 存入数据库
            QiniuContent qiniuContent = new QiniuContent();
            qiniuContent.setSuffix(FileUtil.getExtensionName(putRet.key));
            qiniuContent.setBucket(qiniuConfig.getBucket());
            qiniuContent.setType(qiniuConfig.getType());
            qiniuContent.setKey(FileUtil.getFileNameNoEx(putRet.key));
            qiniuContent.setUrl(qiniuConfig.getHost() + "/" + putRet.key);
            qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(file.getSize() + "")));
            return qiniuContentRepository.save(qiniuContent);
        }
        return content;
    } catch (Exception e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : Response(com.qiniu.http.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) BadRequestException(me.zhengjie.exception.BadRequestException) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) QiniuContent(me.zhengjie.domain.QiniuContent) UploadManager(com.qiniu.storage.UploadManager) QiniuException(com.qiniu.common.QiniuException) IOException(java.io.IOException) BadRequestException(me.zhengjie.exception.BadRequestException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with QiniuContent

use of me.zhengjie.domain.QiniuContent in project eladmin by elunez.

the class QiNiuServiceImpl method findByContentId.

@Override
public QiniuContent findByContentId(Long id) {
    QiniuContent qiniuContent = qiniuContentRepository.findById(id).orElseGet(QiniuContent::new);
    ValidationUtil.isNull(qiniuContent.getId(), "QiniuContent", "id", id);
    return qiniuContent;
}
Also used : QiniuContent(me.zhengjie.domain.QiniuContent)

Example 4 with QiniuContent

use of me.zhengjie.domain.QiniuContent in project eladmin by elunez.

the class QiniuController method uploadQiNiu.

@Log("上传文件")
@ApiOperation("上传文件")
@PostMapping
public ResponseEntity<Object> uploadQiNiu(@RequestParam MultipartFile file) {
    QiniuContent qiniuContent = qiNiuService.upload(file, qiNiuService.find());
    Map<String, Object> map = new HashMap<>(3);
    map.put("id", qiniuContent.getId());
    map.put("errno", 0);
    map.put("data", new String[] { qiniuContent.getUrl() });
    return new ResponseEntity<>(map, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) QiniuContent(me.zhengjie.domain.QiniuContent) Log(me.zhengjie.annotation.Log) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with QiniuContent

use of me.zhengjie.domain.QiniuContent in project eladmin by elunez.

the class QiNiuServiceImpl method downloadList.

@Override
public void downloadList(List<QiniuContent> queryAll, HttpServletResponse response) throws IOException {
    List<Map<String, Object>> list = new ArrayList<>();
    for (QiniuContent content : queryAll) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("文件名", content.getKey());
        map.put("文件类型", content.getSuffix());
        map.put("空间名称", content.getBucket());
        map.put("文件大小", content.getSize());
        map.put("空间类型", content.getType());
        map.put("创建日期", content.getUpdateTime());
        list.add(map);
    }
    FileUtil.downloadExcel(list, response);
}
Also used : QiniuContent(me.zhengjie.domain.QiniuContent)

Aggregations

QiniuContent (me.zhengjie.domain.QiniuContent)5 Configuration (com.qiniu.storage.Configuration)2 Auth (com.qiniu.util.Auth)2 BadRequestException (me.zhengjie.exception.BadRequestException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 QiniuException (com.qiniu.common.QiniuException)1 Response (com.qiniu.http.Response)1 BucketManager (com.qiniu.storage.BucketManager)1 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 FileInfo (com.qiniu.storage.model.FileInfo)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Log (me.zhengjie.annotation.Log)1 ResponseEntity (org.springframework.http.ResponseEntity)1