wang-hao-jie
2021-10-19 e48043a2df9ca0c73fe18298bab3c4d42ca5c0c7
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cn.exrick.xboot.file.manage.impl;
 
import cn.exrick.xboot.core.common.constant.SettingConstant;
import cn.exrick.xboot.core.common.exception.XbootException;
import cn.exrick.xboot.core.entity.Setting;
import cn.exrick.xboot.core.service.SettingService;
import cn.exrick.xboot.core.vo.OssSetting;
import cn.exrick.xboot.file.manage.FileManage;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.InputStream;
 
/**
 * @author Exrick
 */
@Slf4j
@Component
public class QiniuFileManage implements FileManage {
 
    @Autowired
    private SettingService settingService;
 
    @Override
    public OssSetting getOssSetting() {
 
        Setting setting = settingService.get(SettingConstant.QINIU_OSS);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            throw new XbootException("您还未配置七牛云对象存储");
        }
        return new Gson().fromJson(setting.getValue(), OssSetting.class);
    }
 
    public Configuration getConfiguration(Integer zone) {
 
        Configuration cfg = null;
        if (zone.equals(SettingConstant.ZONE_ZERO)) {
            cfg = new Configuration(Region.region0());
        } else if (zone.equals(SettingConstant.ZONE_ONE)) {
            cfg = new Configuration(Region.region1());
        } else if (zone.equals(SettingConstant.ZONE_TWO)) {
            cfg = new Configuration(Region.region2());
        } else if (zone.equals(SettingConstant.ZONE_THREE)) {
            cfg = new Configuration(Region.regionNa0());
        } else if (zone.equals(SettingConstant.ZONE_FOUR)) {
            cfg = new Configuration(Region.regionAs0());
        } else {
            cfg = new Configuration(Region.autoRegion());
        }
        return cfg;
    }
 
    public UploadManager getUploadManager(Configuration cfg) {
 
        UploadManager uploadManager = new UploadManager(cfg);
        return uploadManager;
    }
 
    @Override
    public String inputStreamUpload(InputStream inputStream, String key, MultipartFile file) {
 
        OssSetting os = getOssSetting();
        Auth auth = Auth.create(os.getAccessKey(), os.getSecretKey());
        String upToken = auth.uploadToken(os.getBucket());
        try {
            Response response = getUploadManager(getConfiguration(os.getZone())).put(inputStream, key, upToken, null, null);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return os.getHttp() + os.getEndpoint() + "/" + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            throw new XbootException("上传文件出错,请检查七牛云配置," + r.toString());
        }
    }
 
    @Override
    public String renameFile(String fromKey, String toKey) {
 
        OssSetting os = getOssSetting();
        Auth auth = Auth.create(os.getAccessKey(), os.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, getConfiguration(os.getZone()));
        try {
            bucketManager.move(os.getBucket(), fromKey, os.getBucket(), toKey);
            return os.getHttp() + os.getEndpoint() + "/" + toKey;
        } catch (QiniuException ex) {
            throw new XbootException("重命名文件失败," + ex.response.error);
        }
    }
 
    @Override
    public String copyFile(String fromKey, String toKey) {
 
        OssSetting os = getOssSetting();
        Auth auth = Auth.create(os.getAccessKey(), os.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, getConfiguration(os.getZone()));
        try {
            bucketManager.copy(os.getBucket(), fromKey, os.getBucket(), toKey);
            return os.getHttp() + os.getEndpoint() + "/" + toKey;
        } catch (QiniuException ex) {
            throw new XbootException("复制文件失败," + ex.response.error);
        }
    }
 
    @Override
    public void deleteFile(String key) {
 
        OssSetting os = getOssSetting();
        Auth auth = Auth.create(os.getAccessKey(), os.getSecretKey());
        BucketManager bucketManager = new BucketManager(auth, getConfiguration(os.getZone()));
        FileInfo fileInfo = null;
        try {
            fileInfo = bucketManager.stat(os.getBucket(), key);
        } catch (QiniuException ex) {
            log.error("获取文件失败," + ex.response.error);
        }
        try {
            if (fileInfo != null) {
                bucketManager.delete(os.getBucket(), key);
            }
        } catch (QiniuException ex) {
            throw new XbootException("删除文件失败," + ex.response.error);
        }
    }
}