forked from kidgrow-microservices-platform

zhaoxiaohao
2021-03-02 8b7c75249a83b45dd53cc2177bb61bfa3d7e55cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.kidgrow.filecenter.config;
 
import com.aliyun.oss.model.OSSObject;
import com.kidgrow.filecenter.model.FileInfo;
import com.kidgrow.filecenter.properties.FileServerProperties;
import com.kidgrow.filecenter.service.impl.AbstractIFileService;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.InputStream;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 七牛云配置<br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/18 11:24 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@Configuration
@ConditionalOnProperty(name = "kidgrow.file-server.type", havingValue = "qiniu")
public class QiniuOSSAutoConfigure {
    @Autowired
    private FileServerProperties fileProperties;
 
    /**
     * 华东机房
     */
    @Bean
    public com.qiniu.storage.Configuration qiniuConfig() {
        return new com.qiniu.storage.Configuration(Zone.zone0());
    }
 
    /**
     * 构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager() {
        return new UploadManager(qiniuConfig());
    }
 
    /**
     * 认证信息实例
     *
     * @return
     */
    @Bean
    public Auth auth() {
        return Auth.create(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret());
    }
 
    /**
     * 构建七牛空间管理实例
     */
    @Bean
    public BucketManager bucketManager() {
        return new BucketManager(auth(), qiniuConfig());
    }
 
    @Service
    public class QiniuOssServiceImpl extends AbstractIFileService {
        @Autowired
        private UploadManager uploadManager;
        @Autowired
        private BucketManager bucketManager;
        @Autowired
        private Auth auth;
 
        @Override
        protected String fileType() {
            return fileProperties.getType();
        }
 
        @Override
        protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            // 调用put方法上传
            uploadManager.put(file.getBytes(), fileInfo.getName(), auth.uploadToken(fileProperties.getOss().getBucketName()));
            fileInfo.setUrl(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName());
            fileInfo.setPath(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName());
        }
 
        @Override
        protected void fileUpLoadOss(FileInfo fileInfo, String newFilePath, InputStream fileStream,String fileFolder) {
 
        }
 
        @Override
        protected String FilePath(String imgType, String folderByDate,String hospitalId,String departmentId) {
            return null;
        }
 
        @Override
        protected OSSObject down(String url) {
            return null;
        }
 
        @Override
        protected boolean deleteFile(FileInfo fileInfo) {
            try {
                Response response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath());
                int retry = 0;
                while (response.needRetry() && retry++ < 3) {
                    response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath());
                }
            } catch (QiniuException e) {
                return false;
            }
            return true;
        }
    }
}