郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.gkhy.safePlatform.safeCheck.service.impl;
 
 
import com.gkhy.safePlatform.commons.config.file.MinioConfig;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckMinioAccessService;
import com.gkhy.safePlatform.safeCheck.util.UUIDUtil;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.management.ObjectName;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class SafeCheckMinioAccessServiceImpl implements SafeCheckMinioAccessService {
 
 
//    @Value("${safecheckminio.urlPrefix}")
//    private String externalNetworkPort;
    @Value("${safecheckminio.floders.safecheck}")
    private String minioFloders;
 
    @Value("${safecheckminio.floders.exceorderimage}")
    private String minioExceOrderImageFloders;
 
    @Autowired
    private MinioConfig minioConfig;
 
    @Autowired
    private MinioClient minioClient;
 
    /**
     * @description 获取文件下载地址
     */
    public String viewFile(String objectName){
        return viewFileDown(minioFloders+objectName);
    }
 
    /**
     * @description 获取异常文件的下载地址
     */
    public String viewExceFile(String objectName){
        return  viewFileDown(minioExceOrderImageFloders+objectName);
    }
 
 
    private String viewFileDown(String objectName){
        String url = null;
//        HashMap<String, String> map = new HashMap<>();
//        map.put("id","123");
        try {
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(minioConfig.getBucketName())
                            .object(objectName)
                            //.object(minioExceOrderImageFloders+objectName)
//                            .extraHeaders(map)
                            .expiry(60*60*5).build()
            );
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        url =
                minioConfig.getUrlPrefix() + url.substring((url.indexOf("//") + 2)).substring(url.substring((url.indexOf("//") + 2)).indexOf(
                        "/"));
        return url;
    }
 
    /**
     * @description 获取文件上传地址
     */
    public Map<String, String> getPresignUrl(String prefixName ,String suffixName){
        String  uuid= UUIDUtil.initUUID();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = formatter.format(new Date());
        String objName = nowTime+"_"+uuid+"."+suffixName;
        String url = null;
        Map<String,String> resMap = new HashMap<>();
        resMap.put("rfidImage",objName);
        try {
            Map<String, String> reqParams = new HashMap<>();
            reqParams.put("response-content-type", "application/json");
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.PUT)
                    .bucket(minioConfig.getBucketName())
                    .object(minioFloders+objName)
                    .expiry(60*60*2)
                    .extraQueryParams(reqParams)
                    .build());
            resMap.put("uploadUrl",url);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return resMap;
    }
 
    /**
     * @description 获取巡检异常图片上传地址
     */
    public Map<String, String> getExcPresignUrl(String prefixName ,String suffixName){
        String  uuid= UUIDUtil.initUUID();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = formatter.format(new Date());
        String objName = nowTime+"_"+uuid+"."+suffixName;
        String url = null;
        Map<String,String> resMap = new HashMap<>();
        resMap.put("image",objName);
        try {
            Map<String, String> reqParams = new HashMap<>();
            reqParams.put("response-content-type", "application/json");
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.PUT)
                    .bucket(minioConfig.getBucketName())
                    .object(minioExceOrderImageFloders+objName)
                    .expiry(60*60*2)
                    .extraQueryParams(reqParams)
                    .build());
            resMap.put("uploadUrl",url);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return resMap;
    }
 
    /**
     * @description 删除文件
     */
    @Override
    public String deleteFile(String name) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(minioFloders+name)
                .build());
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return "删除成功";
    }
}